Creating, Configuring, and Consuming a WCF Service in IIS 7

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...

 

 

  1. Start up Visual Studio by running it as Administrator

    image

  2. Begin creating a new Web Site

    image

  3. Feel free to pick your language of choice, for this demo I will call the services CoolServiceCS and CoolServiceVB respectively

    image

  4. 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:

    image

  5. You should ultimately see a web page that has something like this:

    image

  6. Close the service web page and let's see if IIS7 is aware of this service

  7. Go to the IIS Manager

    image

    image

  8. Expand the tree and, under "Default Web Site", you should see your service.

    image

  9. 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.

  10. 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:

    image

  11. 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.

    image

  12. 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.

    image

  13. 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.

  14. 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.

  15. In Visual Studio, right-click on your Solution and choose Add New Project...

    image

  16. Let's use a Console Application for our client this time around.  Make sure to use your language of choice.

    image

  17. Call the application "CoolServiceClientVB" or "CoolServiceClientCS" as appropriate then click on OK.

    image

  18. Set the new project as the Startup Project.  

    image

  19. Now we need to right-click on the project and choose Add Service Reference...

    image

  20. The new service dialog is very cool and will find services in your solution if you just click on Discover --go for it! 

    image

  21. Leave all other settings as they are and click on OK.  You should see your reference in Solution Explorer.

    image

  22. Now let's crank a little code to actually use this service (finally!) heheh...

  23. Open up Module1.vb or Program.cs and put the following code in:

    VB:

    Sub Main()
           Dim myService As New ServiceReference1.ServiceClient()

           Console.WriteLine(myService.GetData(33).ToString())

           Console.ReadKey()
       End Sub

    CS:

    static void Main(string[] args)
            {
                ServiceReference1.ServiceClient myService =
                    new CoolServiceClientCS.ServiceReference1.ServiceClient();

                Console.WriteLine(myService.GetData(33).ToString());

                Console.ReadKey();
            }

  24. 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.

  25. Press F5 to run this sucker and you should get this:

    image

  26. Press any key to continue.

  27. 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.

  28. First, we have to configure Windows Process Activation Service (WAS) settings.  To learn more about WAS, go here:  https://msdn.microsoft.com/en-us/library/ms730158.aspx

  29. 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 https://msdn.microsoft.com/en-us/library/ms752218.aspx

  30. Go to Control Panel

    image

  31. Choose Programs

    image

  32. Now select Turn Windows Features On or Off

    image

  33. 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.

    image

  34. 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 :)

  35. Next we have to configure the the web site to be bound to a TCP port for our activation to work.

  36. Open a command prompt with administrative privileges by right-clicking on the command prompt icon and choosing Run as Administrator

    image

  37. 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:*']

  38. The result should look similar to the following:

    image

  39. Now the Default Web Site is bound to TCP but we still need to allow TCP for our applicationCopy and paste the following into the command prompt area and press Enter:

    VB
    %windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/CoolServiceVB" /enabledProtocols:http,net.tcp

    CS
    %windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/CoolServiceCS" /enabledProtocols:http,net.tcp

  40. You should see something like this:

    image 

  41. 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!

  42. 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.

    image

  43. 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.

    image

  44. Enter myTcpGoodness for the Name, choose netTcpBinding for the Binding, and enter IService for the Contract as seen here:

    image

  45. We also need to identify the BaseAddress for getting to our service.  Go to the Host area in the current dialog.

    image

  46. Under Base Address, click on New... and type in the following for the address then press enter:

    VB
    net.tcp://localhost/CoolServiceVB

    CS
    net.tcp://localhost/CoolServiceCS

  47. You should see something like this in the BaseAddress area:

    image

  48. Close the configuration dialog and you should be prompted to save changes.  Click Yes.

    image

  49. Let's make sure there are no errors.  Build the service by right-clicking on it and choosing Build Web Site.

  50. WOW!  We are on step 50 now.  Does it feel like your brain is going to explode like in the movie "Scanners"? https://www.imdb.com/title/tt0081455/  Just stay with me, we are almost done.

  51. 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.

    image

  52. 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.

     image

  53. You should see this dialog flash by:

    image

  54. 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.

  55. 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:

    VB
    Dim myService As New ServiceReference1.ServiceClient("myTcpGoodness")

    CS

    ServiceReference1.ServiceClient myService =
                    new CoolServiceClientCS.ServiceReference1.ServiceClient("myTcpGoodness");

  56. Now just run your client application and you should see the predictable result.

    image

  57. 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

  58. You should see this:

    image

  59. 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!

  60. 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.  :P