无法在 Windows 7 上的 Windows 服务中使用 netNamedPipeBinding 托管和启动 WCF 服务

Posted

技术标签:

【中文标题】无法在 Windows 7 上的 Windows 服务中使用 netNamedPipeBinding 托管和启动 WCF 服务【英文标题】:Can not Host & Start WCF Service with netNamedPipeBinding in Windows Service on Windows 7 【发布时间】:2016-12-02 21:22:56 【问题描述】:

我正在使用在 4.5 .Net Framework 上使用 C# 构建的两个应用程序

    WPF 桌面应用程序 Windows 服务

并希望他们使用 IPC(进程间通信)方法相互交谈,因为他们需要根据条件经常共享一些数据/对象状态。

带有 netNamedPipeBinding 的 WCF 似乎是一个非常灵活的解决方案。我已经创建了一个 WCF 服务器和客户端并在控制台应用程序中成功测试了托管。

由于此解决方案有效,我希望 WCF 服务器托管在 Windows 服务中(这是我的最终要求)。

我可以成功托管应用程序(猜想是因为我没有看到任何可见的错误),但我无法将任何客户端连接到它。我使用了 WcfTestClient.exe 工具(Visual Studio 默认工具)并尝试从控制台应用程序连接,但它们似乎都不起作用,因为我不断收到错误 - 无法从 net.pipe://localhost/ 获取元数据测试WCFService/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://localhost/TestWCFService/mex 获取元数据 如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI 的 MSDN 文档:net.pipe://localhost/TestWCFService/mex 元数据包含无法解析的引用:'net.pipe://localhost/TestWCFService /墨西哥'。在 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();

        

【问题讨论】:

您在事件日志中看到任何条目吗?当您尝试连接到托管的 WCF 服务时,Windows 服务是否正在运行?同时发布您的客户端和 Windows 服务配置文件。 另外,您没有在服务的 OnStart 方法中添加任何端点或指定任何端点配置。 在事件日志中,我看到“WCF 主机 - 已成功启动”消息,这意味着启动 WCF 服务器没有问题(至少代码认为如此)。 我正在使用配置文件来加载 WCF 服务器的配置设置。 这里是 WCF 服务器配置: 【参考方案1】:

您无法连接的最可能原因是没有将服务地址传递给ServiceHost 构造函数。

在您的 Windows 服务中的 OnStart 方法中尝试此操作:

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

看到“WCF 主机 - 已成功启动”仅表示 ServiceHost 没有抛出错误;它不能保证 WCF 服务正在侦听并准备好接收消息。

另外,像这样对 WCF 客户端使用 using 语句:

using (channel as IDisposable)

    channel.SendMessage("First Message");

被认为是bad practice。

【讨论】:

我已经进行了建议的更改,但得到了同样的错误。 @manvendra,你找到解决方案了吗?我也有同样的问题。 可以使用 netNamedPipeBinding 在 Windows 服务中托管 wcf soap 吗? @aura - 是的,wcf soap 可以通过 netNamedPipeBinding 托管在 Windows 服务中。我的这个解决方案已经在生产环境中运行了一段时间。 @aura - 上述问题,我一直无法找到根本原因。我在各个级别尝试调试了很多天,但始终找不到根本原因。最终,我尝试了一台新机器并且成功了。不知道为什么我之前的机器上没有

以上是关于无法在 Windows 7 上的 Windows 服务中使用 netNamedPipeBinding 托管和启动 WCF 服务的主要内容,如果未能解决你的问题,请参考以下文章

Flashdrive 上的 XAMPP 无法在 Windows 7 上启动 Apache、MySQL

无法在 Windows 7 机器上的 Microsoft Visual C++ 2010 中运行 OpenCV

Windows 7 TLS 1.2 上的 WinInet / IE11 启用无法在网站上运行

Windows 7 上的套接字,无法连接

致命:无法访问“H:\/.config/git/config”:参数无效 [Windows 7 上的 Git]

Windows 7 上的 VB6 应用程序无法访问映射驱动器