Monday 14 May 2012

WCF - Create, host and consume a WCF Service using the WCF Service Library template in VS 2008

Create, host and consume a WCF Service using the WCF Service Library template in Visual Studio 2008

Create a new project using the WCF Service Library template

  1. In Visual Studio 2008, create a new WCF Service Library project.  For this example I named the project MyWcfServiceLibrary image
  2. The template creates a Class Library project with the following files:
    1. IService1.cs
      1. IService1
        interface that defines a ServiceContract with the following methods (OperationContracts):
        1. string GetData(int value)
        2. CompositeType GetDataUsingDataContract(CompositeType composite)
      2. CompositeType
        Class that defines a DataContract with the following properties (DataMembers):
        1. public bool BoolValue
        2. public string StringValue
    2. Service1.cs
      Implementation of the IService1 interface
    3. App.Config
      contains sample config entries to define the endpoints for Service1
  3. Compile the project.  It should compile without any problems.

Test the WCF Service

  1. Run the project.
  2. The WCF Test Client tool should open
    1. A notification window should pop up telling you that the service is being hosted image
    2. The WCF Test Client should open.image
      1. Take note of the url (http://localhost:8731/Design_Time_Addresses/MyWcfServiceLibrary/Service1/mex)
      2. A config file is generated that contains the configuration settings you can use if you want to host it somewhere else (like IIS, WAS or your own application)
      3. You’ll see the Service available as IService1.  WSHttpBinding_IService1 is the name of the service as defined in the config file
  3. Double click on the GetData() method in the left hand pane.  Enter a value and click on Invoke. View the results.  Rejoice.image

Host the WCF Service in IIS using the Personal Webserver

  1. Create a new ASP.Net Web Application in the same solution and add a reference to the project created above.  For this example I named the project MyASPWcfServiceHost. Remember to set this project as the startup project.
  2. Create the file that will host the service (it’s a svc file)
    1. Add a new item and choose Text file image
    2. Edit the file and add the following:
      <%@ServiceHost language=c# Debug="true" Service="MyWcfServiceLibrary.Service1"%>
      1. Note the namespace (MyWcfServiceLibrary) which corresponds to the namespace of the Class Library we created earlier.
    3. Set this file as the start page.
  3. Update the config file to register the endpoint of the service.  Take a look at the config file generated by the WCF Test Client for some guidance when necessary.  (I used it a lot)
    1. Edit the web.config file and add the following


      binding=”wsHttpBinding”
      contract=”MyWcfServiceLibrary.IService1″ />


Test the hosting

  1. Run the application.  You should see a page like this: image
    1. This means our service is hosted and working.
    2. If you read through the page you’ll notice that metadata sharing is not enabled.  The page is kind enough to tell us how to turn it on (if we want).

Enable metadata sharing and test again

  1. Modifying the web.config file as follows


    binding=”wsHttpBinding”
    contract=”MyWcfServiceLibrary.IService1″ />
    binding=”mexHttpBinding”
    contract=”IMetadataExchange” />










  2. Run the application.  You should now see a page like this:image This page now tells us what to do and how to use the service.  Which is what we are going to do next.

Consume the hosted service

We’ll create a console application that uses our hosted service.  Fun :)   We can follow the advice of the test page shown above, but not now – let’s do it another way.

Create the console application

  1. Create a new console application (MyConsoleClient) in the same solution.  Remember to set it as the startup project.

Create the service proxy

We’ll use Visual Studio to do the work for us instead of running the command line as mentioned in the test page shown above.
  1. Right-click on References and select Add Service Reference.
  2. Click on “Discover”
  3. Choose the MyTestService.svc from the list (note the url) and click on OK.
    image
  4. The proxy class and config file are created and added to the project, as well as additional references.
    image
  5. In the Object Browser we see the following:
    image
    1. We have the IService1 interface, as well as a definition of the CompositeType created in the service.
    2. We also have Service1Client which is what we’ll use to consume the service.

Consume the service

  1. Add a reference to the proxy:
    using MyConsoleClient.ServiceReference1;
  2. Copy the code provided by the test page as a starting point.
  3. Use the service and display the results.
    static void Main(string[] args)
    {
    Service1Client client = new Service1Client();
    int myValue = 7;
    string serviceResponse = client.GetData(myValue);
    Console.WriteLine(serviceResponse);
    Console.ReadLine();
    // Always close the client.
    client.Close();
    }
  4. Run the application and see our output.
    image

Debugging

If you try to debug right now, you might get this message:  Unable to automatically step into the server.  The remote procedure could not be debugged.  This usually indicates that debugging has not been enabled on the server.  See help for more information.
image
  1. The “Server” refers to the hosting server, which is the ASP host.
  2. Modify the web.config file of the MyASPWcfServiceHost project to enable debugging.
    1. Modify the and set it to true.
  3. Run the project in debug mode again and you’ll be able to step into the actual GetData() method.  Pretty cool.

No comments:

Post a Comment