2012-01-27 256 views
0

我有一个WCF服务托管在远程计算机上。我能够连接到它并使用WPF应用程序完美地使用服务。我也希望能够从不同机器上的不同Windows服务连接到WCF服务。我将app.config设置为与WPF应用程序相同,但Windows服务将不会连接到WCF服务。以下是错误:从Windows服务连接到WCF服务

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.

下面是从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我能得到它通过更改运行该服务的帐户进行工作。问题是我不想问安装服务的用户去搞乱服务帐户。除此之外,我还有什么可以做的吗?我可以禁用某种安全性吗?

+2

您是否尝试在代码中设置连接以检查它是否是实际连接问题而不是加载.config? – ChrFin 2012-01-27 17:44:57

+0

这可能是很多事情。当DataContract的所有字段都未提供且这些字段未使用EmitDefaultValue = false标记时,我遇到过这个错误最多。如果你跟踪你的WCF通信,如下所示:http://msdn.microsoft.com/en-us/library/ms733025.aspx,你可以追踪它。 – Josh 2012-01-27 17:48:04

+1

默认情况下,Windows服务没有可执行文件位置的当前工作目录。这可能会导致配置文件混乱。 – 2012-01-27 17:49:18

回答

0

要继续从问题的意见。 您需要在ip端口上设置命名空间预留:msdn

+0

我不确定该文章是否相关。我没有使用HTTP或HTTPS,而是使用net.tcp – Brian 2012-01-27 21:32:22