Is it possible/how do I stop and start the HTTP MEX listener of a self hosted WCF service at runtime without affecting the primary WCF service?
(Please don’t ask why I want to do this. It is a hack to get around artificial limitations imposed by someone else.)
*****[Re-added this answer after re-test and code cleanup] This is actual code that I have added to my generic WCF-based service development framework and it is fully tested.*****
Assuming that you start with MEX enabled on the
ServiceHost…First, add these namespaces…
Secondly, define the
IWCFStateinterface…Thirdly, create a static class for some
ServiceHostextension methods (we’ll fill them in below)…Now let’s fill in the extension methods…
Stopping MEX on a
ServiceHostat RuntimeThen call it like this…
Starting MEX (again) on a
ServiceHostat RuntimeThen call it like this…
Summary
This design allows you to use some messaging methods to send a command to the service itself or to code that is hosting the service and have it carry out the enabling or disabling of a MEX
EndpointDispatcher, effectively turning off MEX for thatServiceHost.Note: This design assumes that the code will support MEX at startup, but then it will use a config setting to determine if the service will disable MEX after calling
Open()on theServiceHost. This code will throw if you attempt to call either extension method before theServiceHosthas been opened.Considerations: I would probably create a special service instance with management operations that did not support MEX at startup and establish that as service control channel.
Resources
I found the following two resources indispensable while figuring this out:
.NET Reflector: class browser, analyzer & decompiler for inspecting assemblies like System.ServiceModel.dll: http://www.red-gate.com/products/reflector/
Extending Dispatchers (MSDN): provides a fantastic high-level diagram of the class composition of a WCF service: http://msdn.microsoft.com/en-us/library/ms734665.aspx