In Visual Studio 2008 (Beta 2), there are two WCF application templates.
The WCF Service Library is a class library that defines a WCF service, that can be exposed through a variety of WCF hosting options (i.e., IIS, a custom built WCF host, or a Windows Service). This WCF class library defines the service, operation and data contracts and implements the service specification. The WCF class library also contains a app.config file which also has the settings that any host would need to expose the services endpoints, behaviors, etc. In fact, if you set the WCF class library as a start up project in Visual Studio 2008, it will host the service in a web application and launch the WCF Test Client (part of VS 2008) to allow inspection and test execution of the WCF class library's methods.
The WCF Service Application is a template that assumes you are building a service that will only be hosted within IIS. Thus, it defines the service, equips the project with a web.config and places the details about the endpoints, behaviors, etc. in that file. When set as a startup project, it will host the service in the development web server and listen for calls.
Observing the differences between these two templates is important because it might have ramifications on how WCF applications are developed. If you want to have your WCF application code hosted in eventually multiple hosts (i.e., IIS, custom built or Windows Service) the best approach is to place your service definition and implementation in a WCF class library. If your service will only ever need to be exposed as an IIS service (i.e., if its a front-end service wrapping some business logic/functionality that you exposed to external business trading partners via the Internet), then baking the code into the host like the WCF Service Application template does makes sense. In fact, at a later date, you can migrate the code into a WCF class library and "hallow out" the IIS host by point it to the library.
The trick I recently did is to place my service implementation in a WCF class library template, but expose it to a consuming application in the same solution. When adding a service reference to a WCF client, Visual Studio 2008 allows you to automatically discover services within the same solution. However, because a WCF class library has no host, it isn't visible as a service that can be added to a client. What is required is a host for the WCF class library.
To allow the WCF client to see the WCF class library as a service, I added a WCF Service Application project template to the solution and followed these steps:
- Add a project reference to the WCF class library project
- Rename the Service1.svc file to the service name of my choice
- Deleted the Service1.svc.cs code behind file as we won't have any WCF Service implementation in this project
- Viewed the markup of the Service1.svc file and altered it accordingly to refer to the types referenced from the WCF class library
<@ServiceHost Language="C#" Debug="true" Service="ProjectName.Service.Library.ProjectNameService"%>
<@Assembly Name="ProjectName.Service.Library" %>
Note that Service is the namespace of the referenced WCF class library dll and that the assembly is the name of the referenced assembly
- Altered the web.config, replacing any mention of Service1 with the correct service name from the referenced assembly.
- Note that an approach to this is to compare the web.config with the app.config of the WCF class library to make sure the service name and behavior between the files are equal
- Build the WCF Service Application and test the wsdl by visiting the Service1.svc file and clicking on the wsdl link
- Right click your WCF Client application project file in Visual Studio (in my case a Windows Forms project) and select Add Service Reference
- Click the discover button and select to discover services within the solution
- Assign the service a proper namespace
At this point, you should have a separate WCF class library that can be hosted anywhere (i.e., Windows Service, IIS, or custom-built host) and that can be debugged and tested by the built in WCF Service test harness. You also should have a wrapping IIS host for the WCF Service that can be discovered by potential WCF Service clients in the solution.