返回 DataContract 的子类
我有一个 DataContract,用作 WCF 服务的返回类型。
[DataContract]
public NameResult
{
[DataMember]
public string Name { get; set; }
}
但是,我想在服务端存储附加信息,因此我创建了一个子类:
internal ServiceNameResult : NameResult
{
internal Guid ID { get; set; }
}
但是,我似乎无法使用此实例作为结果值(我在客户端上收到的错误不是很有帮助 - 无法识别)错误 109 (0x6d)。
基本上,如果我这样做了;
NameResult GetName()
{
NameResult result = {...}
return result;
}
那么它就可以了,但如果我这样做了
NameResult GetName()
{
ServiceNameResult result = {...}
return result;
}
;我真的不想将属性从 ServiceNameResult 复制到新的 NameResult。去的路使这个工作?
我已经将 [IgnoreDataMember] 放在子类上,但这没有什么区别,
谢谢。
I have a DataContract which I use as a return type from a WCF service.
[DataContract]
public NameResult
{
[DataMember]
public string Name { get; set; }
}
However, I want to store additional information on the service side, so I create a subclass:
internal ServiceNameResult : NameResult
{
internal Guid ID { get; set; }
}
However, it seems I am unable to use instances of this as a result value (the error I get on the client isn't very helpful - Unrecognized error 109 (0x6d).
Basically, if I do;
NameResult GetName()
{
NameResult result = {...}
return result;
}
Then it works, but if I do;
NameResult GetName()
{
ServiceNameResult result = {...}
return result;
}
It doesn't. I don't really want to have to copy the properties from the ServiceNameResult to a new NameResult. Hopefully there is a way to make this work?
I've already put [IgnoreDataMember] on the subclass, but that makes no difference.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是解决这个问题的一种方法。您可以使用组合来实现您正在寻找的内容:
因此您的内部服务实现可以保存对客户端返回对象的引用以及其他信息,但不会污染您的接口。
Here is one way to approach this problem. You could use composition to achieve what you're looking for:
So your internal service implementation can hold a reference to the client return object as well as additional information, but you don't pollute your interface.
您需要将 ServiceNameResult 定义为已知类型
You'll need to define ServiceNameResult as a known type