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

时间:2009-05-15 03:30:22

标签: c# web-services sharepoint

我有一个在一个环境中正常工作的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.

InnerException是

捕获了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 个答案:

答案 0 :(得分:8)

您确定您的网络服务已正确部署到无法正常工作的环境中。看起来类型已过时。

答案 1 :(得分:8)

这种情况正在发生,因为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/

答案 2 :(得分:6)

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

为了解决这个问题,我从这个链接中得到了一个很好的线索 - http://silverlight.net/forums/t/22787.aspx即“请检查服务的Xml架构”和“架构中的序列按字母顺序排序”

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

类的定义具有大多数属性的私有setter,但不适用于CustomFields属性,即..

[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>

更新后的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="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>

答案 3 :(得分:3)

您是否设置了此命名空间?您必须确保此命名空间与消息命名空间匹配。如果您可以使用xml输入以及可能有用的数据对象更新您的问题。

[DataContract(Namespace = "http://CompanyName.com.au/ProjectName")]
public class CustomFields
{
  // ...
}

答案 4 :(得分:1)

确保您要返回的表具有架构。如果没有,则创建一个默认模式(即在该表中添加一列)。

答案 5 :(得分:0)

我找到了实际的解决方案......从客户端调用服务时出现问题。请检查以下内容。

  1. 确保正确放置所有[datacontract],[datamember]属性 即确保WCF没有错误

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

  3. 然后上面的问题:tempuri问题已解决..它与命名空间无关..虽然你确定你的生活是默认的,

    希望它可以节省您的小时数!

答案 6 :(得分:0)

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

Project Configuration screen

这是项目配置屏幕

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

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

答案 7 :(得分:0)

在我的情况下;我的WCF服务函数使用List<byte> Types参数,我在客户端获得此异常。然后我将其更改为byte[] Types,更新了服务参考并解决了问题。