WCF服务器端崩溃CommunicationObjectFaultedException:

时间:2013-01-16 13:01:26

标签: c# wcf exception client-server

WCF服务(服务器)运行正常一段时间,但是除了意外崩溃异常外, AppDomain.CurrentDomain.UnhandledException 中记录了此异常:

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配置,所有内容都在运行时配置。 服务上下文是Single,并发是Multiple。

我们已禁用重播检测。我们的用户在PC上设置了错误的日期时间时出现了很多问题,所以我们被迫“禁用”时间偏差。 Net.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);
    }
}

1 个答案:

答案 0 :(得分:1)

我和你的情况完全相同(WCF服务在Production上随机崩溃,无法在Dev中复制,堆栈跟踪与你的相同)。我发现了以下KB,并且很快就会试一试:http://support.microsoft.com/kb/2600907/en-us。 “此修补程序尚未经过全面测试”使我无法立即应用它。

如果您尝试了,请告诉我,它可以解决您的问题。希望这会有所帮助。

相关问题