我是否需要在 WCF DataContract 中公开构造函数才能使其在客户端上的对象实例化期间工作?
我在 WCF 服务中有一个类,我们称之为 A。 A 是一个数据协定,其中包含另一个自定义对象 B 的集合作为其数据成员之一。为了避免客户端出现空引用问题,我在构造函数中实例化 BList ,如下所示:
[DataContract]
public class A
{
[DataMember]
public String name { get; set; }
[DataMember]
public List<B> BList {get; set; }
public A()
{
BList = new List<B>();
}
}
我的问题是,在客户端上,此实例化不会发生,并且出现 BList在客户端创建A对象后为null。我猜测构造函数不会出现在客户端上。那么,我需要让构造函数成为显式操作契约吗?如果是这样,那么客户就可以看到他们不应该看到的内部事物,对吧?我如何确保此实例化发生在客户端上?
谢谢,如果这看起来是个愚蠢的问题,我很抱歉。
I have a class in a WCF service, lets call it A. A is a data contract, which contains as one of its DataMembers a collection of another custom object B. To avoid Null Reference problems on the client side, I instantiate the BList in the constructor like so:
[DataContract]
public class A
{
[DataMember]
public String name { get; set; }
[DataMember]
public List<B> BList {get; set; }
public A()
{
BList = new List<B>();
}
}
My problem is that on the client, this instantiation does not happen and BList appears as null after an object of A is created on the client. I'm guessing that the constructor does not appear on the client. So, do I need to make the constructor an explicit operation contract? If so that would make internal things visible to the client that they shouldn't see, right? How do I make sure that this instantiation happens on the client?
Thanks, and sorry if this seems like a dumb question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 [OnDeserializing] 或 [OnDeserialized] 属性来对 DataContract 类型进行初始化。请参阅 http://msdn.microsoft.com/en-us/library/ms733734。 ASPX
You need to use [OnDeserializing] or [OnDeserialized] attributes to do initialization of DataContract types. See http://msdn.microsoft.com/en-us/library/ms733734.aspx
我不确定是否有办法做到这一点,但通过从服务中获取新实例,应该初始化列表,我建议以下
要创建实例:
在服务中编写方法,
I am not sure if there is a way to do this but by getting the new instance from the service the list should be initialized, I suggest the following
To create the instance:
In the service write the method,