我正在使用 4.5 .Net Framework 上使用 C# 构建的两个应用程序
- WPF 桌面应用程序
- 视窗服务
并希望他们使用 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的 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();
}