当前位置:编程学习 > C#/ASP.NET >>

奇怪,wcf 好像序列化时候无法获取对象类型

有个类

/// <summary>
  /// 个人银行配置
  /// </summary>
  [DataContract(IsReference = true)]
  [KnownType(typeof(EPS.EntityCollection.Domain.Person))]
  [KnownType(typeof(EPS.EntityCollection.Domain.SimpleDictionaries.Bank))]
  public class PersonBankConfig : Entity<PersonBankConfig, Guid>
  {
    /// <summary>
    /// 人员
    /// </summary>
    [DataMember]
    public virtual Person Person { get; set; }
    /// <summary>
    /// 发卡银行
    /// </summary>
    [DataMember]
    public virtual Bank IssuingBank  { get; set; }
......

已经标记好了,但是在wcf服务器端 发布正常,调用这个类的方法时候报错,错误信息提示如下,有碰到过的吗,一起探讨探讨


There was an error while trying to serialize parameter http://tempuri.org/:GetLogicAllResult. The InnerException message was 'Type 'PersonBankConfigProxy' with data contract name 'PersonBankConfigProxy:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

详细报错信息

<ExceptionString>System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:GetLogicAllResult. The InnerException message was 'Type 'PersonBankConfigProxy' with data contract name 'PersonBankConfigProxy:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details. ---> System.Runtime.Serialization.SerializationException: Type 'PersonBankConfigProxy' with data contract name 'PersonBankConfigProxy:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteArrayOfPersonBankConfigToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.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.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
   --- End of inner exception stack trace ---</ExceptionString>


wcf types --------------------编程问答-------------------- 求给力啊 --------------------编程问答-------------------- 一周了啊 33 --------------------编程问答-------------------- sos    两周了啊 --------------------编程问答-------------------- There was an error while trying to serialize parameter

很明显说明,对象序列化错误,请问,你要序列化什么对象,有些对象可以序列化,有些不行 --------------------编程问答-------------------- PersonBankConfigProxy类型没有暴露出去吧。 --------------------编程问答-------------------- ProxyDataContractResolver
你需要使用这个类 --------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/dd456853.aspx
序列化 POCO 代理


Windows Communication Foundation (WCF) 不能对代理进行直接序列化或反序列化,因为 DataContractSerializer 只能序列化和反序列化已知类型,而代理类型是未知类型。如果需要序列化 POCO 实体,请禁用代理创建或使用 ProxyDataContractResolver 类将代理对象作为原始 POCO 实体进行序列化。若要禁用代理创建,请将 ProxyCreationEnabled 属性设置为 false。 

在序列化过程中,ProxyDataContractResolver 类可将代理类型映射到 POCO 类型。若要指示 DataContractSerializer 在服务操作中使用 ProxyDataContractResolver 类,请定义一个特性类(此类将被应用于服务操作),在内部使用 ProxyDataContractResolver 将代理类型映射到纯 POCO 类型。将此特性类与作为 WCF 应用程序中服务协定的一部分的方法相关联。

客户端将接收实际的 POCO 实体并对其进行反序列化。这些类不具有代理对象的延迟加载和更改跟踪功能。若要跨多个层跟踪这些实体的更改,请使用自跟踪实体。自跟踪实体是实体框架 上不具有依赖项的 POCO 实体,它包含自己的更改跟踪逻辑。有关更多信息,请参见演练:序列化自跟踪实体(实体框架)。

有关更多信息,请参见演练:使用 WCF 序列化 POCO 代理(实体框架)。 --------------------编程问答-------------------- Person 也加上定义吧。 --------------------编程问答-------------------- 你的那个KnownTypeAttribute加错了位置。
你要查找所有的接口(如果没有接口就找服务类)的所有的方法中,输入输出参数中是否要有IList,那个KnownTypeAttribute是专门结合IList(或类似集合)使用的,用来指定IList成员类型的,如果你的是List<T>,则不需要它,因为T指明了集合成员的数据类型,但是IList就不同,所以必须使用KnownTypeAttribute来说明。 --------------------编程问答-------------------- 谢谢大家,我试试看 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,