C# WCF服务器端崩溃通信对象故障异常:
WCF服务(服务器)在一段时间内运行正常,但由于异常而意外崩溃,此异常记录在AppDomain.CurrentDomain.UnhandledException中:C# WCF服务器端崩溃通信对象故障异常:,c#,wcf,exception,client-server,C#,Wcf,Exception,Client Server,WCF服务(服务器)在一段时间内运行正常,但由于异常而意外崩溃,此异常记录在AppDomain.CurrentDomain.UnhandledException中: System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.SecurityChannelListener`1+SecurityReplySessionChannel[S
System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.SecurityChannelListener`1+SecurityReplySessionChannel[System.ServiceModel.Channels.IReplySessionChannel], cannot be used for communication because it is in the Faulted state.
at System.ServiceModel.Channels.CommunicationObject.ThrowIfFaulted()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.StartInnerReceive()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.OnFaultSent()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.OnInnerReceiveDone()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.StartInnerReceive()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.Start()
at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveRequestAndVerifySecurityAsyncResult.ReceiveMessage(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
没有xml配置,一切都是在运行时配置的。
服务上下文是单个的,并发是多个的
我们已禁用重播检测。我们的用户在他们的电脑上设置了错误的日期时间,所以我们被迫“禁用”时间偏移。
Tcp绑定用于通信,我们使用回调
使用自定义错误处理程序时,HandleError始终返回false
当前解决方案:服务设置为在崩溃后自动重启,但这远远不够令人满意
配置(常量变量替换为值):
Uri tcpBaseAddress = new Uri(String.Format("net.tcp://localhost:{0}", MyMwcNetworkingConstants.NETWORKING_PORT_MASTER_SERVER_NEW));
// create the net.tcp binding for the service endpoint
NetTcpBinding ntcBinding = new NetTcpBinding();
ntcBinding.Security.Mode = SecurityMode.None;
ntcBinding.MaxBufferPoolSize = 1024*1024;
ntcBinding.MaxBufferSize = 10*1024;
ntcBinding.MaxConnections = 500;
ntcBinding.ListenBacklog = 500;
ntcBinding.MaxReceivedMessageSize = 10*1024;
ntcBinding.ReaderQuotas.MaxArrayLength = 10*1024;
ntcBinding.ReaderQuotas.MaxBytesPerRead = 10*1024;
ntcBinding.SendTimeout = 90s;
ntcBinding.ReceiveTimeout = 90s;
ntcBinding.Security.Mode = SecurityMode.Message;
ntcBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
ntcBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
ntcBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None;
m_host = new System.ServiceModel.ServiceHost(Service, tcpBaseAddress);
m_host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyUserValidator();
m_host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
m_host.Credentials.ServiceCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(MyMasterConstants.MASTER_CERTIFICATE, string.Empty);
m_host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new MyCertificateValidator(String.Empty);
m_host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
var endpoint = m_host.AddServiceEndpoint(typeof(IMyMasterService), MyCustomBinding.DecorateBinding(ntcBinding, MyMasterConstants.WCF_MAX_CLIENT_COUNT), tcpBaseAddress);
m_host.Open();
public static class MyCustomBinding
{
public static Binding DecorateBinding(Binding binding, int? maxNegotiationCount)
{
CustomBinding customBinding = new CustomBinding(binding);
SymmetricSecurityBindingElement security = customBinding.Elements.Find<SymmetricSecurityBindingElement>();
if (security != null)
{
security.IncludeTimestamp = false;
security.LocalClientSettings.DetectReplays = false;
security.LocalServiceSettings.DetectReplays = false;
security.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
security.LocalServiceSettings.MaxClockSkew = TimeSpan.MaxValue;
security.LocalClientSettings.SessionKeyRenewalInterval = TimeSpan.MaxValue;
security.LocalServiceSettings.SessionKeyRenewalInterval = TimeSpan.FromMilliseconds(Int32.MaxValue);
if (maxNegotiationCount.HasValue)
{
security.LocalServiceSettings.MaxPendingSessions = maxNegotiationCount.Value;
security.LocalServiceSettings.MaxStatefulNegotiations = maxNegotiationCount.Value;
}
// Get the System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters
SecureConversationSecurityTokenParameters secureTokenParams = (SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;
// From the collection, get the bootstrap element.
SecurityBindingElement bootstrap = secureTokenParams.BootstrapSecurityBindingElement;
// Set the MaxClockSkew on the bootstrap element.
bootstrap.IncludeTimestamp = false;
bootstrap.LocalClientSettings.DetectReplays = false;
bootstrap.LocalServiceSettings.DetectReplays = false;
bootstrap.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.MaxClockSkew = TimeSpan.MaxValue;
bootstrap.LocalClientSettings.SessionKeyRenewalInterval = TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.SessionKeyRenewalInterval = TimeSpan.FromMilliseconds(Int32.MaxValue);
if (maxNegotiationCount.HasValue)
{
bootstrap.LocalServiceSettings.MaxPendingSessions = maxNegotiationCount.Value;
bootstrap.LocalServiceSettings.MaxStatefulNegotiations = maxNegotiationCount.Value;
}
return customBinding;
}
else
{
return binding;
}
}
public static Binding DecorateBinding(Binding binding)
{
return DecorateBinding(binding, null);
}
}
这是我的定制绑定:
Uri tcpBaseAddress = new Uri(String.Format("net.tcp://localhost:{0}", MyMwcNetworkingConstants.NETWORKING_PORT_MASTER_SERVER_NEW));
// create the net.tcp binding for the service endpoint
NetTcpBinding ntcBinding = new NetTcpBinding();
ntcBinding.Security.Mode = SecurityMode.None;
ntcBinding.MaxBufferPoolSize = 1024*1024;
ntcBinding.MaxBufferSize = 10*1024;
ntcBinding.MaxConnections = 500;
ntcBinding.ListenBacklog = 500;
ntcBinding.MaxReceivedMessageSize = 10*1024;
ntcBinding.ReaderQuotas.MaxArrayLength = 10*1024;
ntcBinding.ReaderQuotas.MaxBytesPerRead = 10*1024;
ntcBinding.SendTimeout = 90s;
ntcBinding.ReceiveTimeout = 90s;
ntcBinding.Security.Mode = SecurityMode.Message;
ntcBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
ntcBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
ntcBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None;
m_host = new System.ServiceModel.ServiceHost(Service, tcpBaseAddress);
m_host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyUserValidator();
m_host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
m_host.Credentials.ServiceCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(MyMasterConstants.MASTER_CERTIFICATE, string.Empty);
m_host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new MyCertificateValidator(String.Empty);
m_host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
var endpoint = m_host.AddServiceEndpoint(typeof(IMyMasterService), MyCustomBinding.DecorateBinding(ntcBinding, MyMasterConstants.WCF_MAX_CLIENT_COUNT), tcpBaseAddress);
m_host.Open();
public static class MyCustomBinding
{
public static Binding DecorateBinding(Binding binding, int? maxNegotiationCount)
{
CustomBinding customBinding = new CustomBinding(binding);
SymmetricSecurityBindingElement security = customBinding.Elements.Find<SymmetricSecurityBindingElement>();
if (security != null)
{
security.IncludeTimestamp = false;
security.LocalClientSettings.DetectReplays = false;
security.LocalServiceSettings.DetectReplays = false;
security.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
security.LocalServiceSettings.MaxClockSkew = TimeSpan.MaxValue;
security.LocalClientSettings.SessionKeyRenewalInterval = TimeSpan.MaxValue;
security.LocalServiceSettings.SessionKeyRenewalInterval = TimeSpan.FromMilliseconds(Int32.MaxValue);
if (maxNegotiationCount.HasValue)
{
security.LocalServiceSettings.MaxPendingSessions = maxNegotiationCount.Value;
security.LocalServiceSettings.MaxStatefulNegotiations = maxNegotiationCount.Value;
}
// Get the System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters
SecureConversationSecurityTokenParameters secureTokenParams = (SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;
// From the collection, get the bootstrap element.
SecurityBindingElement bootstrap = secureTokenParams.BootstrapSecurityBindingElement;
// Set the MaxClockSkew on the bootstrap element.
bootstrap.IncludeTimestamp = false;
bootstrap.LocalClientSettings.DetectReplays = false;
bootstrap.LocalServiceSettings.DetectReplays = false;
bootstrap.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.MaxClockSkew = TimeSpan.MaxValue;
bootstrap.LocalClientSettings.SessionKeyRenewalInterval = TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.SessionKeyRenewalInterval = TimeSpan.FromMilliseconds(Int32.MaxValue);
if (maxNegotiationCount.HasValue)
{
bootstrap.LocalServiceSettings.MaxPendingSessions = maxNegotiationCount.Value;
bootstrap.LocalServiceSettings.MaxStatefulNegotiations = maxNegotiationCount.Value;
}
return customBinding;
}
else
{
return binding;
}
}
public static Binding DecorateBinding(Binding binding)
{
return DecorateBinding(binding, null);
}
}
公共静态类MyCustomBinding
{
公共静态绑定装饰绑定(绑定绑定,int?maxNegotiationCount)
{
CustomBinding CustomBinding=新的CustomBinding(绑定);
SymmetricSecurityBindingElement security=customBinding.Elements.Find();
if(安全性!=null)
{
security.IncludeTimestamp=false;
security.LocalClientSettings.DetectReplays=false;
security.LocalServiceSettings.DetectReplays=false;
security.LocalClientSettings.MaxClockSkew=TimeSpan.MaxValue;
security.LocalServiceSettings.MaxClockSkew=TimeSpan.MaxValue;
security.LocalClientSettings.SessionKeyRenewalInterval=TimeSpan.MaxValue;
security.LocalServiceSettings.sessionKeyRenewallinterval=TimeSpan.fromMillicles(Int32.MaxValue);
if(maxNegotiationCount.HasValue)
{
security.LocalServiceSettings.MaxPendingSessions=maxNegotiationCount.Value;
security.LocalServiceSettings.MaxStatefulNegotiations=maxNegotiationCount.Value;
}
//获取System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters
SecureConversationSecurityTokenParameters SecureTokeParams=(SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;
//从集合中获取引导元素。
SecurityBindingElement引导=secureTokenParams.BootstrapSecurityBindingElement;
//在引导元素上设置MaxClockSkew。
bootstrap.IncludeTimestamp=false;
bootstrap.LocalClientSettings.DetectReplays=false;
bootstrap.LocalServiceSettings.DetectReplays=false;
bootstrap.LocalClientSettings.MaxClockSkew=TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.MaxClockSkew=TimeSpan.MaxValue;
bootstrap.LocalClientSettings.SessionKeyRenewalInterval=TimeSpan.MaxValue;
bootstrap.LocalServiceSettings.SessionKeyRenewalInterval=TimeSpan.fromMillicles(Int32.MaxValue);
if(maxNegotiationCount.HasValue)
{
bootstrap.LocalServiceSettings.MaxPendingSessions=maxNegotiationCount.Value;
bootstrap.LocalServiceSettings.MaxStatefulNegotiations=maxNegotiationCount.Value;
}
返回自定义绑定;
}
其他的
{
返回绑定;
}
}
公共静态绑定装饰绑定(绑定绑定)
{
返回decorebinding(binding,null);
}
}
我的处境与您完全相同(WCF服务在生产中随机崩溃,无法在开发中复制,堆栈跟踪与您的完全相同)。我已找到以下知识库,并将很快尝试:。“此修补程序尚未完全测试”阻止我现在应用它
如果您尝试了,请告诉我,它解决了您的问题。希望这有帮助。故障状态可能意味着很多事情(不可能解析配置等)。将绑定更改为basicHttpBinding-更易于调试。配置WCF跟踪以获取更详细的erro消息。就像Peter所说的,故障状态可能意味着很多事情。@PeterLarsen'CPH':我无法在DEV上复制这个问题,它只能在生产中复制。我也不能在生产中改变装订。我将尝试在DEV上复制,但复制需要大量不同的请求和相当长的时间。它每天在生产中崩溃一次。你需要更多关于错误的信息:-就像Strukov说的!