This is the Day 5 article. If you have not read the previous articles, then please go through the following articles:
- Day 1 - WCF Introduction and Contracts
- Day 2 - WCF Fault Contracts
- Day 3 - WCF Message Exchange Patterns
- Day 4 - WCF DataContract
Introduction
This article explains the difference between a Service Application and a Service Library. It also explains where we should use Service Applications and where to use a Service Library.
When you try to create a WCF service using Visual Studio IDE, you will have a choice of two types of WCF services, WCF Service Application and WCF Service Library. Now let us see the differences between them. We will do this by generating each of them and comparing the resutls.
Differences
- The very basic point is that you need to select a "WCF Service Application" template under the WCF project type at the time of project creation. In other words when you create the project from File >> New >> Project, select the WCF as project type from the left side of the screen and select "WCF Service Application" from the template. Select your desired location and provide an appropriate name for your project and press the "OK" button.
For a WCF Service Library you need to select "WCF Service Library" template at the time of project creation.
- After creating the projects, compare the files created by Visual Studio IDE.
In the Service Application we have a service contract i.e. IService1 for the service implementation and Service1 as a Web.config file.
In the Service Library we also have a service contract i.e. IService1 for the service implementation and Service1 as an App.config file for the configuration (instead of web.config as in the Service Application).
The major difference is that the WCF Service Application has a .svc file, whereas the Service Library does not have a .svc file. Suppose we want to host this service application in IIS, then you need to specify for IIS the execution runtime environment requirements.
In a web application we set an .aspx, similarly for a Service Application we need to set a .svc.
- Run the WCF Service Application and it will show a directory listing, as in:
Now double-click on Service1.svc and it is hosted by default by the ASP.Net Development server. You can see in the above popup window that the ASP.Net development server has been started.
A WCF Service Library is not hosted by the ASP.Net development server. It is hosted by the Test Client, since a Service Library does not have .svc file.
If you want to host your service in IIS then you should select the WCF Service Application template and if you want to host it as a Windows Service or set a reference of your library then you should select the WCF Service Library template.
Conclusion
The WCF Service Application template can be used to create WCF services with a hosting website created within the project
The WCF Service Library template can be used to create WCF services that are hosted by the WCF Service Host, and these can be tested using the WCF service Test Client.