0

我正在开发一个客户端-服务器项目,其中客户端通过 Internet 部署。所有服务都用 WCF 编写并使用wsHttpBinding. 我需要实现客户端回调功能,并且由于客户端位于防火墙和 NAT 之后,我被告知可以使用netTcpBinding绕过防火墙/NAT 的方法。所以我创建了服务和回调合约。

在客户端,这就是我尝试连接它的方式:

var binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;

AuthorizationToken authorizationToken = 
  new AuthorizationToken(Session.Current.Username, Session.Current.Password, RequiredPermission.User);

using (DuplexChannelFactory<INotificationService> channel =
       new DuplexChannelFactory<INotificationService>(new InstanceContext(this), binding,
       new EndpointAddress("net.tcp://mywebsite.com:808/MyServices/NotificationService.svc")))
{
    channel.Credentials.UserName.UserName = authorizationToken.FormatTokenForTransmission();

    channel.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = 
       System.ServiceModel.Security.X509CertificateValidationMode.None;

    INotificationService proxy = channel.CreateChannel();
    proxy.Subscribe();
}

该代码几乎与连接到wsHttpBinding服务的代码相同,除了绑定类型和ChannelFactory. web.config也看起来像这样:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpConfig" maxReceivedMessageSize="2147483647">
          <security mode="Message" >
            <message clientCredentialType="UserName"/>
          </security>
          <readerQuotas maxArrayLength="2147483647"/>
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="netTcpConfig">
             <security mode="Message" >
                <message clientCredentialType="UserName"/>
             </security>
             <readerQuotas maxArrayLength="2147483647"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="MyProject.Services.NotificationService" behaviorConfiguration="authenticationBehavior">
        <endpoint binding="netTcpBinding" bindingConfiguration="netTcpConfig" 
          contract="MyProject.ServiceContracts.INotificationService" />

        <endpoint address="mex" 
                  binding="mexTcpBinding" 
                  bindingConfiguration=""
                  name="MyServiceMexTcpBidingEndpoint" 
                  contract="IMetadataExchange" />

      </service>

      // other non-net.tcp services go here
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="authenticationBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceCredentials>
            <serviceCertificate findValue="SomeCertificate" storeLocation="LocalMachine"
              storeName="TrustedPeople" x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="MyProject.Authentication.CustomAuthenticator, MyProject.Authentication" />
          </serviceCredentials>
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

当我进行服务调用时,请求确实到达了IIS,但是抛出了一个没有内部异常消息的异常,并且在服务器端,我得到了以下跟踪日志:

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. Local socket timeout was '00:01:00

我知道这是一个非常普遍的错误,我的问题是,您是否发现我的 Web 配置设置有任何问题?任何帮助表示赞赏。

4

1 回答 1

0

可能有多种原因——

1)检查您的 net.tcp 适配器服务是否正在运行(甚至尝试重置一次) 2)检查您的 IIS 服务是否配置为通过 Tcp 绑定 3)检查 tcp 端口是否打开 4)最后尝试增加超时时间

于 2013-11-11T18:41:41.077 回答