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

Tips Search

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

  • Comments 10

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:
  24. 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();
            }

  25. 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.
  26. Press F5 to run this sucker and you should get this:

    image
  27. Press any key to continue.
  28. 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.
  29. 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
  30. 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
  31. Go to Control Panel

    image
  32. Choose Programs

    image
  33. Now select Turn Windows Features On or Off

    image
  34. 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
  35. 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 :)
  36. Next we have to configure the the web site to be bound to a TCP port for our activation to work.
  37. Open a command prompt with administrative privileges by right-clicking on the command prompt icon and choosing Run as Administrator

    image
  38. 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:*']
  39. The result should look similar to the following:

    image
  40. 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
  41. You should see something like this:

    image 
  42. 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!
  43. 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
  44. 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
  45. Enter myTcpGoodness for the Name, choose netTcpBinding for the Binding, and enter IService for the Contract as seen here:

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

    image
  47. 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
  48. You should see something like this in the BaseAddress area:

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

    image
  50. Let's make sure there are no errors.  Build the service by right-clicking on it and choosing Build Web Site.
  51. 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.
  52. 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
  53. 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
  54. You should see this dialog flash by:

    image
  55. 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.
  56. 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
  57. ServiceReference1.ServiceClient myService =
                    new CoolServiceClientCS.ServiceReference1.ServiceClient("myTcpGoodness");

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

    image
  59. 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
  60. You should see this:

    image
  61. 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!
  62. 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
  • Dude,

    The link for the mentioned subject is missing. Pl repair that and make a suitalbe link to the saviour of coding world.

    Regards 'n' Thanks,

  • Yeah, that's my bad.  Time got away from me because I forgot I had a real life launch just after the Second Life one.  Hopefully this weekend will be my big "catch up" time and I can get out several posts that I need to take care of...

  • Webcasts Page MSDN and TechNet Search Pages MSDN Webcast Page: http://msdn2.microsoft.com/en-us/events/aa740361.aspx

  • Finally updated the post that shows how to create, configure, and consume a WCF service.&#160; Check

  • &#160; For my victims at EDS :) &#160; WCF Resources http://blogs.msdn.com/zainnab/archive/2008/01/01/wcf-resources.aspx

  • Could you please include a demo of how to use the WCF service using an XML input from the client? and also generate an XML output? that would be a great help! thanks. :)

  • Newbie,

    shoot me an email at zainnab@microsoft.com and let's hash out exactly what you are looking for and then I'll whip up something to share with everyeone :)

  • Hi,

    I have a singleton wcf under IIS 7.  I can invoke it manually through the browser or via a client just fine.  However, how can i set it up such that the service will be constructed whenever the IIS is restarted??

    Thanks,

    Mike

  • Hi,

    I have a wcf singelton service running under iis 7.  I can invoke the service manually through a browser or via a client just fine.  The problem is I can't get iis to construct the wcf upon restart.  How can I accomplish this?  The wcf service needs to be running like a windows service - restarted when the machine is restarted.

    Cheers.

    Mike Bitton

  • Sounds like you need to host your WCF service in a Windows Service instead of IIS 7.  Here is a blurb on the reasons to use a Windows Service for hosting WCF:

    ===

    Often, WCF developers must decide whether to host their WCF application inside of a Windows service application or inside of the Internet Information Services (IIS) or Windows Process Activation Service (WAS) hosting environment. You should consider using Windows service applications under the following conditions:

    **Your application requires explicit activation. For example, you should use Windows services when your application must start automatically when the server starts instead of being dynamically started in response to the first incoming message.

    **The process that hosts your application must remain running once started. Once started, a Windows service process remains running unless explicitly shut down by a server administrator using the service control manager. Applications hosted in IIS or WAS may be started and stopped dynamically to make optimal use of system resources. Applications that require explicit control over the lifetime of their hosting process should use Windows services instead of IIS or WAS.

    **Your WCF service must run on Windows Server 2003 and use transports other than HTTP. On Windows Server 2003, the IIS 6.0 hosting environment is restricted to HTTP communication only. Windows service applications are not subject to this restriction and can use any transport WCF supports, including net.tcp, net.pipe, and net.msmq.

    ===

    Check out this link for more info:  http://msdn.microsoft.com/en-us/library/ms734781.aspx

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post