The different ways to host a WCF Service Application in a Web App

There are at least three different ways to configure the hosting of a WCF Service Application inside a Web Application.

They are:

  1. Add a WCF Service directly via “Add New Item…”
  2. By copying files, including the service implementation code-behind file
  3. By copying files, including the compiled, service implementation DLL

In this post, I’m going to take a walk through each of these methods.

Method #1: Add a WCF Service directly via “Add New Item…”

This is the most straightforward of the methods. Right-click on the Web App project name and select Add | New Item…:

image

Then, select WCF Service and click Add:

image

All the grunt work has been done for you: a reference to System.ServiceModel has been added, the Web App’s web.config has been modified, and the service contract and implementation files have been added. Your project layout will look something like this (new files highlighted):

image

That’s it for this method. You can run and see the service by navigating to Service1.svc. But this model does not fit all situations. To see why, let’s move on to Method #2.

Method #2: By copying files, including the service implementation code-behind file

The first method is fine when you have a Web Application and just want to host a WCF service in that one and only project. But what about reusability? If you want to use the WCF service across two or more web sites you generally don’t want to have duplicate code existing in two different places. You can create a base class, and derive each service from that, but I’d rather just have the entire service contained within a single project. Then it’s just a matter of deploying the service onto the different web sites (or web projects).

One way of doing that is to copy the service’s contract, implementation, and config into the web project:

1.) First, create a new WCF Service Application in a separate solution.

2.) Copy each of the following files from the root of the service application folder to the root of the web application folder:

  • IService1.cs
  • Service1.svc
  • Service1.svc.cs

Right-click on your web app project name and add these files via the Add Existing Item…dialog.

3.) Add a reference to System.ServiceModel:

image

4.) From your WCF Service Application’s web.config, copy the <system.serviceModel> section and paste into your Web Application’s web.config (right after the </runtime> closing tag).

5.) Run the Web Application and navigate to Service1.svc. You should see the service.

That’s not horrible; you can probably automate most of the file copies and really only need to add the files themselves to the web project the first time. There is, however, a slightly easier way.

Method #3: By copying files, including the compiled, service implementation DLL

The WCF Service Application compiles down to a DLL. Let’s take advantage of that. While this step doesn’t differ that much from Method #2, it is another alternative.

1.) Compile the WCF Service Application.

2.) Copy the resulting DLL into the Web App’s bin folder or alternatively an “imports” folder.

3.) In the Web Application, add a reference to the service DLL:

image

4.) Add a reference to System.ServiceModel.

5.) Copy only the Service1.svc file (not the code-behind or contract files) from the service project folder to the web project folder and add it to the Web Application via the Add Existing Item… dialog.

6.) Open the Service1.svc file and remove the CodeBehind attribute from ServiceHost.

4.) From your WCF Service Application’s web.config, copy the <system.serviceModel> section and paste into your Web Application’s web.config (right after the </runtime> closing tag).

5.) Run the Web Application and navigate to Service1.svc. You should see the service.

It’s really the same number of steps as Method #2. The biggest difference is that instead of copying over the contract and implementation files, you instead copy the single DLL. The advantage of this method, however, is that once you’ve done the initial setup (add references, copy over config info) the only thing you’ll have to do if the service implementation changes is copy over the DLL.

Leave a Reply

Your email address will not be published. Required fields are marked *