从Windows服务连接到WCF服务

时间:2012-01-27 17:42:08

标签: c# .net wcf windows-services

我在远程计算机上托管了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,我可以通过更改运行该服务的帐户来使其工作。问题是我不想要求安装该服务的用户弄乱服务帐户。除此之外我还能做些什么才能让它发挥作用?我可以禁用某种安全性吗?

1 个答案:

答案 0 :(得分:0)

继续对该问题的评论。 您需要在ip端口上设置名称空间保留:msdn

相关问题