Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 166313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:04:34+00:00 2026-05-11T12:04:34+00:00

Is it possible/how do I stop and start the HTTP MEX listener of a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:04:34+00:00Added an answer on May 11, 2026 at 12:04 pm

    *****[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…

    The following solution is written in terms of a ServiceHost subclass (WCFServiceHost<T>) that implements a special interface (IWCFState) for storing an instance of the MEX EndpointDispatcher class.

    First, add these namespaces…

    using System.ServiceModel; using System.ServiceModel.Dispatcher; 

    Secondly, define the IWCFState interface…

    public interface IWCFState {     EndpointDispatcher MexEndpointDispatcher     {         get;         set;     } } 

    Thirdly, create a static class for some ServiceHost extension methods (we’ll fill them in below)…

    public static class WCFExtensions {     public static void RemoveMexEndpointDispatcher(this ServiceHost host){}      public static void AddMexEndpointDispatcher(this ServiceHost host){} } 

    Now let’s fill in the extension methods…

    Stopping MEX on a ServiceHost at Runtime

    public static void RemoveMexEndpointDispatcher(this ServiceHost host) {     // In the simple example, we only define one MEX endpoint for     // one transport protocol     var queryMexChannelDisps =              host.ChannelDispatchers.Where(                 disp => (((ChannelDispatcher)disp).Endpoints[0].ContractName                                             == 'IMetadataExchange'));     var channelDisp = (ChannelDispatcher)queryMexChannelDisps.First();      // Save the MEX EndpointDispatcher     ((IWCFState)host).MexEndpointDispatcher = channelDisp.Endpoints[0];      channelDisp.Endpoints.Remove(channelDisp.Endpoints[0]); } 

    Then call it like this…

    // WCFServiceHost<T> inherits from ServiceHost and T is the Service Type, // with the new() condition for the generic type T.  It encapsulates  // the creation of the Service Type that is passed into the base class  // constructor. Uri baseAddress = new Uri('someValidURI'); WCFServiceHost<T> serviceImplementation = new WCFServiceHost<T>(baseAddress);  // We must open the ServiceHost first... serviceImplementation.Open();  // Let's turn MEX off by default. serviceImplementation.RemoveMexEndpointDispatcher(); 

    Starting MEX (again) on a ServiceHost at Runtime

    public static void AddMexEndpointDispatcher(this ServiceHost host) {     var queryMexChannelDisps =             host.ChannelDispatchers.Where(                     disp => (((ChannelDispatcher)disp).Endpoints.Count == 0));     var channelDisp = (ChannelDispatcher)queryMexChannelDisps.First();      // Add the MEX EndpointDispatcher     channelDisp.Endpoints.Add(((IWCFState)host).MexEndpointDispatcher); } 

    Then call it like this…

    serviceImplementation.AddMexEndpointDispatcher(); 

    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 that ServiceHost.

    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 the ServiceHost. This code will throw if you attempt to call either extension method before the ServiceHost has 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# start Windows Service programmatically I want to start/stop a preexisting windows
Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Is it possible to stop the execution of a function in javascript on click
Is it possible to stop the automatic preventDefault() from applying in a simple Jquery
is it possible to stop the submission of a form,after submit button has been
Is it possible to stop all started services when the user hits the Home
I'm trying to figure out if its possible to stop a foreach loop in
Given a certain set of parameters is it possible to stop a set of
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.