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

Posted on 4/12/2008 @ 11:52 PM in #WCF by | Feedback | 62827 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.

Sound off but keep it civil:

Older comments..


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.


On 8/12/2009 7:46:12 PM SJMavelil said ..
In Step 9 does the svc file have only the directive <% @ServiceHost Service="MyServices.HelloWorld" %>


or is there a code bihind too? I have only the directive. but the service does not seem to be running. I am confused by your statement


Note that it looks very much like ASP.NET code behind .. shocking huh?


On 8/14/2009 1:54:09 AM Alex Angas said ..
Thank you! Thank you! Very helpful series. The only one amongst many I've tried that actually got me started.


On 8/24/2009 7:38:03 AM Vibhas Devnani said ..
The images here in the points 5,6 and 11 are appearing as red crosses..! Please help!


I tried it with different browsers on several machines...its not loading the images..


I also tried to go to the links where these images have been uploaded..on imageshack.com it gives the error "503 Service Unavailable".. HELP!!


:(


On 9/1/2009 5:07:45 PM Jeff Paredes said ..
We had the same problem using IIS7, W2k8, and WCF. I found many blog posts with instructions, similar to above. However, none of them worked in my case.

I found this article, which fixed my problem.

http://blogs.msdn.com/rjohri/archive/2009/06/29/the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration.aspx

This seems to automate the steps above. Hope this helps.


On 9/23/2009 6:19:04 AM Jarkko said ..
Excellent article! Very clear and easy to follow. Thank you!


On 9/24/2009 9:20:24 AM Craig Johnson said ..
Good work on this POST. It saved my job. I wasted so much time getting WCF webservices working on IIS 7 with Win2008 server. The ServiceModelReg command worked like a charm. Thanks a bunch.


On 10/14/2009 2:36:43 PM Nathan said ..
Damn good post. Keep it up, dude!


On 12/15/2009 1:33:58 AM sajiv said ..
thanks a lot :)


On 12/28/2009 12:14:30 PM Munish Singla said ..
Thanks, It really works.


On 12/29/2009 3:15:02 PM Andrei said ..
I get "The caller was not authenticated by the service" from a remote client with wsHttpBinding (I ran svcutil on the remote machine to generate the client's app.config). Any ideas?


On 1/16/2010 6:49:53 PM Kenneth said ..
I have setup the service and can access it locally but when I try to access it via the internet I cannot access it.

I can access the default page for IIS from the internet but I cannot access the HelloWorld.svc.

What do you mean Network Service and IIS_IUSRS so far as permissions? Do I need to add a new User for IIS_IUSRS?

I am new to IIS and I can remote into my box but I cannot access a service via IIS 7, the service hosted in a .NET app or installed as a service. When ran hosted in a .NET app I add the application thru the firewall on any port but still cannot access it.

I would appreciate any help I can get.

Thanks,

Kenneth


On 1/17/2010 9:42:38 AM Kenneth said ..
After more digging and the fact that I needed to open the port and making it matched to what port 80 settings had for the default website that is installed with IIS I was finally able to see and use the service.

Thanks for this blog and the previous blog which really helped.

Kenneth


On 1/20/2010 10:08:58 AM Chan Souvannarath said ..
Thank you for posting this solution, I just moved from the old server with iss6 to the new one with iis7. My web service did not work untill I found this posting and I went to the server and run that ServiceModelReg.exe and it works. I develop silverlight applications that uses the web service


On 2/8/2010 11:37:04 PM Peter said ..
Great post! I also read the other 2 post on WCF in general and on how to create WCF Library. Very simple! Thanks a lot!


I used IIS on Windows 7 and didn't need step2:

Go to your Win2k8 server, and add the "Web Server" role - make sure ASP.NET is enabled.


On 2/12/2010 4:15:43 PM Chris Deweese said ..
Just saved the day with your post! Thanks!


On 3/1/2010 12:52:47 PM Scott C. said ..
Great article! Thanks for documenting the step by step on the WCF service.


On 3/7/2010 8:07:09 AM Dror said ..
Thanks for the halpfull information - it saved me much time.


One question: How the IIS "knows" to look for the .\bin directory for the service dll?


(i.e. I want to expose the .\bin\debug version instead of copying it from the build folder).


Thanks.


On 3/10/2010 9:11:39 PM Genere35 said ..
works like a charm, thanks!


On 3/22/2010 10:03:15 AM Alberto said ..
Wonderful :-)


It worked.


On 3/25/2010 5:04:49 PM Raghuraman said ..
Hi Sahil,

This is the article I mentioned about in the CAPAREA.NET presentation day which helped us get WCF Hosted Successfully.

Thank you very much.

Raghuraman

Raghuraman


On 4/1/2010 11:49:36 AM Senthil said ..
Shahil ,

I had a chance to watch your sharepoint trainign in dnrtv . It was really good .

I have one quick question , How to call dll application in WCF service library . This is dll is legasy COM . Please let me know your thought in this regards .

Thanh you ,


Shahil.


On 5/9/2010 6:18:42 AM Rytis Ūsalis said ..
Hello, I spend 2 hours searching how to setup WCF in IIS 7.0 (i had no experience in IIS configuring). This article helped me to solve problem in 5 minutes. Thank you very much.


On 5/27/2010 8:35:40 AM Brian King said ..
Just as a note if you're using .Net 4.0 the process is slightly different. Overall though, these few articles on WCF have been more useful to me than half of the books I have looked at.


On 5/27/2010 4:14:21 PM Sahil Malik said ..
Brian - thanks for your comment. Are you talking about the configuration by convention difference or is there something else?


On 6/2/2010 6:29:48 AM TheDevelopers said ..
Thanks a lot.. this post was exceptionally good and helpful.


On 6/23/2010 5:39:00 AM Simon R said ..
Thank you my friend .. you are an absolute star.


On 6/25/2010 6:41:37 PM Pete Bock said ..
Nice post. Very useful and spot on ... I think. Would be more helpful if the images (for steps 5,6,7,11) were not broken. Is this a problem only I am experiencing or are others seeing it too.

Note: The image for step 10 shows fine.

Thanks


On 7/5/2010 6:09:41 AM Neluka said ..
Hi,

We have 2 windows 2008 standard edition servers. One is acting as the application server and the other is the DB server. DB is SQL 2005 standard version. Our application developed with .Net 3.5 and using WCF and "httpbinding" is used. We install the application using click once. It fine the application gets installed on client machines, shows the login screen with branches but when we try to log into the system it gives an error msg "The underlying connection fails". Main suspect is when the app is trying to write to DB it gives this 'cos those methods are transactional. I would be greatful if you can give us advice on this. And this app was worked fine with windows server 2003.

Regards

Neluka


On 7/23/2010 6:04:09 AM Geert said ..
Step 7 can be done autmatically by adding WCF Activation in Windows Server 2008′s Features for .NET Framework 3.0

found it here:


http://dbvt.com/blog/post/WCF-SVC-4043-W2K8-IIS7-Fix.aspx


On 8/11/2010 8:16:10 AM emirhan said ..
how can ı create client(.net2.0)-wcf(.net3.5)


can you halp me ? it s important for me


On 8/11/2010 8:27:48 PM Rajesh said ..
Thanks for the blog. It really helped me


On 8/24/2010 12:22:10 PM Scott said ..
I sure wish those broken image links would get fixed. Make's it kinda hard to follow the process when the screen shots are MIA. : /

Thanks,


Scott


On 8/24/2010 12:24:41 PM Scott said ..
Sure wish those screen shots weren't MIA. Can't follow those steps that use missing screen shots as examples of what we're supposed to do. :(


On 8/24/2010 3:31:03 PM Sahil Malik said ..
Scott, the screenshots are there, I think your ISP might be blocking imageshack.


On 9/7/2010 12:41:49 PM Radhakrishna Naik said ..
Hi Sahil,


This was great post and worked for my sample WCF application on IIS7 and win 2008.


We had WCF host under in Sharepoint web application which worked fine with IIS 6 and win 2003. As soon as I moved in IIS 7 and win 2008, it get an error "there is a duplicate 'system.web.extensions scripting". WCF folder is configured as an application under main Sharepoing web application and configured to execute under same application pool as sharepoint 2007.


can you please help me?

Regards,


RK


On 11/26/2010 4:19:06 AM Tsvetoslav said ..
Hi Sahil,


I have a WCF service deployed as part of a web part .wsp. The service is being deployed to the _vti_bin folder and is called from the Web Part through javascript. However, when the WCF service request is sent, SharePoint tries to authenticate the user and no matter what credentials you enter, it won't let you in. The only solution I have found so far is to disable Anonymous Authentication for the _vti_bin folder but this leads to other problems. Could you shed some light on that as an expert on WCF services in SharePoint 2010.

Thanks in advance.


On 12/20/2010 8:34:49 AM srikanth said ..
Hi,


I wrote one Wcf Service and hosted it locally, it is working fine. I am able to call it.But the problem is,i want to use that wcf service to be called from my share point 2010 foundation,iis7,windows server 2k8.

For that i followed the way that created a website project in .net by adding service reference.And created client proxy in aspx.cs and called that service.Now i copied that aspx, aspx.cs into layouts. while running my application it is throwing error as service reference could not be found.


and also i copied my service.svc into ISAPI with web.config.I did not found any change in error.

Please help me,


Thanks..


On 1/12/2011 9:35:27 PM Willy said ..
I think all you would need to to is to set Application Pool must set to .Net Framwork v4.0.


On 1/19/2011 2:12:20 AM Goran Tesic said ..
Hello,

My situation is as follows:

1) I developed WCF service using VS2010 on computer PC1 and published it using wizard to IIS 7 on the same computer. I've checked the code in to TFS2010. Everything works fine.

2) When I go to another computer PC2 and get the latest version from TFS2010, I can't load my WCF service source code because there's an error message that said I couldn't create virtual directory on remote computer, which is PC1.

All I've set up is in WCF service project properties where I've checked "Use Local IIS Web server" option on "Web" tab. I've put there this: http://0099FRSLABTS01:8001/. This is the address of IIS 7 server on PC1.

The problem is VS2010 on PC2 sees this server http://0099FRSLABTS01:8001/ as local IIS, but it's not.

How can I make it work?

Thank you in advance.

Regards,

Goran


On 2/20/2011 5:25:13 PM JT said ..
Hi Sahil,

Thank you for your contributions on .NET Rocks!

I am working in Windows 7, IIS 7. This DOES NOT work for me.

I've set up dozens of ASMX web services on XP IIS 5.0 and Windows 2003 IIS (not sure of version). I even set up a couple of WCF services on XP quickly with no effort at all. I set up a couple of simple WCF services on the same W7 IIS 7 machine by painstakingly going through a Microsoft Press book (hated it), but there is nothing that can let me reliably set up even a plain vanilla WCF web service that I create with File New, build, publish.

I'm sure I'm doing something wrong, but I've been looking all over for a simple checklist for setting up a WCF service. I was hoping this was it, but it's not. At this point I am completely unable to create another WCF service hosted on my W7 machine.

With http://localhost/MyService.svc all I get is "HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly." With the port specified, I just get "Internet Explorer cannot display the webpage" with no discernable error information.

There MUST be an easy way to do this! Can you help?

Thanks!


On 2/21/2011 3:17:08 PM Sahil Malik said ..
Hi JT,

Check and see if the dependent assemblies for the service are accessible to your localhost.

S


On 2/23/2011 11:19:42 AM IfSomeoneAsksYou JustSayYouDon'tKnow said ..
calling from wcfestclient.exe works fine indeed, but this Microsoft toy does not really act as a client. If you use a real asp.net client app for instance, you'll get 405: method not allowed


I did what Microsoft suggested: edit handlers in host config file to allow POST (which what WCF uses, but Microsoft is such a group of stupid people that they created 2 products: WCF and IIS from the same company that cannot communicate).


The best (and only) way to make a web suff work is.... to stay away from Microsoft.


Aren't shocked by the amount of instructions, the complexity of what you write to just do an "hello world"

If people would be smart, they would refuse to play stupid games. That comes from buying million $ ugly 2br houses to using Microsoft products


On 3/4/2011 9:47:02 AM Gabriele Guizzardi said ..
I guys,


I discover today that if you put a service under W2008 with IIS7 and ISA server, you can test and use your service with localhost settings (port 80) from remote. If you want to use it under default VS port (like 8731) from remote, you can receive back a timeout error. This because this kind of ports needs by default a certificate. So, who have problem with "timeout" remote message try port 80.


On 7/11/2011 5:43:54 AM TonyC said ..
Ledgend!!

I've been banging by head against a brick wall for I don't know how long. As soon as I ran the command line at step 7 everything worked as sweet as a nut!!

Thanks, Tony


On 7/15/2011 5:00:29 AM Murali.M said ..
Hi,

It worked for me.


thanks for your post.

Thanks,


M.M


On 7/18/2011 2:30:56 PM Hal Martin said ..
I found that at least in Vista there is one more step required if you create a new application pool instead of simply putting this site in the default pool. Without this extra step, it is not possible to access the service in Vista. The step is to change the identity of the new application pool to Network Service. That is done by selecting the new pool, rightclicking, and choosing Advanced Properties in the context menu. In the dialog which comes up, look for Identity, and set it to Network Service. You have already given Network Service the required permissions so the service will now be accessable


On 7/26/2011 12:14:34 PM Dmitry said ..
Ошибка HTTP 500.21 - Internal Server Error


Обработчик "svc-Integrated" содержит поврежденный модуль "ManagedPipelineHandler" в списке модулей


On 8/6/2011 4:38:48 AM Nawaz said ..
Great work Sahil. Keep it up.


On 9/21/2011 10:12:03 AM shooter25 said ..
Nice, but it needs updated to IIS7.5 and Newer screen shots. I link at the begining on how to create the web service was one of the best I have read online! Thanks!


On 10/14/2011 4:13:14 PM mike said ..
Windows 7 here....did all the steps threw it out and started again. Stuck on the error:

"The type 'MyServices.HelloWorld', provided as the Service attribute value in the ServiceHost directive could not be found."

no idea how to trouble-shoot this.

Getting WCF to work in VS = no problem


getting to work in the real world with IIS = big problem.


The MS tutorial did not work either.

if anyone can point me to a solution that would be great.


On 12/12/2011 10:16:08 AM Frank said ..
haha... a lot of 2 persons here :)


On 2/22/2012 9:01:25 AM James said ..
This is the only proper tutorial on hosting a WCF Service in IIS!!


On 3/13/2012 11:37:50 PM Farhan said ..
Works like a charm. I also had the following problem "WCF - Error 500.21 Handler "svc-Integrated" has a bad module "ManagedPipelineHandler" in its module list". The resolution is to run aspnet_regiis -i in the visual studio command prompt


On 3/6/2013 2:58:23 AM TottiYang said ..
Thanks so much. I also think the MSDN guide article is sub-optimal.


Thanks again~


On 4/9/2013 11:14:29 AM Ross said ..
My God, I slaved over this for hours and the end result was a chronic "FAIL" message when I tired to navigate to the svc file. I was teetering on the edge of a complete nervous breakdown when I finally re-read each step in your instructions with my magnifying glass and an electron microscope...until...ALOHAA YEAH BABY! Put the .dll files into the BIN folder you dufus! The BIN folder...BIN BIN BIN...doh!


Dude. Thanks for this excellent tech post.