When a WCF service runs in IIS, it can take full advantage of IIS features such as process recycling, idle shutdown, process health monitoring, and message-based activation, it will automatically launch the host process when it gets the first client request. This hosting option requires that IIS be properly configured, but you do not need to write hosting code in your application. The disadvantage is that it only supports HTTP.
Development Environment: IIS 6.0, VS2010, C#
1. Create a new web site, select "WCF Service" as Template
Choose location as HTTP, http://localhost/MyCalculatorService and click OK Button
2. Open Service.cs, double click Service to select, and then use "Refactor | Rename" to rename it to MyCalculatorService, rename IService to IMyCalculatorService in the same way. Rename file service.svc to MyCalculatorService.svc, Service.cs to MyCalculatorService.cs, IService.cs to IMyCalculatorService.cs
3. Open IMyCalculatorService.cs, add
public interface IMyCalculatorService { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); } |
4. Open MyCalculatorService.cs, add
public double Add(double n1, double n2) { return n1 + n2; } public double Subtract(double n1, double n2) { return n1 - n2; } public double Multiply(double n1, double n2) { return n1 * n2; } public double Divide(double n1, double n2) { return n1 / n2; } |
5. Open MyCalculatorService.svc, make sure the name matches the changed name:
<%@ ServiceHost Language="C#" Debug="true" Service="MyCalculatorService" CodeBehind="~/App_Code/MyCalculatorService.cs" %> |
6. Open Web.config, modify
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="false" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service behaviorConfiguration="ServiceBehavior" name="MyCalculatorService"> <endpoint address="http://localhost/MyCalculatorService/MyCalculatorService.svc" binding="wsHttpBinding" contract="IMyCalculatorService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> |
I take out <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
from the web.config, because it causes an error as following:
When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://localhost/MyCalculatorService/MyCalculatorService.svc'.
7. Test in IE
Copy and paste 'http://localhost/MyCalculatorService/MyCalculatorService.svc' to IE, you should see something like the following, which means the service is ready to be consumed.
You can also test it using WcfTestClient tool
8. Create a Console application ServiceClient in the same solution or separate solution. Add Reference System.ServiceModel and System.Runtime.Serialization
9. Generate proxy files.
Open Visual Studio command prompt and nagivate to the ServiceClient folder, the same folder as Program.cs
10. Run
svcutil http://localhost/MyCalculatorService/MyCalculatorService.svc
Two files will be generated:
MyCalculatorService.cs and output.config
11. Rename output.config to app.config and include it in the application, also include MyCalculatorService.cs into the application. In the Main method of Program.cs, add
MyCalculatorServiceClient client = new MyCalculatorServiceClient(); Console.WriteLine("calling the service..."); Console.WriteLine("112.78+45.20=" + client.Add(112.78, 45.20).ToString()); Console.Read(); |
12. Ctrl+F5 to run the Console application, you should see:
13. Yeahhh!!! you have done it, take a break.
No comments:
Post a Comment