WCF:揭露只读DataMember属性没有设置? [英] WCF: Exposing readonly DataMember properties without set?

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

问题描述

我有一个服务器端类,我通过[DataContract]做可在客户端。这个类有一个只读域,我想通过一个属性,以使可用。不过,我不能这样做,因为它似乎并不认为我被允许添加一个[数据成员]属性,而无需get和set。

所以 - 是有没有办法有没有二传手一个[数据成员]属性?

  [DataContract]
类SomeClass的
{
    私人只读INT _id;

    公共SomeClass的(){..}

    [数据成员]
    公众诠释编号{{返回_id; }}

    [数据成员]
    公共字符串SomeString {获得;组; }
}
 

或将解决方案被使用[的DataMember]作为场 - (例如像显示<一href="http://stackoverflow.com/questions/556775/wcf-datamember-attribute-on-property-vs-member/556886#556886">here)?尝试过做这件事,但它似乎并不关心该字段为readonly ..?

修改的:是由黑客像这样使一个只读属性的唯一途径? (没有 - 我不希望这样做...)

  [数据成员]
公众诠释标识
{
    {返回_id; }
    私人集合{/ * NOOP * /}
}
 

解决方案

您服务器端级将不会是提供给客户的,真的。

会发生什么情况是这样的:基于数据契约,客户端会从服务的XML模式创建一个新的单独的类。它的不能使用服务器端类本身!

这将重新创建的XML模式定义一个新的类,但该模式不包含任何像可见性或访问修饰符的.NET具体的事情 - 它只是一个XML模式,毕竟。客户端类将在这样一种方式,它具有在电线上相同的足迹创建 - 例如它系列化到相同的XML格式,基本上是这样。

不可以运输.NET具体通过标准的基于SOAP的服务诀窍关于类 - 毕竟,所有你身边掠过的连载的消息 - !没有课

检查四原则SOA的(由微软的唐盒定义):

  1. 在边界是明确
  2. 服务是自治的
  3. 在服务共享模式和契约,而非类
  4. Compability基于策略

看点#3 - 服务共享模式和契约,不可以类 - 你永远只能分担数据契约接口和XML模式 - 这一切 - 没有.NET类

I have a server side class which I make available on the client side through a [DataContract]. This class has a readonly field which I'd like to make available through a property. However, I'm unable to do so because it doesn't seem that I'm allowed to add a [DataMember] property without having both get and set.

So - is there a way to have a [DataMember] property without setter?

[DataContract]
class SomeClass
{
    private readonly int _id; 

    public SomeClass() { .. }

    [DataMember]
    public int Id { get { return _id; } }        

    [DataMember]
    public string SomeString { get; set; }
}

Or will the solution be use the [DataMember] as the field - (like e.g. shown here)? Tried doing this too, but it doesn't seem to care the field is readonly..?

Edit: Is the only way to make a readonly property by hacking it like this? (no - I don't want to do this...)

[DataMember]
public int Id
{
    get { return _id; }
    private set { /* NOOP */ }
}

解决方案

Your "server-side" class won't be "made available" to the client, really.

What happens is this: based on the data contract, the client will create a new separate class from the XML schema of the service. It cannot use the server-side class per se!

It will re-create a new class from the XML schema definition, but that schema doesn't contain any of the .NET specific things like visibility or access modifiers - it's just a XML schema, after all. The client-side class will be created in such a way that it has the same "footprint" on the wire - e.g. it serializes into the same XML format, basically.

You cannot "transport" .NET specific know-how about the class through a standard SOAP-based service - after all, all you're passing around are serialized messages - no classes!

Check the "Four tenets of SOA" (defined by Don Box of Microsoft):

  1. Boundaries are explicit
  2. Services are autonomous
  3. Services share schema and contract, not class
  4. Compability is based upon policy

See point #3 - services share schema and contract, not class - you only ever share the interface and XML schema for the data contract - that's all - no .NET classes.

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

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