After much delay, here are the goods 🙂
The goal here is to show how to create a basic WCF service that can be hosted in IIS7 and will kick over to other protocols using Windows Activation Service (WAS) as needed. Your choice of language doesn't matter for this one and I will show code for C# and VB. And awaaaaaaay we go...
- Start up Visual Studio by running it as Administrator
- Begin creating a new Web Site
- Feel free to pick your language of choice, for this demo I will call the services CoolServiceCS and CoolServiceVB respectively
- Press F5 to build and run the application just to make sure all is well so far, you will get the obligatory error for debugging, just click OK:
- You should ultimately see a web page that has something like this:
- Close the service web page and let's see if IIS7 is aware of this service
- Go to the IIS Manager
- Expand the tree and, under "Default Web Site", you should see your service.
- Okay! So far everything seems to be going well. Now let's take a quick peek at the service in Visual Studio to verify that it is, in fact, using HTTP to communicate.
- There are a couple of ways we can do this: One way is to just open up Web.config and scroll to the <services> element:
- Another way is to right-click on your Web.config and choose "Edit WCF Configuration..."
SPECIAL NOTE: There seems to be a "feature" that makes this not show up initially. This is usually "fixed" by simply going to Tools...WCF Service Configuration Editor
The funny thing is you don't appear to have to actually use this tool just open it and close it then you can right-click Web.config to get the editor option. Weird.
- If you expand the Service area and look at the Endpoints, you can see that wsHTTPBinding is used. Once you have verified, close this dialog box.
- The only thing I wish here is that the endpoints would be "automagically" named for us (we could always change the names) but, sadly, that is not the case.
- At this point we have created our service, run it, verified it is in IIS, and verified it uses wsHTTPBinding. Now we need to create a client to consume the service.
- In Visual Studio, right-click on your Solution and choose Add New Project...
- Let's use a Console Application for our client this time around. Make sure to use your language of choice.
- Call the application "CoolServiceClientVB" or "CoolServiceClientCS" as appropriate then click on OK.
- Set the new project as the Startup Project.
- Now we need to right-click on the project and choose Add Service Reference...
- The new service dialog is very cool and will find services in your solution if you just click on Discover --go for it!
- Leave all other settings as they are and click on OK. You should see your reference in Solution Explorer.
- Now let's crank a little code to actually use this service (finally!) heheh...
- Open up Module1.vb or Program.cs and put the following code in:
- This should look pretty straightforward. We are calling one of the sample operations that are available with our WCF Service and then use ReadKey() to pause when we run the app.
- Press F5 to run this sucker and you should get this:
- Press any key to continue.
- So we have a client and we have a service and we are all having a group hug. Now what? Well, now let's configure this puppy to use other protocols besides HTTP. For this run, we will use TCP and Named Pipes.
- First, we have to configure Windows Process Activation Service (WAS) settings. To learn more about WAS, go here: http://msdn.microsoft.com/en-us/library/ms730158.aspx
- Assuming you're running Vista, we have to tell WAS that non-HTTP protocols can be used for activation. If we were not using IIS to host this service we would also have had to tell it that HTTP activation is possible but fortunately IIS handles those details for us. For more info on what we are doing here, you can check out this area http://msdn.microsoft.com/en-us/library/ms752218.aspx
- Go to Control Panel
- Choose Programs
- Now select Turn Windows Features On or Off
- Go to the Microsoft .NET Framework 3.0 area and check the WCF Non-HTTP Activation. NOTE: Since this is my play machine, I also turned on WCF HTTP Activation as well but you don't have to unless you need it.
- Click on OK; It may take a minute to install the needed files so be patient. Go grab a drink or something, go on, you deserve it 🙂
- Next we have to configure the the web site to be bound to a TCP port for our activation to work.
- Open a command prompt with administrative privileges by right-clicking on the command prompt icon and choosing Run as Administrator
- Copy and paste the following command into the command prompt area and press Enter
%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']
- The result should look similar to the following:
- Now the Default Web Site is bound to TCP but we still need to allow TCP for our application. Copy and paste the following into the command prompt area and press Enter:
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/CoolServiceVB" /enabledProtocols:http,net.tcp
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/CoolServiceCS" /enabledProtocols:http,net.tcp
- You should see something like this:
- Okay, the hard part is done. We now have the Web Site and the application within the site configured for TCP. We just need to tell the service to use TCP and then have the client use TCP to talk to the service. Here we go!
- To configure the service, go to Visual Studio and Solution Explorer. Now let's go to our service and right-click on Web.config to edit the WCF Configuration. You may have to do that stupid Tools...WCF Service Configuration Editor workaround to get the context menu item to show up. You may wonder why I just don't have you edit the XML directly and the answer is simple: I'm all about easy and think that this way is easier for folks.
- We need to make sure the service knows it can use TCP by creating a new endpoint. Currently the only usable endpoint for communicating is HTTP but we are going to change that by right-clicking on endpoints under the Service and adding a new service endpoint.
- Enter myTcpGoodness for the Name, choose netTcpBinding for the Binding, and enter IService for the Contract as seen here:
- We also need to identify the BaseAddress for getting to our service. Go to the Host area in the current dialog.
- Under Base Address, click on New... and type in the following for the address then press enter:
- You should see something like this in the BaseAddress area:
- Close the configuration dialog and you should be prompted to save changes. Click Yes.
- Let's make sure there are no errors. Build the service by right-clicking on it and choosing Build Web Site.
- WOW! We are on step 50 now. Does it feel like your brain is going to explode like in the movie "Scanners"? http://www.imdb.com/title/tt0081455/ Just stay with me, we are almost done.
- At this point we could start configuring the client but I really like to run the service one last time to make sure there are no issues. So this step is optional but suggested just to make sure everything is good with the service: (1) Right-click on the Service.svc file and set it as the Start Page. Right-click on the service project and choose Set as Startup Project. Press F5 to run the application. You should see the test page for the service. If you do then all is well, if not go back and fix any issues. Make sure to close the web page when you are done here and set the client application as the Start Up Project again.
- Okay now on to the client. Since we have updated the service we need to update the Service Reference on the client. This is easily done. Simply right-click on ServiceReference1 and choose Update Service Reference.
- You should see this dialog flash by:
- We are almost done. Now we just have to tell the client what protocol to use based on the binding name. Remember when you set the Name of the TCP Binding to myTcpGoodness? Well you did that to make this step easier for us. Go to Module1.vb or Program.cs to see the code for your client application.
- Now we will pass the name of the binding to use in the constructor so that the client uses the protocol we want. Change the code for creating an instance of the service to this:
Dim myService As New ServiceReference1.ServiceClient("myTcpGoodness")
- Now just run your client application and you should see the predictable result.
- At this point you are probably asking if it is REALLY using TCP. It's easy enough to test. From your command prompt type in the following:
DANGER: DO NOT RUN THIS COMMAND ON A PRODUCTION MACHINE UNLESS YOU LIKE GETTING FIRED!
net stop w3svc
- You should see this:
- You have stopped IIS and killed HTTP as an option for communicating to our service. Now run the client application again. Does it work? Then you are in business!
- We are done but if you want you can add net.pipe to be used just like you did with net.tcp and see if it works. Play with it and enjoy! Oh yeah, you might want to type in "net start w3svc" from the command prompt to get IIS running again. 😛
Dim myService As New ServiceReference1.ServiceClient()
static void Main(string args)
ServiceReference1.ServiceClient myService =
ServiceReference1.ServiceClient myService =