代码之家  ›  专栏  ›  技术社区  ›  Paul Rowland

如何解决此错误,“尝试反序列化参数时出错”

  •  23
  • Paul Rowland  · 技术社区  · 15 年前

    我有一个web服务,它在一个环境中运行良好,但在另一个环境中运行不好。

    web服务从SharePoint获取文档元数据,它运行在一个我无法调试的服务器上,但通过日志记录,我确认该方法成功地进入和退出。

    错误的原因可能是什么?

    错误消息是,

    The formatter threw an exception while trying to deserialize the message: 
    
    There was an error while trying to deserialize parameter http://CompanyName.com.au/ProjectName:GetDocumentMetaDataResponse. 
    
    The InnerException message was 'Error in line 1 position 388. 'Element' 'CustomFields' from namespace 'http://CompanyName.com.au/ProjectName' is not expected. 
    
    Expecting element 'Id'.'.  Please see InnerException for more details.
    

    内部异常是

    已捕获System.ServiceModel.Dispatcher.NetDispatcherFaultException Message=“格式化程序在尝试反序列化消息时引发异常: http://CompanyName.com.au/ProjectName:GetDocumentMetaDataResponse .

    InnerException消息被删除

    'Error in line 1 position 388. 
    'Element' 'CustomFields' 
    from namespace 'http://CompanyName.com.au/ProjectName' is not expected. 
    Expecting element 'Id'.'.  Please see InnerException for more details."
    
    
    
      Source="mscorlib"
      Action="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault"
      StackTrace:
        Server stack trace: 
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, Boolean isRequest)
           at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
           at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
           at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
           at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
           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 CompanyName.ProjectName.External.Sharepoint.WebServiceProxies.SharepointProjectNameSiteService.ProjectNameSiteSoap.GetDocumentMetaData(GetDocumentMetaDataRequest request)
           at CompanyName.ProjectName.External.Sharepoint.WebServiceProxies.SharepointProjectNameSiteService.ProjectNameSiteSoapClient.CompanyName.ProjectName.External.Sharepoint.WebServiceProxies.SharepointProjectNameSiteService.ProjectNameSiteSoap.GetDocumentMetaData(GetDocumentMetaDataRequest request) in D:\Source\TFSRoot\ProjectName\trunk\CodeBase\External\CompanyName.ProjectName.External.Sharepoint.WebServiceProxies\Service References\SharepointProjectNameSiteService\Reference.cs:line 2141
           at CompanyName.ProjectName.External.Sharepoint.WebServiceProxies.SharepointProjectNameSiteService.ProjectNameSiteSoapClient.GetDocumentMetaData(ListSummaryDto listSummary, FileCriteriaDto criteria, List`1 customFields) in D:\Source\TFSRoot\ProjectName\trunk\CodeBase\External\CompanyName.ProjectName.External.Sharepoint.WebServiceProxies\Service References\SharepointProjectNameSiteService\Reference.cs:line 2150
           at CompanyName.ProjectName.Services.Shared.SharepointAdapter.GetDocumentMetaData(ListSummaryDto listSummary, FileCriteriaDto criteria, List`1 customFields) in D:\Source\TFSRoot\ProjectName\trunk\CodeBase\Services\CompanyName.ProjectName.Services\Shared\SharepointAdapter.cs:line 260
           at CompanyName.ProjectName.Services.Project.ProjectDocumentService.SetSharepointDocumentData(List`1 sourceDocuments) in D:\Source\TFSRoot\ProjectName\trunk\CodeBase\Services\CompanyName.ProjectName.Services\Project\ProjectDocumentService.cs:line 1963
           at CompanyName.ProjectName.Services.Project.ProjectDocumentService.GetProjectConversionDocumentsImplementation(Int32 projectId) in D:\Source\TFSRoot\ProjectName\trunk\CodeBase\Services\CompanyName.ProjectName.Services\Project\ProjectDocumentService.cs:line 3212
      InnerException: System.Runtime.Serialization.SerializationException
           Message="Error in line 1 position 388. 'Element' 'CustomFields' from namespace 'http://CompanyName.com.au/ProjectName' is not expected. Expecting element 'Id'."
           Source="System.Runtime.Serialization"
           StackTrace:
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(XmlReaderDelegator xmlReader, Int32 memberIndex, Int32 requiredIndex, XmlDictionaryString[] memberNames)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.GetMemberIndexWithRequiredMembers(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, Int32 memberIndex, Int32 requiredIndex, ExtensionDataObject extensionData)
                at ReadFileMetaDataDtoFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
                at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadArrayOfFileMetaDataDtoFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
                at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadMetaDataSearchResultsDtoFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
                at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadGetDocumentMetaDataResponseBodyFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
                at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
                at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
                at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
                at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
                at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
           InnerException: 
    
    8 回复  |  直到 15 年前
        1
  •  8
  •   Jeff    15 年前

    您确定您的web服务已正确部署到不工作的环境中吗。看起来类型已过时。

        2
  •  8
  •   Shalom Craimer    13 年前

    这是因为web服务依赖于使用XmlSerializer将wsdl等转换为XML,而XML不支持混合模式属性,如:

    public string strThing { get; private set; }
    

    见: http://msdn.microsoft.com/en-us/library/ms978420.aspx

    更多信息: http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-runtime-serialization-datacontractserializer/

        3
  •  6
  •   Paul Rowland    15 年前

    我对此有一个解决方案,但不确定为什么不同的环境会有不同-尽管两个环境之间的一个很大区别是WSS svc pack 1安装在发生错误的环境中。

    http://silverlight.net/forums/t/22787.aspx ie至“请检查您服务的Xml模式”和“模式中的顺序按字母顺序排序”

    查看生成的wsdl,我注意到对于导致错误的序列化类,该类的属性在wsdl中不可见。

    [Serializable]
    public class FileMetaDataDto
    {
        .
        . a constructor...   etc and several other properties edited for brevity
        . 
    
        public int Id { get; private set; }
        public string Version { get; private set; }
        public List<MetaDataValueDto> CustomFields { get; set; }
    
    }
    

    从setter中删除private并重新部署服务,然后再次查看wsdl时,这些属性现在是可见的,原始错误得到了修复。

    所以更新之前的wsdl是

    - <s:complexType name="ArrayOfFileMetaDataDto">
    - <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="FileMetaDataDto" nillable="true" type="tns:FileMetaDataDto" /> 
      </s:sequence>
      </s:complexType>
    - <s:complexType name="FileMetaDataDto">
    - <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" name="CustomFields" type="tns:ArrayOfMetaDataValueDto" /> 
      </s:sequence>
      </s:complexType>
    

    - <s:complexType name="ArrayOfFileMetaDataDto">
    - <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="FileMetaDataDto" nillable="true" type="tns:FileMetaDataDto" /> 
      </s:sequence>
      </s:complexType>
    - <s:complexType name="FileMetaDataDto">
    - <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="Id" type="s:int" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Title" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="ContentType" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Icon" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="ModifiedBy" type="s:string" /> 
      <s:element minOccurs="1" maxOccurs="1" name="ModifiedDateTime" type="s:dateTime" /> 
      <s:element minOccurs="1" maxOccurs="1" name="FileSizeBytes" type="s:int" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Url" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="RelativeFolderPath" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="DisplayVersion" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Version" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="CustomFields" type="tns:ArrayOfMetaDataValueDto" /> 
      <s:element minOccurs="0" maxOccurs="1" name="CheckoutBy" type="s:string" /> 
      </s:sequence>
      </s:complexType>
    
        4
  •  3
  •   bendewey    15 年前

    你有这个名称空间设置吗?您必须确保此命名空间与消息命名空间匹配。如果您可以使用xml输入和可能的数据对象更新您的问题,这将非常有用。

    [DataContract(Namespace = "http://CompanyName.com.au/ProjectName")]
    public class CustomFields
    {
      // ...
    }
    
        5
  •  1
  •   Ben    11 年前

        6
  •  1
  •   Fatih Çelik    9 年前

    就我而言;我的WCF服务功能正在使用 List<byte> Types 参数,我在客户端得到了这个异常。然后我把它改成了 byte[] Types ,更新了服务参考,问题得到解决。

        7
  •  0
  •   HydTechie    10 年前

    我找到了实际的解决方案…从客户端调用您的服务时出现问题。。检查以下事项。

    1. 确保所有[datacontract]、[datamember]属性放置正确 i、 e.确保WCF无错误

    2. WCF客户端(web.config或任何窗口应用程序配置)确保配置条目正确指向正确的条目。。绑定信息、服务的url等

    然后上面的问题:tempuri问题得到解决。。它与名称空间无关。。虽然你确信你生活在默认状态下,

    希望它能节省你的时间!

        8
  •  0
  •   Pablo Claus Venkata K. C. Tata    10 年前

    在我们的例子中,问题是我们更改了默认值 认名字空间 名称

    Project Configuration screen

    这是项目配置屏幕

    我们最终决定回到原来的名字,问题就解决了。

    问题实际上是根命名空间中的点。有两个点( Name.Child.Child )它不起作用。但是有一个( Name.ChidChild )工作。