Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence / machine-learning

Exploring WCF 3.5 Tools - WcfSvcHost and WcfTestClient

3.73/5 (8 votes)
3 Feb 2008CPOL6 min read 1   1.5K  
Disucssing use of WCFSvcHost and WcfTestClient for Service hosting and testing

Introduction

Windows Communication Foundation 3.5 was released to general public in November 2007 which leverages the existing WCF libraries provided as part of .NET 3.0 and provides further enhancement to the Microsoft’s suite of distributed application development framework. Windows Communication Foundation is by far the most comprehensive framework provided for distributed application development on the .NET platform which can be interoperable, robust, scalable and easy to develop. The underlying plumbing is mostly handled by the WCF libraries so the developer’s can focus on creating the business logic instead of worrying about protocols, sockets and bindings required for communication.

<o:p>In WCF 3.5, the salient enhancements can be listed as follows.
<o:p>

  • Add Service Reference Enhancements
  • Various New Tools
    • WCF Provided Test Client
    • WCF Provided Host
  • UriTemplates Support
  • webHttpBinding addition to the Binding Stack.
  • Support for REST Style Services
  • Create RSS & ATOM Feeds for Services
  • More WS-* Protocols
  • Better Support for WCF in a Partial Trust Environment
  • ASP.NET <st1:city w:st="on"><st1:place w:st="on">AJAX Support for Consuming WCF Services
  • Ajax Integration and JSON support
  • Better Integration between Workflow and WCF<o:p>

The focus of this article is to explore the two utilities provided by WCF 3.5, WcfSvcHost and WcfTestClient.

Background

For anyone with a understanding of ABC’s of WCF i.e. Address, Binding and Configuration, it is self evident that a typical WCF library requires two components for its exposure and consumption i.e. a service host and a consuming client. A WCF library in itself can be simply defined as piece of business logic encapsulated with attributes which support nicer things such as contracts and versioning however; the actual exposure of the service depends on the hosting application or service.
<o:p>

There are several ways of hosting a WCF service supported in WCF 3.0 including IIS hosting, Self Hosting, WAS (Windows Activation Service) and Windows Service hosting. A simple breakdown of hosting options is as follows.

Operating System

Supported Protocol

Hosting Options

Windows XP

HTTP

Named pipes, TCP, MSMQ

IIS 5.1 or self-host

Self Host

Windows <st1:place w:st="on">Vista

HTTP, named pipes, TCP, MSMQ

HTTP

IIS 7.0/WAS or self-host


IIS 6.0

Windows Server 2003

HTTP

Named pipes, TCP, MSMQ Self-host, HTTP

IIS 6.0

Self Hosting

Windows Server 2008

HTTP, named pipes, TCP, MSMQ

IIS 7.0/WAS or self-hos

<o:p>The binding and further details about service end point as well as host are defined in the config file using the following four elements in the System.ServiceModel class.

  • Services: This element holds one or more <service> definitions. Each <service> section is associated with a particular service type and includes any base addresses and endpoints to be exposed for that type.
  • Client: This element holds one or more <endpoint> definitions for each service endpoint the client may consume. Each individual <endpoint> includes an address, binding, and contract matching a service endpoint.
  • Bindings: This element holds one or more binding configuration sections. This makes it possible to customize a binding instead of using the defaults provided by a standard binding, such as webHttpBinding.<o:p>
  • Behaviors: This element contains <serviceBehaviors> and <clientBehaviors>. At the host, the <service> configuration section may reference an element from the <serviceBehaviors> section to apply local behavioral settings to the service. At the client, endpoints defined in the <client> section may reference an element from the <clientBehaviors> section to apply local behavioral settings to the client communication channel.

Therefore, a service can have multiple end points as the following example shows.

<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/WCFInsiders" />
<add baseAddress="net.tcp://localhost:9000/ WCFInsiders" />
<add baseAddress="net.pipe://localhost/ WCFInsiders" />  
</baseAddresses>   
</host>  

<o:p>

Using the WcfSvcHost and WcfSvcClient

Now instead of creating a test client and a host with every project, these two new utilities WcfSvcHost and WcfTestClient allow us to seamlessly test a service without having to write extra code or committing to a specific binding. Both of these utilities can be find here.

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

First you can create a simple WCF Service library project in Visual Studio.NET 2008.

WCFLibrary_dialog.JPG


The syntax for WcfSvcHost is as follows.

Usage: WcfSvcHost.exe /service:<PathToServiceDLL>  /config:<PathToServiceConfig> [/client:<pathToClientApp>][/clientArgs:<argumentsToBePassedToClientApp>]


As your probably notice that the arguments in the square brackets are optional and therefore, a simple call can be as follows.

WcfSvcHost /Service:MySampleService.dll /config:app.config


And in the example provided with this article, the command line looked like this

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE>WcfSvcHost /service:c:\Development\MySampleService\MySampleService\bin\Debug\MySampleService.dll /config:c:\Development\MySampleService\MySampleService\App.config

WCFService_Host.JPG


Once you’ve started the service host, you have a meta-data end point available which can be used to start the client. In the case of MySampleService, the mex end point is

http://localhost:8731/Design_Time_Addresses/MySampleService/Service1/mex


A typical syntax for WcfTestClient would be

WcfTestClient.exe <ServiceMetaDataURI>


You can specify any metadata address, such as HTTP-GET, or metadata endpoint over HTTP, TCP, or IPC (named pipes). Multiple metadata addresses can also be specified as shown in the statement below

WcfTestClient.exe http://localhost:8000/ net.tcp://localhost:9000/MEX


Now the test client can be invoked by using the following command.

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE>WcfTestClient http://localhost:8731/Design_Time_Addresses/MySampleService/Service1/mex

WCF_Test_Client.JPG


Windows Communication Foundation (WCF) Test Client (WcfTestClient.exe) is a tool which allows testing the service via a generic user interface without writing any custom code. It also displays the response returned from the service.

WCF Service Host can host the following WCF service project types:

• WCF Service Library
• WCF Service
• Sequential Workflow Service Library
• State Machine Workflow Service Library
• WF State Machine Service
• WF Sequential Service
• XAML WF State Machine Service
• XAML WF Sequential Service
• RSS / ATOM Library
• WAP Service
• Web Site Service.

Conclusion.

One of the biggest benefits of both of these tools is their stand alone execution capability. These can be used outside visual studio from a command prompt which makes them a highly valuable choice in testing environments where web server is either not available or setting it up requires jumping lots of hoops. Using these utilities makes hosting and testing WCF services much easier. With a lot of practical use in rudimentary testing, I hope developers would find these tools useful in their everyday jobs.

About the Author

Adnan Masood works as a Sr. Software Engineer / Technical Lead in a Monrovia based financial institution where he develops middle tier architectures, distributed systems and web applications using Microsoft .NET framework. He holds various professional memberships (ACM, BCS, and ACS) and several technical certifications including MCSD.NET, MCAD.NET and SCJP-II. Adnan is attributed and published in print media and on the web and holds a Masters Degree in Computer Science from Nova Southeastern University, FL and is currently pursuing his doctoral studies in Machine Learning. Adnan has taught WCF courses at University of California at San Diego and regularly present at local code camps. He is actively involved in the .NET community as co-founder and president of San Gabriel Valley .NET Developers group. His blog can be found at www.AdnanMasood.com and he can be reached via email at adnanmasood at acm.org

References

HTTP Programming with WCF and the .NET Framework 3.5 - Justin Smith<o:p>
http://msdn.microsoft.com/msdnmag/issues/08/01/WCFinOrcas/default.aspx<o:p>

What's New for WCF in Visual Studio 2008Juval Lowy <o:p>
http://msdn.microsoft.com/msdnmag/issues/08/02/Foundations/default.aspx<o:p>

Mix 2007 Talk by Don Box and Steve Maine “DEV03 - Navigating the Programmable Web” http://sessions.visitmix.com/default.asp?event=1011&session=2012&pid=DEV03&disc=&id=1512&year=2007&search=DEV03

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)