【发布时间】:2012-01-27 17:42:08
【问题描述】:
我有一个托管在远程计算机上的 WCF 服务。我能够连接到它并使用 WPF 应用程序完美地使用该服务。我还希望能够从不同机器上的不同 Windows 服务连接到 WCF 服务。我将 app.config 设置为与 WPF 应用程序相同,但 Windows 服务不会连接到 WCF 服务。这是错误:
套接字连接被中止。这可能是由错误引起的 处理您的消息或接收超时被超过 远程主机或底层网络资源问题。
这是来自 Windows 服务的 app.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ServiceIP" value="127.0.0.1"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib" />
</assemblyBinding>
</runtime>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBindingEndpoint" closeTimeout="00:01:00"
openTimeout="00:00:10" receiveTimeout="00:10:00" sendTimeout="00:00:15"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8731/WCFService/" binding="netTcpBinding"
bindingConfiguration="NetTcpBindingEndpoint" contract="WCFService.IWCFService"
name="NetTcpBindingEndpoint">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
知道为什么我可以与 WPF 应用程序正常连接,但不能通过 Windows 服务连接吗?
编辑:
这是我在代码中创建连接的方式:
EndpointAddress ep = new EndpointAddress("net.tcp://" + remoteIP + ":8731/WCFService/");
InstanceContext context = new InstanceContext(this);
wcfObj = new WCFService.WCFServiceClient(context, "NetTcpBindingEndpoint", ep);
wcfObj.Subscribe();
编辑 2:
感谢 Huusom,我能够通过更改运行服务的帐户来使其工作。问题是我不想让安装服务的用户去弄乱服务帐户。除了这个我还能做些什么来让它工作吗?我可以禁用某种安全性吗?
【问题讨论】:
-
您是否尝试在代码中设置连接以检查是否是实际连接问题而不是.config的加载?
-
这可能是很多事情。当没有提供 DataContract 的所有字段并且这些字段没有用 EmitDefaultValue = false 标记时,我最常遇到此错误。如果您跟踪您的 WCF 通信,如下所示:msdn.microsoft.com/en-us/library/ms733025.aspx,您可以跟踪它。
-
Windows 服务默认没有可执行文件位置的当前工作目录。这可能会对配置文件造成严重破坏。
-
您是否在运行 wcf 服务的机器上运行 wpf 应用程序?看起来您正在尝试连接到远程系统,但您的端点地址设置为 localhost。端点地址应该是远程系统的地址,而不是 localhost。我猜它可以在 wpf 应用程序中运行,因为它与服务运行在同一个盒子上......
-
尝试在自己的账号下运行windows服务。可能是windows服务的账号没有权限在windows中打开ip端口。
标签: c# .net wcf windows-services