How do we host a WCF service in IIS


How do we host a WCF service in IIS?

✍: Guest


Note: - The best to know how to host a WCF in IIS is by doing a small sample. So what we will do is host the same GetCost sample which was self hosted in the previous question.
First thing you will need is to create the SVC file which exposes the service class. SVC file contains the pointer to the class. You can see from the figure below the class attribute points to the class whose interface is exposed by the service.svc.cs file. Also note the actual interface is in service.svc.cs file. Below figure has both the files service.svc which has the class attribute which points to the service class and the interface which resides in service.svc.cs file. We have taken the same sample which was self hosted in the previous question.

Figure 15.11: - The SVC file and the behind code
We also need to provide implementation for the interface. So we have made a class ServiceGetCost which has the actual implementation. Below figure shows the same in detail. In the below figure you can also see the solution files.

Figure 15.12: - Implementation of Service.svc.cs
We also need to specify the service type and endpoint in web.config file. Also note we have specified HTTP binding because we will be hosting the service on IIS.

Figure 15.13: - Web.config file for hosting service on IIS
Now that we are done with the coding part. We need to create the virtual directory in IIS. In the below figure in Step1 and Step2 we have shown how to create the virtual directory in IIS. One important thing to note while creating virtual directory set the access permission to execute.

Figure 15.14 :- IIS Configuration
In the third step we will publish the website to our virtual directory. Note the fourth step in which we have copied the svc file so that the service can be requested.
Note: - ASP.NET compilation has changed in ASP.NET 2.0. In 2.0 there is no concept of solution files. So if you want to have full compiled DLL you need to publish the project to a virtual directory.
Once you have hosted the SVC file you can test the same by request the service.svc file. If everything works fine you will get something as shown in the below figure.

Figure 15.15:- IIS WCF client
Using the Svcutil.exe you will need to generate the proxy class and the config file. The proxy and config will be same as we had done for self hosting. The one important change is the address. The config file URL now points to the service.svc which is hosted on IIS. You can run the same client which we had created for self hosting. The only change you will need to do is change the endpoint address.

LOL…You should get the same output which we had received for self hosting.

2007-11-04, 12096👍, 0💬