WCF - Difference Between Service Application and Service Library: Day 5

This is the Day 5 article. If you have not read the previous articles, then please go through the following articles:

  1. Day 1 - WCF Introduction and Contracts
  2. Day 2 - WCF Fault Contracts
  3. Day 3 - WCF Message Exchange Patterns
  4. 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

  1. 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.

    WCF-Service-application.jpg

    For a WCF Service Library you need to select "WCF Service Library" template at the time of project creation.

    WCF-Service-library.jpg
     
  2. 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.

    Service1-Web.config-file.jpg

    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).

    web.config-in-service.jpg

    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.
     
  3. Run the WCF Service Application and it will show a directory listing, as in:

    Run-WCF-Service.jpg

    asp.net-development-server.jpg

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.

Service1.svc.jpg

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.

Test-Client,-service-library.jpg

WCF-SVC-Host.jpg

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.
 

Next Recommended Readings