RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: A 13-Step Crash Course for Learning Windows Communication Foundation (WCF) : Page 5

WCF is meant to make things easier for developers, but as with any new technology, getting started can be difficult. Follow these thirteen steps to rev up your WCF knowledge quickly.

Step 9: Building a .NET Client to Access an IIS-hosted WCF Service (Part 2 of 2)
Now that you've created the IIS-hosted WCF service, the next step is to build a .NET client to consume the service. In the .NET client, you'll add a service reference to the WCF service, similar to the way you might have added a Web reference prior to WCF.

So create a Windows Forms application, and in Solution Explorer, right click on the References item to add a Service Reference (see Figure 5). Visual Studio 2005 will prompt you for the address of the WCF service (see Figure 6), along with the default name of the local reference to the service. (You can change these if you want, but for this demo, just keep it as localhost.)

Figure 5. Adding a WCF Service Reference: Right click on the References item to add a service reference.
Figure 6. Add Service Reference Dialog: Fill in the fields to add a service reference and a default name.
After you add the service reference, Visual Studio 2005 will do three things:

Figure 7. New WCF Service Reference: Here's how Solution Explorer looks after adding a WCF service reference.
  • Automatically add a reference to System.ServiceModel.
  • Add the binding information into the local app.config file, based on the information from the service.
  • Add an interface proxy to the WCF service, which you'll programmatically access (in the same way you'd access a Web reference proxy).
Figure 7 shows Solution Explorer with these additions.

Finally, you can use the proxy object to consume the WCF service and call the back-end classes. Once again, if you previously consumed ASMX services, this should appear familiar:

   // C# code 
   localhost.CustomerClient oClient = 
      new localhost.CustomerClient();
   string cResults = oClient.GetOrderHistory(1);
   'VB.NET code
   Dim oClient As New localhost.CustomerClient()
   Dim cResults As String = _
Figure 8. Svcutil.exe: The figure shows the prompt for the svcutil.exe command-line utility.
One final note: this approach essentially "pulled" the WCF service reference from IIS. In some instances, you may wish to "push" service references out. The Microsoft Windows SDK contains a utility called svcutil.exe (short for ServiceModel MetaData Utility): this utility generates service model code from the service metadata.

Figure 8 shows a command prompt for using svcutil.exe. You specify the full URL for the service and the utility generates two files: the service proxy and an output.config file that you can incorporate into your client application.

Step 10: WCF Services Under IIS for Non-.NET Clients, (Part 1 of 3)—Creating the Service and the Configuration
Here's a complete listing for a modified web.config file for an IIS-hosted service over multiple bindings.

One of the many great things about WCF is that you can define as many endpoints as necessary—such as a specific endpoint for a .NET client, and another endpoint for a non-.NET client.
         <service name="MyService"
           <endpoint contract="IMyService"
           <endpoint contract="IMyService"
             address="ws" />
           <endpoint address="mex" 
             contract="IMetadataExchange" />
           <behavior name="FoxWcfServiceBehaviors" >
               includeExceptionDetailInFaults="true" />
             <serviceMetadata  httpGetEnabled="true"/>
       <compilation debug="true"/>
       <authentication mode="Windows"/>
       <identity impersonate="true"/>
One of the many great things about WCF is that you can define as many endpoints as necessary, such as a specific endpoint for a .NET client, and another endpoint for a non-.NET client.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date