Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

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

Posted on 4/12/2008 @ 11:52 PM in #WCF | 3 comments | 4690 views

Okay boys'n'girls, and everyone else - listen up. 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 ..

Knock Knock
who'se there
Alex
Alex who?
Alex'plain why IIS 7 can be a real pain in the donkey.

Now, we already wrote up a WCF Service over here. Actually, what you wrote, was a WCF Service Library - the guts of the service itself are actually a DLL. And that is great, because now the host can be anything, independent of the service implementation itself. So the same service, can be hosted in IIS, WAS, or your custom host. NEAT.

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 (Doh!). 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.


On 4/18/2008 10:43:05 AM John said ..
Good timing...I actually came across this issue the same day you posted it! One challenge I did run into, though, was that when I ran the command listed (ServiceModelReg), the service loaded (as opposed to the 404 error before), but it acted like .net 3.0 was registered as opposed to 3.5. It was very similar to when you try to start an ASP.NET 2.0 site when the site is running under 1.1. I couldn't find a similar command in any of the 3.5 directories, and ended up just running the repair of .Net 3.5, which apparently registered IIS correctly. Any ideas?

On 5/7/2008 6:00:04 AM Ajeet said ..
Well I am atleast proud to be the only third user to use the Combo IIS7 + WCF(Syndication namespace) .NET 3.5.

On 5/7/2008 6:05:22 AM Sahil Malik said ..
Ajeet - Who are the first two? :)

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.