Windows Communication Foundation - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF IIS Hosting

WCF IIS Hosting

shape Step-1

First open the Visual Studio. Then take the New Project -> WCF -> WCF Application -> OK like below screen. After clicking on OK button, a new screen comes like below.

shape Step-2

Two default files like IService1.cs, and Service1.cs can be seen on the right side in the above window. These two files are created automatically when you create a new WCF Application. Delete them if not needed. Here those two files not being used. But a new WCF Service class is added as shown in below screen.

shape Step-3

After Adding the WCF Service, Write a code in Interface class IMathCal.cs like below: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServiceHostinIIS { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMathCal" in both code and config file together. [ServiceContract] public interface IMathCal { [OperationContract] int additin(int x, int y); [OperationContract] int Square(int x); [OperationContract] int Cube(int x); [OperationContract] int Factorial(int x); } }[/csharp] Write a code-behind class like below [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServiceHostinIIS { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MathCal" in the code, svc and config file together. public class MathCal: IMathCal { public int additin(int x, int y) { return x + y; } public int Cube(int x) { return Factorial(x) * x; } public int Square(int x) { return x * x; } public int Factorial(int x) { int r = 1; for (int i = 1; i <= x; i++) { r = r * 1; return r; } return x * x; } } } [/csharp]

shape Step-4

Here go to View Markup, below two screen shows how to open the markup file and how to change the class name.

shape Step-5

The config file contains the server-side configurations. Only onne end point is present. This end point configures to "wsHttpBinding". Here to host in IIS, there are multiple endpoints with different bindings but HTTP Binding must be only used because IIS supports Http bindings only. [csharp] // This the importent thing to set endpoints <configuration> <system.serviceModel> <services> <service behaviorConfiguration="WCFIISHOSTING.Service1Behavior" name="WCFIISHOSTING.Service1"> <endpoint address="" binding="wsHttpBinding" contract="WCFIISHOSTING.IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service behaviorConfiguration="WCFIISHOSTING.MathCalBehavior" name="WCFIISHOSTING.MathCal"> <endpoint address="" binding="wsHttpBinding" contract="WCFIISHOSTING.IMathCal"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WCFIISHOSTING.Service1Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior name="WCFIISHOSTING.MathCalBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> [/csharp]

shape Step-6

After setting the endpoints, publish service for hosting in ISS. Below screen shows how to the publish screen and how to set the settings in publishing screen.

shape Step-7

After publishing succeeds a ".dll" file is obtained. Then open the ISS (Internet Information Service), a new screen appears like below.Below two screen shows how to add our service into IIS and all the settings.

shape Step-8

After adding our service into IIS, refresh the sites option then Website in IIS can be obtained. Then open website like below screen.

shape Step-9

After clicking on browser like above screen. A new screen like below appears. Once the above screen is obtained service is completed to host in IIS and website URL will be obtained.