2013-06-12 43 views
4

我試圖通過NetNamedPipeBinding公開一個接口。將mex端點添加到NetNamedPipeBinding

這是我做的:

 try 
     { 
      //load the shedluer static constructor 
      ServiceHost svh = new ServiceHost(typeof(MyClass)); 

      var netNamedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); 

      var netNamedPipeLocation = "net.pipe://localhost/myservice/"; 
      svh.AddServiceEndpoint(typeof(IMyInterface), netNamedPipeBinding, netNamedPipeLocation); 

      // Check to see if the service host already has a ServiceMetadataBehavior 
      ServiceMetadataBehavior smb = svh.Description.Behaviors.Find<ServiceMetadataBehavior>(); 
      // If not, add one 
      if (smb == null) 
       smb = new ServiceMetadataBehavior(); 

      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
      svh.Description.Behaviors.Add(smb); 

      // Add MEX endpoint 
      svh.AddServiceEndpoint(
       ServiceMetadataBehavior.MexContractName, 
       MetadataExchangeBindings.CreateMexNamedPipeBinding(), 
       netNamedPipeLocation + "/mex" 
       ); 

      svh.Open(); 

      Console.WriteLine("Service mounted at {0}", netNamedPipeLocation); 
      Console.WriteLine("Press ctrl+c to exit"); 

      ManualResetEvent me=new ManualResetEvent(false); 
      me.WaitOne(); 
      svh.Close(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Exception"); 
      Console.WriteLine(e); 
     } 

服務啓動OK,但是當我創建一個新的Visual Studio項目,並嘗試添加在位置net.pipe://localhost/myservice/服務的參考,我得到以下錯誤:

The URI prefix is not recognized. 
Metadata contains a reference that cannot be resolved: 'net.pipe://localhost/myservice/'. 
Metadata contains a reference that cannot be resolved: 'net.pipe://localhost/myservice/'. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

如果我將NetNamedPipeBinding替換爲TcpBinding,則代碼可以正常工作,並且可以添加服務引用。

我應該改變什麼,所以我可以在Visual Studio中添加服務引用?

回答

2

With var netNamedPipeLocation = "net.pipe://localhost/myservice/";netNamedPipeLocation + "/mex"結尾爲net.pipe://localhost/myservice//mex。您的通話AddServiceEndPoint

svh.AddServiceEndpoint(
      ServiceMetadataBehavior.MexContractName, 
      MetadataExchangeBindings.CreateMexNamedPipeBinding(), 
      netNamedPipeLocation + "mex" 
      ); 

去除多餘之後/我是能夠連接到本地命名管道服務中使用你的代碼沒有問題主持。

+0

哇。我怎麼可能錯過了? )) –