Saturday, March 13, 2010

WCF Service Hosting on Production Server

Host a WCF Service in IIS 7 & Windows 2008 - The right way

I am going to cover some information here in this blog post that is nowhere else on the web. That is not surprising, consider that less than 2 people in the world are actually using WCF + .NET 3.5 + IIS 7.

Anyway, deploying a WCF service using .NET 3.5 framework to IIS 7 requires you to jump through a couple of hoops ..

Well, in this blogpost, I am going to cover hosting the service in IIS 7. I saw this MSDN article talking about hosting the WCF service in IIS. I thought that article was very sub-optimal. Here are my reasons:

a) It won't work for IIS 7 . IMO it's quite bad that an MSDN article isn't upto the times - and they expect us devs to be up on all this cool new shiny stuff :-).
b) It talks of creating an App_Code directory, and throwing a .cs file in there - this is okay for your development environments, but not for production code deployment. Your deployment process must not require you to work in .cs.
c) That article literally talks about writing a new WCF service in IIS - Dangit!

I'm going to instead talk about - you've developed a service, now how the heck we deploy it. Well, here are the steps.

  1. Build your Service Library in release mode.
  2. Go to your Win2k8 server, and add the "Web Server" role - make sure ASP.NET is enabled.
  3. Create a dir on your disk - I put mine in c:\code\HelloWorld <-- this will be the physical directory that will run my WCF service.
  4. Fix the security on this dir by adding the following permissions
    1. Ensure that "Network Service" has Read & Execute + List folder contents + Read.
    2. Ensure that IIS_IUSRS have Read & Execute + List folder contents + Read.
  5. Now, go to IIS MGR, and add a new application pool called "Hello World". Ensure that this Application Pool is set to the settings as shown below -

  6. Next, create a website in IIS7 with the following settings -

  7. Are we done yet? Heck no! Apparently, WCF .. doesn't work with IIS 7 OOTB. So you need to run command prompt as administrator, and run the following command -

    "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r -y
  8. Now, the DLL that you created in step #1, copy that to C:\Code\HelloWorld\Bin .. where C:\Code\HelloWorld is the physical path of my website.
  9. Next, create a file called HelloWorld.svc at C:\Code\HelloWorld with the following text in it -

    <% @ServiceHost Service="MyServices.HelloWorld" %>

    Note that it looks very much like ASP.NET code behind .. shocking huh?
  10. Next, create a web.config with the following -
  11.    1: xml version="1.0" encoding="utf-8"?>
       2: <configuration>
       3:   <system.serviceModel>
       4:     <services>
       5:       <service behaviorConfiguration="MyServices.HelloWorldBehavior" name="MyServices.HelloWorld">
       6:         <endpoint address="" binding="wsHttpBinding" contract="MyServices.IHelloWorld" />
       7:         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
       8:         <host>
       9:           <baseAddresses>
      10:             <add baseAddress="http://localhost:8000/" />
      11:           baseAddresses>
      12:         host>
      13:       service>
      14:     services>
      15:     <behaviors>
      16:       <serviceBehaviors>
      17:         <behavior name="MyServices.HelloWorldBehavior">
      18:           <serviceMetadata httpGetEnabled="true" />
      19:           <serviceDebug includeExceptionDetailInFaults="false" />
      20:         behavior>
      21:       serviceBehaviors>
      22:     behaviors>
      23:   system.serviceModel>
      24: configuration>

    As you can see, I have set a base address of http://localhost:8000 <-- change that to what suits you. Also, I have created 2 endpoints, one uses wsHttpBinding, and the other uses mexHttpBinding.
  12. Now browse to this URL - http://localhost:8000/HelloWorld.svc, you should see the service hosted as shown below -

  13. Thats it! You can either use svcutil now, or you can hand-create a channel/proxy to use the above service as shown in this earlier blogpost of writing a WCF client.


There are other ways of hosting a WCF service too. You could host them using WAS - Windows Activation Service. Hosting them in WAS or IIS means, you don't have to worry about the host lifetime. IIS and Win2k8/Vista take care of the host lifetime for you. However, for the rather interesting situation of two WF's talking to each other, you may need to have an EXE that is both a WF host, and a WCF host, as I described here. In such instances, you may need to write your very own host. As it turns out .. it's rather easy to write your own WCF host.

No comments: