I wrote a class that I am using to create and open my ServiceHost instances for each service. I want to allow them to add in a MEX endpoint. Below is the code for the Register method which creates all the hosts. When I later loop through the host list and call Open on all the hosts, the second host to be opened errors out with "Additional information: The ChannelDispatcher at '_THE_MEX_ADDRESS' with contract(s) '"IMetadataExchange"' is unable to open its IChannelListener." If I remove the MEX code it runs fine and opens each host. How do I fix this?

Edit: Added full class

Edit2: Keep in mind this is fully in code and not in app.config

public class ServiceHostMgr { #region Constructor public ServiceHostMgr(string ip, int httpport, int nettcpport, bool discoverable) { _hosts = new List<ServiceHost>(); _ip = ip; _httpport = httpport; _nettcpport = nettcpport; _discoverable = discoverable; } #endregion #region Fields private string _ip; private int _httpport; private int _nettcpport; private bool _discoverable; private bool _disc = false; #endregion #region Properties private List<ServiceHost> _hosts; public List<ServiceHost> Hosts { get { return _hosts; } } #endregion #region Methods public ServiceHostMgr Register(Type serviceType, Type implementedContract, string path) { string nettcpuri = "net.tcp://" + _ip + ":" + _nettcpport.ToString(); string httpuri = "http://" + _ip + ":" + _httpport.ToString(); List<Uri> baseAddresses = new List<Uri>(); baseAddresses.Add(new Uri(nettcpuri)); if (_discoverable) baseAddresses.Add(new Uri(httpuri)); ServiceHost host = new ServiceHost(implementedContract, baseAddresses.ToArray()); /* if (_discoverable) { ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); // If not, add one if (smb == null) smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; host.Description.Behaviors.Add(smb); host.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex" ); } */ if (_discoverable) { ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); // If not, create new one, set values, add to collection if (smb == null) { smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; // add to behaviors collection host.Description.Behaviors.Add(smb); // add service endpoint host.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex" ); } } NetTcpBinding nettcpbinding = new NetTcpBinding(); host.AddServiceEndpoint(serviceType, nettcpbinding, nettcpuri + "/" + path); _hosts.Add(host); return this; } public ServiceHostMgr Open() { foreach (ServiceHost h in _hosts) h.Open(); return this; } public ServiceHostMgr Close() { foreach (ServiceHost h in _hosts) h.Close(); return this; } #endregion }

Here is the calling code: