2014-02-11 59 views
0

中註冊具有多個ServiceHost實例的MEX端點我編寫了一個用於爲每個服務創建並打開我的ServiceHost實例的類。我想讓他們加入MEX端點。以下是創建所有主機的Register方法的代碼。當我稍後循環訪問主機列表並在所有主機上調用Open時,第二臺主機將被打開出錯,其中包含「其他信息:具有合同」IMetadataExchange「的ChannelDispatcher'_THE_MEX_ADDRESS'無法打開它IChannelListener「。如果我刪除MEX代碼,它運行良好並打開每個主機。我該如何解決?無法在代碼

編輯:加滿級

EDIT2:請記住,這是完全的代碼,而不是在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 
} 

下面是調用代碼:

 ServiceHostMgr hostMgr = new ServiceHostMgr("localhost", 8012, 8002, true) 
      .Register(typeof(IPurchaseOrderSvc), typeof(PurchaseOrderSvc), "PurchaseOrder") 
      .Register(typeof(ILoginSvc), typeof(LoginSvc), "Login") 
      .Open();    

     Console.WriteLine("Running..."); 
     Console.ReadLine(); 
     hostMgr.Close(); 

回答

0

好,你正在檢查,看看行爲是否已經存在 - 這很好。

但即使它已經存在 - 你試圖再次添加它 - 這是你的問題。

ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); 

// assume you found the ServiceMetadataBehavior... - you set new values 
smb.HttpGetEnabled = true; 
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 

// and now you're ADDING the already existing "smb" to the behaviors collection! 
host.Description.Behaviors.Add(smb); 

所以我相信你真正想要的是更多這樣的:

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" 
    ); 
} 

或者,如果你真的想設置一個現有的行爲,您的特定值的值,你必須調用.Behaviors.Add(smb);前再次​​檢查並host.AddServiceEndpoint前看看是否你處理的現有行爲(當時再次加!),或者一個新的

+0

我試過你的代碼,並且它失敗了。我更新了上面的代碼,使其成爲完整的類和它的調用代碼與您的編輯,它仍然失敗:( –