从 WCF 服务返回自定义类时出现 SerializationException [英] SerializationException when returning custom classes from a WCF service

查看:35
本文介绍了从 WCF 服务返回自定义类时出现 SerializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程...

public abstract class Fallible<T> {
}

public class Success<T> : Fallible<T> {
  public Success(T value) {
    Value = value;
  }

  public T Value { get; private set; }
}

可以在 我之前的问题,但你不需要阅读那篇文章,因为上面的类就是看到问题所需要的全部内容.

The background to this can be found in a previous question of mine, but you don't need to read that post as the classes above are all that's needed to see the problem.

如果我有一个像这样简化的 WCF 服务调用...

If I have a simplified WCF service call like this...

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

...然后当我尝试从使用它的 WPF 应用程序(或 WCF 测试客户端)调用该服务时,我收到一个 CommunicationException 异常...

...then when I try to call the service from the WPF app that consumes it (or the WCF test client), I get a CommunicationException exception...

尝试序列化参数 :GetPatientResult 时出错.这InnerException 消息是 'Type 'PhysioDiary.Entities.FallibleClasses.Success`1[[PhysioDiary.Entities.Patient,PhysioDiary.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'数据合同名称 > 'SuccessOfPatient0yGilFAm:http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses'不是预期的.如果您正在使用,请考虑使用 DataContractResolverDataContractSerializer 或将任何静态未知的类型添加到列表中已知类型 - 例如,通过使用KnownTypeAttribute 属性或通过将它们添加到传递给序列化程序的已知类型列表中.'.请有关更多详细信息,请参阅 InnerException.

There was an error while trying to serialize parameter :GetPatientResult. The InnerException message was 'Type 'PhysioDiary.Entities.FallibleClasses.Success`1[[PhysioDiary.Entities.Patient, PhysioDiary.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name > 'SuccessOfPatient0yGilFAm:http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.'. Please see InnerException for more details.

...具有内部 SerializationException 异常...

...with an inner SerializationException exception of...

输入'PhysioDiary.Entities.FallibleClasses.Success`1[[PhysioDiary.Entities.Patient,PhysioDiary.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'数据合同名称 > 'SuccessOfPatient0yGilFAm:http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses'不是预期的.如果您正在使用,请考虑使用 DataContractResolverDataContractSerializer 或将任何静态未知的类型添加到列表中已知类型 - 例如,通过使用 KnownTypeAttribute 属性或者将它们添加到传递给序列化程序的已知类型列表中.

Type 'PhysioDiary.Entities.FallibleClasses.Success`1[[PhysioDiary.Entities.Patient, PhysioDiary.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name > 'SuccessOfPatient0yGilFAm:http://schemas.datacontract.org/2004/07/PhysioDiary.Entities.FallibleClasses' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.

我已经尝试将 [DataContract] 添加到类和 [DataMember] 到每个属性,以及添加 [KnownType]> 涉及的所有四个类的属性,并在服务合同中为每个类添加一个 [ServiceKnownType],但没有任何帮助.

I've tried adding [DataContract] to the class and [DataMember] to each property, as well as adding a [KnownType] attribute for all four classes involved, and adding a [ServiceKnownType] for each of them on the service contract, but nothing helps.

我已经阅读了无数相同问题的答案,但没有找到任何有效的答案.我的服务返回其他自定义类,并且它们都可以毫无问题地序列化.

I've read countless answers to the same question, but have not found anything that works. My services return other custom classes, and they all get serialised without a problem.

谁能解释一下这里的问题是什么?如果我没有提供足够的信息,请告诉我.

Anyone able to explain what the problem is here? Please let me know if I've not supplied enough information.

推荐答案

原来我需要做的就是使用 [ServiceKnownType] 属性为基本类型和每个派生类型装饰服务方法...

Turns out all I needed to do was decorate the service method with [ServiceKnownType] attributes for the base type, and each derived type...

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

尽管每次调用都必须添加四个属性很痛苦,但它确实有效.我想知道是否有办法将它们组合成一个属性,但至少我现在有一个可用的服务.

Although it's a pain to have to add four attributes to every call, it works. I'd like to know if there is a way to combine them into one attribute, but at least I have a working service now.

希望这对某人有所帮助.

Hope this helps someone.

这篇关于从 WCF 服务返回自定义类时出现 SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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