Wcf - 隐藏属性?
我有一个看起来有点像这样的类....
[DataContract]
public partial class Area : ModelBase
{
private String name;
private Guid floorId;
private Guid areaTypeId;
private int assetCount;
[DataMember]
public String Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public Guid FloorId
{
get { return floorId; }
set { floorId = value; }
}
public Guid AreaTypeId
{
get { return areaTypeId; }
set { areaTypeId = value; }
}
}
....并且我有一个 Wcf 服务库,它定义了以下接口...
IEnumerable<Area> GetSomeStuff(IEnumerable<Area> uploadedAreas);
一切都工作得很好,但是在我的客户端应用程序中(一个紧凑的框架应用程序)AreaTypeId
属性是否公开?
我想如果我不添加 [DataMember]
属性,客户端就看不到它?什么不懂???
谢谢,
ETFairfax
I've got a class which looks a little like this....
[DataContract]
public partial class Area : ModelBase
{
private String name;
private Guid floorId;
private Guid areaTypeId;
private int assetCount;
[DataMember]
public String Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public Guid FloorId
{
get { return floorId; }
set { floorId = value; }
}
public Guid AreaTypeId
{
get { return areaTypeId; }
set { areaTypeId = value; }
}
}
....and I have a Wcf Service Library which has the following interface defined...
IEnumerable<Area> GetSomeStuff(IEnumerable<Area> uploadedAreas);
It's all working just fine, but in my client app (a compact framework application) the AreaTypeId
property is exposed?
I thought that if I didn't add the [DataMember]
attribute it wouldn't be seen by the client? What am not understanding???
Thanks,
ETFairfax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想对客户端隐藏任何属性,只需向该属性添加 [IgnoreDataMember] 属性即可。
If you want to hide any property from client then just add [IgnoreDataMember] attribute to that property.
如果您在客户端和服务器之间共享类型程序集,除非您关闭重用引用程序集中的类型(单击代理上的“配置服务引用”),否则它将可见。
如果您不共享程序集,它将不可见,因为类的代码是根据公开的合同生成的(您可以通过在 VS 中打开“显示所有文件”,然后转到服务代理下生成的文件 Reference.cs 来查看它)。
DataMember 是 DataContractSerializer 的属性,所以如果您是共享程序集属性不会在服务器上序列化,也不会在客户端上初始化,但它是可见的。 参考
If you are sharing type assembly between client and server It will be visible unless you turn off reusing types from referenced assemblies (click "Configure Service Reference" on proxy).
If you are not sharing assembly it will not be visible because code for class is generated based on exposed contract (you can see it by turning on Show All Files in VS and then go to generated file Reference.cs under service proxy).
DataMember is attribute for DataContractSerializer so if you are sharing assembly the property will be not serialized on server and not initialized on client but it will be visible. Reference