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).
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 =
string cResults = oClient.GetOrderHistory(1);
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.
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 necessarysuch as a specific endpoint for a .NET client, and another endpoint for a non-.NET client.|
<behavior name="FoxWcfServiceBehaviors" >
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.