Click here to Skip to main content
11,647,748 members (70,268 online)
Click here to Skip to main content

Tagged as

WCF Ping

, 28 Jun 2010 CPOL 30.9K 1.7K 20
Rate this:
Please Sign up or sign in to vote.
A Command-line utility to ping WCF Services


After a WCF service is hosted in a ServiceHost, there is no direct way to check if a WCF service is running. One can subscribe to the ServiceHostevents and trigger alerts when the ServiceHoststarts or faults. For Services that are exposed through HTTP binding, if HTTPGetis enabled, you have an option of hitting the URL through a browser to query the Metadata. For Services exposed through TCP and NamedPipebinding, a direct option is not available. I thought a command line utility like the Ping would be very handy. This Command line utility is just for that. It helps you to ping a WCF Service.


I would like to acknowledge and thank the authors of the following two articles. I have built this utility only on top of that.


A typical Ping implementation requires a method to be exposed by the service that can be queried by a caller. The method generally returns some data from the service.

From a Design perspective, a Pingmethod may not always be part of the Service Contract. And adding one permanently to every Contract is also not elegant. Without compromising these two aspects, we can achieve our goal of having a Pingmethod by using a concept of WCF Extensions, the Operation Behavior. The behavior would add the Pingmethod that we need, to all the Contracts at runtime. With this Behavior turned off through a simple setting in a configuration file, a proxy could be generated without this method and distributed during development. And when the Service is deployed, the Behavior can be turned on for testing or diagnostics on a live environment.

Now that the Pingmethod is ready, let us see how the Caller works. The primary job of the Caller is to invoke the Pingmethod through the Service Proxy. Since the utility needs to be generic enough to work with any WCF Service, a static Proxy cannot be tied to the Caller. So the WCF Service Proxy should be created dynamically at runtime. This would keep the utility generic. Here is what the Caller does. Given the WCF MEX URL, the caller downloads the Metadata, builds the instance of the Client and invokes the Pingmethod that was dynamically added to the contract.

As a pre-requisite, the utility expects the Service to expose the querying of metadata through IMetadataExchangeand no security defined on the exchange Binding.


The implementation is fairly self explanatory. So I have not shown any explicit code here.

The solution contains 3 projects. The WCFPing.Hosting, WCFPing.Liband WCFPing. The WCFPing.Lib contains all the core classes used for both adding the dynamic pingmethod and a set of classes used for invoking the Pingmethod through a Dynamic Proxy. WCFPing.Hosting shows how to host a Service with the PingBehavior enabled. WCFPingis the command line utility that you use to Pingfor the service.

The Extension method EnablePingof XtensionMethods.cs under the WCFPing.Lib provides implementation for adding the Pingmethod at runtime. The implementation is straight forward as explained in the article, Adding Dynamic Methods in WCF. The Pingmethod takes a stringparameter and returns a stringvalue containing the input ping back data appended with the time of the server.

The WCFServicePingclass does the job of the caller. This class is invoked by the WCFPingconsole application. This class is responsible for invoking the Pingmethod on a dynamically created Service Proxy instance. The logic behind the Dynamic Proxy is explained in the article, Dynamic Proxy Factory. I have completely re-factored the code and have only retained what is needed for this implementation.

The WCFServicePingclass first imports the Metadata of the given Service through the MetadataImporter. Using the Metadata, the complete source code for the Proxy is generated through the ServiceProxySourceGenerator. Using the Proxy source, a dynamic Assembly is generated using the ServiceProxyAssemblyFactory. From the Assembly, the Proxy Client is instantiated using the ServiceProxyInstanceFactory. Finally the Pingmethod is invoked on the Client instance.


The WCFPingcan be fired from the command prompt. The usage and the output are illustrated in the screen shot below.


A more verbose output can be got by using the -verboseswitch as illustrated in the screen shot below. This switch provides the complete exception stack on errors.


As an example, I have provided a simple WCF Service under the WCFPing.Hostingproject. You can start this application and then run the utility project WCFPingto check out how the ping works.


That's WCFPing. Hope you find this utility useful.


  • 28th June, 2010: Initial post


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


About the Author

Krishnan Srinivasan
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
yepimafreak20-May-11 4:09
memberyepimafreak20-May-11 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150804.3 | Last Updated 28 Jun 2010
Article Copyright 2010 by Krishnan Srinivasan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid