2016-07-28 168 views
0

我使用的是4.5 .Net框架中使用C#內置兩個應用無法在Windows服務在Windows主機和啓動WCF服務與netNamedPipeBinding 7

  1. WPF桌面應用程序
  2. Windows服務

,並希望他們互相交談使用IPC(進程間通信)的辦法,因爲它們需要經常共享基於條件的一些數據/對象的狀態。

WCF與netNamedPipeBinding似乎非常靈活的解決方案。我創建了一個WCF服務器和客戶端&在控制檯應用程序中成功託管。

由於該解決方案的工作,我想WCF服務器在Windows服務託管(這是我最後的要求)。

我就成功應用(估計是因爲我沒有看到任何明顯的錯誤),但我不能任何客戶端連接到它。我用WcfTestClient.exe工具(Visual Studio中默認工具),以及從控制檯應用程序試圖連接,他們都不工作,因爲我不斷收到錯誤 - 無法從net.pipe獲得元數據://本地主機/ TestWCFService/mex。下面添加詳情。

我註冊了具有管理員權限的Windows服務,並使用同一用戶運行控制檯應用程序測試客戶端& WcfTestClient.exe。

自然,我想的東西,欣賞到固定它你的幫助。

下面是代碼,我使用Windows服務來託管WCF服務netNamedPipeBinding:

protected override void OnStart(string[] args) 
    { 
     try 
     { 
      if (wcfServiceHostObj != null) 
       wcfServiceHostObj.Close(); 

      wcfServiceHostObj = new ServiceHost(typeof(TestWCFService)); 
      wcfServiceHostObj.Open(); 
      EventLog.WriteEntry(ServiceName, "WCF Host - Started Successfully ", EventLogEntryType.Information); 

     } 
     catch (Exception ex) 
     { 
      EventLog.WriteEntry(ServiceName, "exception raised " + ex.InnerException, EventLogEntryType.Error); 
      throw; 
     } 

    } 

錯誤:無法從net.pipe獲得元數據://本地主機/ TestWCFService/MEX如果這是您可以訪問的Windows(R)Communication Foundation服務,請檢查您是否已在指定地址啓用了元數據發佈。有關啓用元數據發佈的幫助,請參閱http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata上的MSDN文檔Exchange錯誤URI:net.pipe:// localhost/TestWCFService/mex元數據包含無法解析的引用:'net.pipe:// localhost/TestWCFService/mex 」。 net.pipe:// localhost/TestWCFService/mex中沒有可以接受消息的端點。這通常是由不正確的地址或SOAP操作引起的。有關更多詳細信息,請參閱InnerException(如果存在)。在本地計算機上找不到管道端點「net.pipe:// localhost/TestWCFService/mex」。


這裏我的WCF服務器配置設置:

<system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="TestWCFServiceNetPipeBehavior"> 
        <serviceDebug includeExceptionDetailInFaults="true" /> 
        <serviceMetadata /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 

     <services> 
      <service behaviorConfiguration="TestWCFServiceNetPipeBehavior" 
       name="WCFHostTest.WCFService.TestWCFService"> 

       <endpoint address="net.pipe://localhost/TestWCFService" binding="netNamedPipeBinding" bindingConfiguration="" 
        name="TestWCFServiceNetPipeEndPoint" contract="WCFHostTest.WCFService.ITestWCFService" > 

       </endpoint> 

       <endpoint address="net.pipe://localhost/TestWCFService/mex" binding="mexNamedPipeBinding" bindingConfiguration="" 
        name="TestWCFServiceMexPipeEndpoint" contract="IMetadataExchange" /> 
       <host> 
        <!--<baseAddresses> 
         <add baseAddress="net.pipe://localhost/TestWCFService" /> 
        </baseAddresses>--> 
       </host> 
      </service> 
     </services> 
    </system.serviceModel> 

對於客戶端(這是在控制檯應用程序),我使用的是內聯。 這裏的代碼

private static void testClient() 
    { 
     try 
     { 
      string address = "net.pipe://localhost/TestWCFService"; 
      NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); 
      EndpointAddress ep = new EndpointAddress(address); 

      ITestWCFService channel = ChannelFactory<ITestWCFService>.CreateChannel(binding, ep); 

      using (channel as IDisposable) 
      { 
       channel.SendMessage("First Message"); 
      } 

      Console.ReadLine(); 

     } 
     catch (Exception ex) 
     { 

      Console.Write(ex.InnerException); 
      Console.ReadLine(); 

     } 
+0

您是否在事件日誌中看到任何條目?當您嘗試連接到託管的WCF服務時,Windows服務是否正在運行?發佈您的客戶端和Windows服務配置文件。 – Tim

+0

此外,您並未在服務的「OnStart」方法中添加任何端點或指定任何端點配置。 – Tim

+0

在事件日誌中,我看到「WCF主機 - 成功啓動」消息,這意味着,啓動WCF服務器時沒有問題(至少代碼是這樣)。 – manvendra

回答

0

您無法連接最可能的原因是因爲沒有傳遞到ServiceHost構造沒有服務地址。

OnStart方法在你的Windows服務,試試這個:

wcfServiceHostObj = new ServiceHost(typeof(TestWCFService), 
    new Uri[] { "net.pipe://localhost/TestWCFService" }); 

眼看「WCF主機 - 已成功啓動」僅僅意味着ServiceHost沒有拋出一個錯誤;它不保證WCF服務正在偵聽並準備好接收消息。

此外,使用using語句這樣的WCF客戶端:

using (channel as IDisposable) 
{ 
    channel.SendMessage("First Message"); 
} 

被認爲是bad practice

+0

,感謝Tim的輸入。 – manvendra

+0

我已經提出了建議的更改,但得到相同的錯誤。 – manvendra