Click here to Skip to main content
Click here to Skip to main content

WCF Ping

By , 28 Jun 2010
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Acknowledgement

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

Concept

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.

Implementation

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

The solution contains 3 projects. The WCFPing.Hosting, WCFPing.Lib and WCFPing. 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.

The 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.

The 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.

Usage

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

WCFPing1.JPG

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.

WCFPing2.JPG

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.

Conclusion

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

History

  • 28th June, 2010: Initial post

License

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

About the Author

Krishnan Srinivasan
Architect
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberyepimafreak20-May-11 4:09 
Exactly what I was looking for ! Thanks !
General[My vote of 2] Great idea - but very limited usefulness PinmemberMarc Scheuner28-Jun-10 8:20 
GeneralRe: [My vote of 2] Great idea - but very limited usefulness PinmvpPIEBALDconsult28-Jun-10 18:23 
GeneralRe: [My vote of 2] Great idea - but very limited usefulness PinmemberKrishnan Srinivasan29-Jun-10 0:27 

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 | Mobile
Web02 | 2.8.140415.2 | Last Updated 28 Jun 2010
Article Copyright 2010 by Krishnan Srinivasan
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid