WCF Programming - How to write a client app that connects to a WCF Service

Overview

This post is about connecting to a WCF service from a client application. The key takeaways in this post relate to understanding endpoints and how to connect to them. We will explore adding service references from client applications. We will also show you how client applications can pass data into a WCF service.

The client is also capable of receiving return data from a WCF service call.

In this post, you will learn a few things:

  • How to write and debug a client application connecting to WCF services

  • How to access named endpoints inside of an app.config on the client

  • How to attach a service reference and generate type information to simplify client programming

  • How to run both the WCF service and the client using the debugging tools


  1. Before we can connect from a client application, we need to look at the WCF services Mex endpoint. The client application will use this endpoint to retrieve type information about FlipCaseService.

    Image001

    Viewing app.config

  2. We will now add the client project to the overall solution. Right mouse click on the solution and follow the menu below.

    Image002

    Adding a new project

  3. Choose console application from the new Project dialog box. Provide a name as seen below.

    Image003

    Adding a console application

  4. Note that our solution now has to projects. One for server and one for client.

    Image004

    Viewing solution Explorer

  5. In order for the client to connect to the WCF service, we need to add a service reference. Right mouse click on the references I the client and choose Add Service reference.

    Image005

    Adding a service reference from the client

  6. Although we already looked at the endpoint so that we knew how to connect to it, we can choose the Discover button as seen below. Because our client application is in the same solution as the WCF of service, this works. Note that we will provide a namespace name before clicking OK.

    Image006

    Connecting to a Mex endpoint

  7. Adding the service reference also added two additional references as seen below.

    Image007

    Viewing the references that were added

  8. Double-click on Program.cs to open the file to be edited.

    Image008

    Opening program.cs

  9. The first red box below was about adding that using statements for the previous reference added. The second red box is the first line of code which will start the process of connecting to the WCF service. Notice the quotes are blank. Our WCF service offers three separate endpoints. The desired endpoint name is the one that gets typed into the quotes below in the second red box.

    Image009

    Adding some code

  10. The five viewing the client’s app.config file, you can get the name of each of the three endpoints. We will arbitrarily use the endpoint that has basic HTTP binding.

    Image010

    Viewing the names of the endpoints

    App.config (ConsoleServiceClient)
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <?xml version="1.0" encoding="utf-8" ?> <configuration>   <startup>     <supportedRuntime       version="v4.0"       sku=".NETFramework,Version=v4.5"/>   </startup>   <system.serviceModel>     <bindings>       <basicHttpBinding>         <binding name="BasicHttpBinding_IFlipCaseService"/>       </basicHttpBinding>       <netTcpBinding>         <binding name="NetTcpBinding_IFlipCaseService"/>       </netTcpBinding>       <wsHttpBinding>         <binding name="WSHttpBinding_IFlipCaseService">           <reliableSession enabled="true"/>         </binding>       </wsHttpBinding>     </bindings>     <client>       <endpoint         address="https://localhost:8080/flipcase/wsAddress"         binding="wsHttpBinding"         bindingConfiguration="WSHttpBinding_IFlipCaseService"         contract="ConsoleServiceReference.IFlipCaseService"         name="WSHttpBinding_IFlipCaseService">         <identity>           <userPrincipalName value="bterkaly\@redmond.corp.microsoft.com"/>         </identity>       </endpoint>       <endpoint         address="https://localhost:8080/flipcase/basic"         binding="basicHttpBinding"         bindingConfiguration="BasicHttpBinding_IFlipCaseService"         contract="ConsoleServiceReference.IFlipCaseService"         name="BasicHttpBinding_IFlipCaseService"/>       <endpoint         address="net.tcp://localhost/FlipCaseNetTcp"         binding="netTcpBinding"         bindingConfiguration="NetTcpBinding_IFlipCaseService"         contract="ConsoleServiceReference.IFlipCaseService"         name="NetTcpBinding_IFlipCaseService">         <identity>           <userPrincipalName value="bterkaly\@redmond.corp.microsoft.com"/>         </identity>       </endpoint>     </client>   </system.serviceModel> </configuration>
  11. Paste in the name of the endpoint. Then add the remaining courage you see in the red box. This is the main routine for our client file Program.cs. You can double-click on Program.cs to open it so that you can edit it.

    Image011

    Wrapping up Program.cs

    Program.cs (ConsoleServiceClient)
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConsoleServiceClient.ConsoleServiceReference; namespace ConsoleServiceClient {     class Program     {         static void Main(string[] args)         {             FlipCaseServiceClient client =                 new FlipCaseServiceClient("BasicHttpBinding_IFlipCaseService");             StringData sd = new StringData();             sd.OriginalString = "Bruno";             sd = client.FlipTheCase(sd);             Console.WriteLine("The flipped case is " + sd.FlippedCaseString);             Console.ReadLine();         }     } }
  12. Notice in the red box below we made a slight edit. The variable sd is assigned the return value from FlipTheCase() . Notice that the original string being passed in is Bruno.

    Image012

    Writing the client-side code to consume Web services

  13. After compiling the solution, you can begin by running the WCF service. Right mouse click on the Web service project and choose Debug/ Start new instance. This will load the WCF service into memory and make it available to the client application.

    Image013

    Starting the WCF service

  14. Notice the WCF service is now up and running, as evidenced by the WCF test client application in the red box.

    Image014

    Viewing the WCF Client

  15. We will now debug the client application by right mouse clicking on the client project in solution Explorer. Then choose Debug/Start new instance.

    Image015

    Starting the client

  16. Notice below that the flipped case has appeared bRUNO.

    Image016

    Verifying correctness

This concludes explanation of how a client application can consume WCF Services. Although we chose a console application, you can also choose WPF or Winforms, and more.

Summary

In this post, you learned a few things:

  • How to write and debug a client application connecting to WCF services

  • How to access named endpoints inside of an app.config on the client

  • How to attach a service reference and generate type information to simplify client programming

  • How to run both the WCF service and the client using the debugging tools