我正在开发一个客户端服务器项目,通过互联网部署客户端。所有服务都是用WCF编写的,并使用wsHttpBinding
。我需要实现客户端回调功能,并且由于客户端位于防火墙和NAT之后,我被告知我可以使用绕过防火墙/ NAT的netTcpBinding
。所以我创建了服务和回调合同。
在客户端,我正在尝试连接它:
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配置设置有什么问题吗?任何帮助表示赞赏。
答案 0 :(得分:0)
可能有多种原因 -
1)检查你的net.tcp适配器服务是否正在运行(即使尝试重置一次) 2)检查您的IIS服务是否配置为通过Tcp绑定 3)检查tcp端口是否打开 4)最后尝试增加超时时间