使用派生类型时的反序列化问题-我已经在使用ServiceKnownType [英] Deserialisation problem when using a derived type - and I'm already using ServiceKnownType

查看:77
本文介绍了使用派生类型时的反序列化问题-我已经在使用ServiceKnownType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程...

  public abstract class Fallible<T> {
  }

  public class Success<T> : Fallible<T> {
    public T Value { get; set; }
  }

...和这样的WCF服务调用(简化代码)...

...and a WCF service call like this (simplified code)...

    [OperationContract]
    [ServiceKnownType(typeof(Fallible<Patient>))]
    [ServiceKnownType(typeof(Success<Patient>))]
    public Fallible<Patient> GetPatient(int id) {
      return new Success<Patient> {Value = new Patient {ID = 1, FirstName = "Jim", Surname = "Spriggs"}};
    }

在测试项目中,此方法工作正常,并且使用WCF调用的应用程序(或WCF测试客户端)可以接收Success< Patient>消息.结果.

In a test project, this works fine, and the app that consumes the WCF call (or the WCF test client) can receive the Success<Patient> result.

但是,当我尝试在实际项目中使用它时,尝试对结果进行反序列化时会遇到异常...

Whoever, when I try to use this in a real project, I get an exception when it tries to deserialise the result...

格式化程序尝试反序列化消息时引发异常:尝试反序列化参数:GetPatientResult时发生错误. InnerException消息为'Element':GetPatientResult'包含映射到 名称"http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses:SuccessOfPatient0yGilFAm".解串器不知道任何映射到该名称的类型.如果您正在使用DataContractSerializer,请考虑使用DataContractResolver 或将与"SuccessOfPatient0yGilFAm"相对应的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将其添加到传递给序列化程序的已知类型的列表中.  请参阅InnerException了解更多信息 详细信息.

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :GetPatientResult. The InnerException message was 'Element ':GetPatientResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses:SuccessOfPatient0yGilFAm'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'SuccessOfPatient0yGilFAm' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.'.  Please see InnerException for more details.

从服务调用中可以看到,我在服务调用上使用了ServiceKnownType属性,这是我在测试项目中要做的全部操作.

As you can see from the service call, I use the ServiceKnownType attribute on the service call, which is all I needed to do in the test project.

我尝试将KnownType属性添加到Fallable和Success类,以及添加DataContract/DataMember属性,但这没有帮助.我总是得到这个例外. Patient类是从实体框架模型生成的,但是我 不要以为这没有什么区别,就好像我用一个非常简单的新类尝试相同的事情一样,我也会遇到相同的异常.

I have tried adding the KnownType attribute to the Fallible and Success classes, as well as adding DataContract/DataMember attributes, but it doesn't help. I always get this exception. The Patient class was generated from an Entity Framework model but I don't think it makes any difference, as if I try the same thing with a very simple new class, I get the same exception.

有人知道我做错了什么吗?我已经从测试项目中复制并粘贴了代码,可以正常运行,但是在这里不起作用.

Anyone any idea what I've done wrong? I have copied and pasted the code from my test project, where it works fine, but it doesn't work here.

预先感谢您可以提供的任何帮助.

Thanks in advance for any help you can give.

推荐答案

嗨Yossu,

要检查它是否与真实项目相关,建议您创建一个简单的WCF服务,然后复制Fallable< T> Success< T>和易跌倒的<患者> 与该新项目有关的GetPatient(int id)相关代码.

To check whether it is related with real project, I suggest you create a simple WCF Service, and copy the Fallible<T> Success<T> and Fallible<Patient> GetPatient(int id) related code to this new project.

如果没有发生此问题,建议您向我们分享WCF服务合同的完整代码,以及您可以想到的真实项目与测试项目之间的区别.

最好的问候

爱德华


这篇关于使用派生类型时的反序列化问题-我已经在使用ServiceKnownType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆