2016-07-28 104 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