WCF 在没有“设置"的属性上窒息.任何解决方法? [英] WCF chokes on properties with no "set ". Any workaround?

查看:24
本文介绍了WCF 在没有“设置"的属性上窒息.任何解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些作为服务方法的结果传递的类,并且该类有一个 get-only 属性:

I have some class that I'm passing as a result of a service method, and that class has a get-only property:

[DataContract]
public class ErrorBase
{
  [DataMember]
  public virtual string Message { get { return ""; } }
}

我在服务端遇到异常:

System.Runtime.Serialization.InvalidDataContractException:未设置'MyNamespace.ErrorBase' 类型中属性 'Message' 的方法.

System.Runtime.Serialization.InvalidDataContractException: No set method for property 'Message' in type 'MyNamespace.ErrorBase'.

我必须将此属性作为唯一的getter,我不能允许用户为其分配值.我可以使用任何解决方法吗?还是我缺少一些额外的属性?

I have to have this property as only getter, I can't allow users to assign it a value. Any workaround I could use? Or am I missing some additional attribute?

推荐答案

给 Message 一个公共 getter 但受保护的 setter,这样只有子类(和 DataContractSerializer,因为它作弊 :)可以修改值.

Give Message a public getter but protected setter, so that only subclasses (and the DataContractSerializer, because it cheats :) may modify the value.

这篇关于WCF 在没有“设置"的属性上窒息.任何解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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