0

我使用 net.tcp 绑定创建了一个 WCF 服务,并且能够在我的控制台应用程序中添加对它的服务引用而没有错误。

完成此操作后,我尝试在我的控制台应用程序中自行托管此服务。服务主机被打开,但是当我使用服务引用对象调用服务中的方法时,我得到了这个异常

'无法连接到 net.tcp://localhost/Design_Time_Addresses/MyWCFServiceLibrary/Service2/。连接尝试持续了 00:00:02.0800000 的时间跨度。TCP 错误代码 10061:无法建立连接,因为目标机器主动拒绝它 [::1]:808。

这是我的cs文件代码

Main method()

 Service2Client sc2 = new Service2Client();
                Uri baseaddress = new Uri("net.tcp://localhost:8732/Design_Time_Addresses/MyWCFServiceLibrary/Service2/");

                ServiceHost serviceHost = new ServiceHost(typeof(Service2Client), baseaddress);

                serviceHost.Open();

                Console.WriteLine(sc2.GetData(124)); --> exception comes at this point

                Console.ReadLine();

这是控制台应用程序中的 app.config

<system.serviceModel>
    <client>
      <endpoint address="net.tcp://localhost/Design_Time_Addresses/MyWCFServiceLibrary/Service2/"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService2"
        contract="ServiceReference2.IService2" name="NetTcpBinding_IService2">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IService2" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          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="None">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <!--<client>
      <endpoint address="net.tcp://localhost:8732/Design_Time_Addresses/MyWCFServiceLibrary/Service2/"
        binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="MyWCFServiceLibrary.IService2"
        name="endpointClient" />
      <endpoint address="net.tcp://localhost/Design_Time_Addresses/MyWCFServiceLibrary/Service2/"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService2"
        contract="ServiceReference2.IService2" name="NetTcpBinding_IService2">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>-->
  </system.serviceModel>

环境:VS2010

任何人都可以帮我解决这个问题。

4

3 回答 3

2

有可能:

1)防火墙阻止了请求。一探究竟。

2) 确保

  • Net.Tcp 侦听器适配器
  • Net.Tcp 端口共享服务

正在运行(Windows 服务)

于 2012-05-18T14:08:03.797 回答
2

您的客户端配置的 URL 为

net.tcp://localhost/Design_Time_Addresses/MyWCFServiceLibrary/Service2/

当您的服务开启时

net.tcp://localhost:8732/Design_Time_Addresses/MyWCFServiceLibrary/Service2/

控制台应用程序客户端端点元素的 app.config 中缺少端口号。

于 2012-05-18T14:52:59.897 回答
0

可能有多种原因,因为其他答案可以解决问题。但就我而言,安装挂起的 Windows 更新后问题解决了。

于 2021-06-15T14:21:23.190 回答