WCF 继承/多态性和序列化

发布于 2024-07-18 12:28:11 字数 1115 浏览 7 评论 0原文

我有一个类 ReportDef,它是一个具体类,我根据需要使用 [DataContract][DataMember] 属性进行了修饰。 ReportDef 与我的 ServiceContractIReportService 一起位于程序集 A1 中。 然后,我有另一个类 UiReportDef,它派生自 ReportDef 并位于程序集 A2 中。 UiReportDef 没有服务关心的其他状态。

我想使用 UiReportDef 实例调用我的服务。 有没有什么方法(除了从 UiReportDef 手动构造 ReportDef 实例)可以在我的服务不了解 A2 的情况下执行此操作? 我了解 KnownType。 我不想引用 A2


编辑:这里有一些上下文可能会让我的问题更容易理解。 我的 ServiceContract 实现了 IReportService,它定义了一个方法 RunReport(ReportDef report)ReportDefDataContract 属性修饰,并具有用 DataMember 修饰的私有成员。 UiReportDef 位于依赖于 UI 相关程序集等的程序集中。我没有设计现有的类层次结构。 我需要将 ReportDefsUiReportDefs (作为 ReportDefs)传递给新服务。 由于 ReportDef 是具体的,因此我希望序列化程序在没有任何其他信息的情况下将 UiReportDefs 视为 ReportDefs

I have a class, ReportDef, which is a concrete class that I've decorated with [DataContract] and [DataMember] attributes as needed. ReportDef is in assembly A1 along with my ServiceContract, IReportService. I then have another class, UiReportDef, which derives from ReportDef and is in assembly A2. UiReportDef has no additional state that the service cares about.

I want to invoke my service with an instance of UiReportDef. Is there any way (short of manually constructing a ReportDef instance from UiReportDef) to do this without having my service know about A2? I know about KnownType. I don't want to reference A2.


EDIT: Here's some context that might make my question easier to understand. My ServiceContract implements IReportService which defines a method, RunReport(ReportDef report). ReportDef is decorated with the DataContract attribute, and has private members decorated with DataMember. UiReportDef is in an assembly that depends on UI-related assemblies, etc. I didn't design the existing class hierarchy. I need to pass ReportDefs and UiReportDefs (as ReportDefs) to the new service. Since ReportDef is concrete, I would expect the serializer to treat UiReportDefs as ReportDefs in the absence of any other information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-07-25 12:28:12

WCF 不是多态的,因为它不是面向对象的。 因此这是不可能的。

WCF is not polymorphic because its not object oriented. Hence this is not possible.

酒废 2024-07-25 12:28:11

我没听懂你的问题。

但我想我现在明白了一部分,你想反序列化程序集中没有引用它的对象吗?

如果是这样,你不能,除非你愿意进行大量的反思并将其定义/引用为“对象”。

执行此操作并在我之前的答案中试图解释的常见方法是,你应该使用客户端/服务器端都可以引用的接口。

对于这种情况,常见的做法是创建只包含接口的存根程序集。

或者正如您对问题的评论所暗示的那样,您可以使用 DTO 对象。
http://en.wikipedia.org/wiki/Data_Transfer_Object

I didn't understand you question.

But I think I understand part of it now, you want to deserialize an object in an assembly that has no reference to it?

If so, you can't unless you're willing to do a whole lot of reflection and keep it defined/referenced as "object"

The common way to do this and was trying to explain in my previous answer is that you should use an interface that can be referenced by both client/server.

It is common practice to create stub assemblies consisting of nothing but interfaces for this case.

Or as your comment on your question suggests, you can use DTO objects.
http://en.wikipedia.org/wiki/Data_Transfer_Object

臻嫒无言 2024-07-25 12:28:11

该线程可能会有很大帮助: WCF 继承和 DataContract

This thread may probably help a lot : WCF Inheritance and DataContract

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文