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.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
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.
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.
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.
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.
WCFPing. Hope you find this utility useful.
- 28th June, 2010: Initial post