我发现如果ServiceHost具有需要一段时间处理的UserNamePasswordValidator,WCF客户端可能会崩溃WCF ServiceHost。在我把它扔到微软之前,我想听听有关解决方案的任何建议。
重现的步骤是:
如果UserNamePasswordValidator需要一段时间来处理(例如,2秒睡眠),则只会发生上述情况。如果UserNamePasswordValidator立即返回,则上述情况发生变化:
客户端代码如下:
public static void Main(string[] args)
{
var binding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
var identity = new DnsEndpointIdentity("Dummy");
var endpointAddress = new EndpointAddress(new Uri("net.tcp://localhost:5000/"), identity);
var channelFactory = new ChannelFactory<IService>(binding, endpointAddress);
channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
channelFactory.Credentials.UserName.UserName = "foo";
channelFactory.Credentials.UserName.Password = "bar";
channelFactory.Open();
IService service = channelFactory.CreateChannel();
ThreadPool.QueueUserWorkItem(CallPingOnChannel, service);
Thread.Sleep(TimeSpan.FromSeconds(1));
channelFactory.Close();
}
private static void CallPingOnChannel(object state)
{
var result = ((state) as IService).Ping();
}
ServiceHost的设置如下:
public static void Main(string[] args)
{
var serviceHost = new ServiceHost(typeof(Service), TcpBaseAddress);
serviceHost.Description.Behaviors.Add(new ErrorHandler());
var netTcpBinding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
netTcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new UserValidator();
serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
serviceHost.Credentials.ServiceCertificate.Certificate = GetCertificate();
serviceHost.AddServiceEndpoint(typeof(IService), netTcpBinding, "");
serviceHost.Open();
Console.ReadLine();
}
UserValidator实现如下:
public class UserValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
导致主线程崩溃的未处理异常是:
System.ServiceModel.CommunicationException: 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 '10675199.02:48:05.4775807'. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.ServiceModel.CommunicationException: 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 '10675199.02:48:05.4775807'. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
答案 0 :(得分:3)
回答我自己的问题;我向微软报告了这一点,他们已经确认这是一个框架中的错误,将在即将发布的版本中修复。
答案 1 :(得分:0)
首先,您应该在客户端代码中捕获System.TimeOutException。
接下来,您应该考虑扩展您的客户端和服务的SendTimeout
和RecieveTimeout
。
看看这两个链接。