WCF DataContract与只读属性 [英] WCF DataContract with readonly properties

查看:471
本文介绍了WCF DataContract与只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从WCF服务方法返回一个复杂类型。我使用C#和.NET 4。这个复杂类型的意思是不变的(以同样的方式。NET字符串)。此外,该服务仅返回它,但是没有收到它作为一个参数。

I'm trying to return a complex type from a service method in WCF. I'm using C# and .NET 4. This complex type is meant to be invariant (the same way .net strings are). Furthermore, the service only returns it and never receives it as an argument.

如果我尝试在属性定义只干将我得到一个运行时错误。我想这是因为没有制定者会导致序列化失败。不过,我认为这种类型应该是不变的。

If I try to define only getters on properties I get a run time error. I guess this is because no setters causes serialization to fail. Still, I think this type should be invariant.

例如:

[DataContract]
class A 
{
   [DataMember]
   int ReadOnlyProperty {get; private set;}
}

该服务无法加载由于与序列化的问题。

The service fails to load due to a problem with serialization.

有没有办法对一个WCF DataContract使只读属性?也许通过更换串行?如果是这样,怎么样?如果没有,你会建议对这个问题的?

谢谢,
阿萨夫

Thanks,
Asaf

推荐答案

数据成员字段不能为只读,因为WCF不序列化对象,是与befor反序列化,每次启动时创建新​​的对象实例,使用默认的构造函数。调度员使用制定者反序列化后,设置字段值。

DataMember Field can't be readonly, because wcf dont serialize object as-is and every time befor deserialization starts creates new object instance, using default constructor. Dispatchers use setters to set field values after deserialization.

但是,所有上面的文本可以是一个很大的错误:)

But all upper text can be a big mistake :)

为了让他们真的只读,使服务器逻辑,验证thiss字段值。

To make them realy readonly, make the server logic, validating thiss field values.

这篇关于WCF DataContract与只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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