奇怪的间歇性WCF错误强制转换IPermissions

本文关键字:错误 转换 IPermissions WCF | 更新日期: 2023-09-27 18:29:03

我目前正在尝试修复WCF服务错误,但我一生都无法找到任何原因的线索。。。

本质上的问题是,它得到了一个InvalidCastException,试图将事物转换为IPrincipal对象(我在错误中看到的XmlDictionaryString&ClassDataContractCriticalHelper)。在服务器端,它甚至不会影响我们的代码,问题是我们只是间歇性地遇到这个问题(每周最多2-3次,每秒多次使用API),考虑到影响服务的流量,如果我们保留完整的消息日志记录,我们将在几分钟内填满生产服务器硬盘。

堆栈跟踪为:

System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlDictionaryString' to type 'System.Security.IPermission'.    
Server stack trace:      at WriteOUROBJECTNAMEToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )     
at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)     
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)    
 at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)     
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)     
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)     
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)     
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)    
 at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)    
 at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)     
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, Object[] parameters)     
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)     
at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)     
at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)     
at System.ServiceModel.Channels.BodyWriter.WriteBodyContents(XmlDictionaryWriter writer)     at System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)     
at System.ServiceModel.Channels.Message.WriteBodyContents(XmlDictionaryWriter writer)     
at System.ServiceModel.Security.SecurityAppliedMessage.WriteBodyToSignThenEncryptWithFragments(Stream stream, Boolean includeComments, String[] inclusivePrefixes, EncryptedData encryptedData, SymmetricAlgorithm algorithm, XmlDictionaryWriter writer)     
at System.ServiceModel.Security.WSSecurityOneDotZeroSendSecurityHeader.ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator)     
at System.ServiceModel.Security.SecurityAppliedMessage.OnWriteMessage(XmlDictionaryWriter writer)     at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)     
at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)     
at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)     
at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message)     at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)     
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)     
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)     
at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)     
at System.ServiceModel.Channels.TransactionRequestChannelGeneric`1.Request(Message message, TimeSpan timeout)     
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)    
 at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)    
 at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)    
 at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)     
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)   
 Exception rethrown at [0]:      
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)     
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)     
at OUR.CLIENT.ANONYMIZED.AddThing(ThingInfo data)
at OUR.CLIENT.ANONYMIZED.DoStuff()

最初,我们启用了消息安全性,考虑到IPrincipal提示,我们切换到TransportWithMessageCredential和HTTPS加密,只是想看看它是否有效,但它没有给我们带来任何好处。配置为:

<serviceHostingEnvironment>
  <baseAddressPrefixFilters>
    <add prefix="http://www.anonymized.com"/>
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>
<bindings>
  <basicHttpBinding>
    <binding name="httpBindingConfig">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="httpBindingBehaviour" name="Anonymized.Service.Implementation">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBindingConfig" name="httpBindingEndpoint" 
              contract="Anonymized.Service.Contract" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/Anonymized" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="httpBindingBehaviour">
      <serviceThrottling maxConcurrentCalls="160" maxConcurrentSessions="100" maxConcurrentInstances="100" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="AnonymizedSqlMembershipProvider" />
      </serviceCredentials>
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="SqlRoleProvider">
      </serviceAuthorization>
    </behavior>
  </serviceBehaviors>
</behaviors>

有人有什么理论吗,甚至有人暗示去哪里看?

奇怪的间歇性WCF错误强制转换IPermissions

这就是DebugDiag最适合解决的问题。您可以配置崩溃规则,以便在发生此异常时仅为其记录崩溃转储。DebugDiag附带了一个崩溃分析脚本,它可能会告诉你足够多的信息,这样你就可以解决你的问题。否则,您可能需要手动调试它以找到根本原因。

相关文章: