Benefits: If IIS is used as the host, a host is not required for the service.ProgramAnd deployment is relatively easy.
First, prepare the WCF program for testing. This example is just for simple sum calculation,CodeAs follows:
Calculatorservice. SVC(Service file ):
<% @ Servicehost Language = "C #" Debug = " True " Service = " Owenzhang. wcfsamples. calculatorservice " %>
Icalculator. CS(Define a service contract ):
[Servicecontract]
Public Interface Icalculator
{
[Operationcontract]
Int Add ( Int N, Int M );
}
Calculatorservice. CS(Executing the service contract ):
Public Class Calculatorservice: icalculator
{
Public Int Add ( Int N, Int M)
{
Return N + M;
}
}
Note:: Assume that the new project type isWebsiteThen,Icalculator. CSAndCalculatorservice. CSThe file is located inApp_codeFolder. SimilarlyCalculatorservice. SVCCodebehind = "~ /App_code/calculatorservice. cs ".
InWeb. configAdd an endpoint to the file:
<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< Configuration >
< System. servicemodel >
< Services >
< Service name = " Owenzhang. wcfsamples. calculatorservice " >
< Endpoint address = "" Binding = " Wshttpbinding " Contract = " Owenzhang. wcfsamples. samples. icalculator " />
</ Service >
</ Services >
</ System. servicemodel >
</ Configuration >
Then, create a new website or virtual directory in IIS. We will create the virtual directory "C: \ Inetpub \ wwwroot \ calculatorservice" under the default website of the system ", copy the compiled files of the program to the calculatorservice directory, and you can call the service through the endpoint.
Note:: If you are using IIS 7.0 (in Windows Vista and Windows Server 2008 environments), check WCF activation (Server Manager-> features->. HTTP activation (HTTP) and non-HTTP activation (Message Queuing, TCP, and named pipes) in Net Framework 3.0 features have been installed. Otherwise, the related services will fail to be loaded. In addition, if the application extension ing for identifying. SVC files is missing in IIS, you need to add them manually.