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 ..
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.
Build your Service Library in release mode.
Go to your Win2k8 server, and add the "Web Server" role - make sure ASP.NET is enabled.
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.
Fix the security on this dir by adding the following permissions
Ensure that "Network Service" has Read & Execute + List folder contents + Read.
Ensure that IIS_IUSRS have Read & Execute + List folder contents + Read.
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 -
Next, create a website in IIS7 with the following settings -
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
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.
Next, create a file called HelloWorld.svc at C:\Code\HelloWorld with the following text in it -
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.
Now browse to this URL - http://localhost:8000/HelloWorld.svc, you should see the service hosted as shown below -
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.