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
ServiceHost events and trigger alerts when the
ServiceHost starts or faults. For Services that are exposed through HTTP binding, if
HTTPGet is enabled, you have an option of hitting the URL through a browser to query the Metadata. For Services exposed through TCP and
NamedPipe binding, 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
Ping method 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
Ping method by using a concept of WCF Extensions, the Operation Behavior. The behavior would add the
Ping method 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
Ping method is ready, let us see how the Caller works. The primary job of the Caller is to invoke the
Ping method 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
Ping method that was dynamically added to the contract.
As a pre-requisite, the utility expects the Service to expose the querying of metadata through
IMetadataExchange and 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
ping method and a set of classes used for invoking the
Ping method through a Dynamic Proxy.
WCFPing.Hosting shows how to host a Service with the
Ping Behavior enabled.
WCFPing is the command line utility that you use to
Ping for the service.
The Extension method
EnablePing of XtensionMethods.cs under the WCFPing.Lib provides implementation for adding the
Ping method at runtime. The implementation is straight forward as explained in the article, Adding Dynamic Methods in WCF. The
Ping method takes a
string parameter and returns a
string value containing the input ping back data appended with the time of the server.
WCFServicePing class does the job of the caller. This class is invoked by the
WCFPing console application. This class is responsible for invoking the
Ping method 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.
WCFServicePing class 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
Ping method is invoked on the Client instance.
WCFPing can 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
-verbose switch 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.Hosting project. You can start this application and then run the utility project
WCFPing to check out how the ping works.
WCFPing. Hope you find this utility useful.
- 28th June, 2010: Initial post