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 | 15 comments | 18028 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? :)


On 9/12/2008 11:22:33 AM Darrell Mozingo said ..
F'in brilliant! Just what we needed. Thanks man!


On 9/12/2008 1:17:01 PM Sahil Malik said ..
LOL. Glad to be of service Darrell.


On 9/24/2008 2:40:40 AM Harsh said ..
That was a good article. I have a question. I have hosted my WCF service on IIS. Do my services goes to sleep if I dont use my service for longtime?


On 1/22/2009 1:42:12 PM Ricardo said ..
Hi, This is a good article. I followed all your recommendations and it worked. But, the problem is that I need to host a service in WAS with TCP instead of HTTP. I already search other articles like http://bloggingabout.net/blogs/marc/archive/2007/10/23/wcf-hosting-non-http-protocols-in-iis-7-0.aspx and http://msdn.microsoft.com/en-us/magazine/cc163357.aspx but they dont show me the complete process with a sample like you and therefore it didn't work after so many tries. Do you know or do you have some basic sample on hosting and consuming a service using TCP and WAS???


On 2/5/2009 10:21:40 AM vivek said ..
That is neat!!! Thanks much


On 2/13/2009 5:54:10 AM Brett said ..
Good post - one question though...

When I follow the above guide, I can see the service summary that you've shown in step 11, but only on the server that I'm hosting it with - am I missing some permission settings somewhere that allows PCs on my domain to access this service?


On 2/18/2009 10:45:35 AM Guy Ellis said ..
Thanks - some of that helped!


On 3/19/2009 4:13:16 PM SilverDark said ..
Sahil,

on Windows 2008, ServiceModelReg.exe has to be executed in "Run As Administrator" mode. The option is available in the right-click menu on the command line prompt (just right-click on the icon, ok? That's the new design in Windows Server 2008, the UI "Dont make me think" in reverse)

Great article, saved me a lot of potential headache.

The cooler the platform gets, the more interesting the deployment gets :)


On 3/23/2009 11:52:51 PM james bond said ..
required more slides


On 4/9/2009 2:15:29 AM Janne said ..
I love this site!


On 4/21/2009 12:30:53 PM Shail said ..
Hi, We are doing a load test against WCF Servcie on IIS 7.0 and Windows 2008 over TCPIP Binding. We ran the test for 10 minutes and got 40,000 exceptions in Perfmon of the same type. Exceptions were as follows:


System.ArgumentException: The AddressFamily Unspecified is not valid for the System.Net.IPEndPoint end point, use InterNetwork instead.

Can you please any insight you might have on this or any pointers.. Thankss !!


On 6/3/2009 7:52:23 PM Shobana said ..
Thanks a ton! This post was very useful and to the point. Loved it.

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.