0

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配置,一切都是在运行时配置的。服务上下文是单一的,并发是多重的。

我们已禁用重放检测。我们的用户在他们的 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);
    }
}
4

1 回答 1

1

我的情况与您完全相同(WCF 服务在生产中随机崩溃,无法在开发中复制,堆栈跟踪与您的相同)。我找到了以下 KB,很快就会尝试一下:http: //support.microsoft.com/kb/2600907/en-us。“此修补程序尚未完全测试”阻止我立即应用它。

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

于 2013-04-24T11:45:58.353 回答