Pages

Windows Communication Foundation (part three)

This is the final part of a project designed to teach myself more about Windows Communication Foundation (WCF). The previous parts are here: part 1 and part 2. When we last left off, we had just finished getting the service ready to host. Now we need to create the client that will access the service.

Step Four: Create a WCF Client

I created a new console application project within my solution and named it "Client". I then added the appropriate reference to System.ServiceModel.dll. Next, we start the service created in step three.

Once our service is running, we can run the ServiceModel Metadata Utility Tool (Svcutil.exe) from the Visual Studio command prompt. Navigate to the folder where you would like the code generated. With the correct parameters, this tool will auto-generate our client code. Here was my input:


I then added the proxy file that is generated to the client project of my solution. Next, we will need to configure the client we just created.

Step Five: Configure the WCF Client

An app.config file was also generated when we ran the Svcutil.exe program from the command line. We will add this to our project. Configuring the client consists of specifying the endpoint that the client uses to access the service. An endpoint has an address, a binding, and a contract. Each of these must be specified. Here is what the Svcutil.exe tool created for our app.config file:


Svcutil.exe generates values for every setting on the binding. The endpoint is configured here (look to the endpoint element tags). Here's a link to some more information from Microsoft about how to use the generated client: link.

Step Six: Using the WCF Client

This is the final step in my little test project. Our WCF proxy has been created and configured. A client instance can be created and the client app can now communicate with the WCF service. Here's what the procedure does:

  • Creates a WCF client.
  • Calls the service operations from the generated proxy
  • Closes the client once the operation call is completed

The following code is all placed in the mainline of the Program class in Client.

  1. Create an EndpointAddress instance for the base address of the service being called and then create a WCF client object.
  2. Call the client operations from within the Client.
  3. Close the WCF client

Here is the code:


Now make sure the service is up and running before you use the client. Run the client by starting a new instance in the Solution Explorer. If all is correct, the service should send the appropriate calculations to the client, and they will be displayed. Below you can see both my Service and Client running, with a sweet picture of the space shuttle Atlantis from its final mission:


And one more time, here's a link to the completed application:





No comments:

Post a Comment