除了“复制粘贴继承”之外,还有什么办法吗?与 Silverlight 应用程序共享 .net 类?

发布于 2024-08-26 00:07:07 字数 412 浏览 10 评论 0原文

我的项目分为两部分:Silverlight 前端和 WCF 双工服务。理想情况下,我希望从 WCF 服务传递自定义类型的消息(称为 TradeOffer)以供 Silverlight 应用程序使用。

当我尝试这样做时,我收到一条错误,表明我无法像这样通过线路传递未知类型的对象,如果我在服务器组件上使用 InternalsVisibleTo 属性,也许我可以这样做。

我不确定这是否适用于这种环境,并且知道这在开发中会很混乱。我最初将消息定义放在一个库中以供服务和客户端使用,但无法从 Silverlight 客户端添加对该库的引用(因为它不是 Silverlight 程序集。)

有什么方法可以访问来自使用它的 Silverlight 客户端和发布它的服务的消息类的定义,而不使用 InternalsVisibleTo 属性,或者我应该以其他方式编写应用程序?

I have a project in two parts: a Silverlight front end and a WCF duplex service. Ideally, I would like to pass a message of a custom type (call it TradeOffer) from the WCF service to be consumed by the Silverlight application.

When I try to, I get an error that indicates I can't pass an object of an unknown type across the wire like that and that, maybe, I could do so if I used the InternalsVisibleTo attribute on the server component.

I'm not sure if that would work in this environment and know it would be messy in development. I originally put the message definition in a library to be used by both the service and the client, but couldn't add a reference to the library from the Silverlight client (because it's not a Silverlight assembly.)

Is there some way I can access the definition of a message class from both the Silverlight client that consumes it and the service that publishes it without using the InternalsVisibleTo attribute or should I write the application another way?

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

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

发布评论

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

评论(4

醉生梦死 2024-09-02 00:07:07

构建 2 个不同的 VS 项目/程序集。一种用于 Silverlight,另一种用于服务器。在两个项目中使用相同的物理类文件。如果组件中有 Silverlight 或 Server 特定功能;您可以使用编译器指令来对它们进行排序。

Build 2 different VS projects/assemblies. One for Silverlight and one for the Server. Use the same physical class file in both projects. If you have Silverlight or Server specific functionality in the component; you can use compiler directives to sort them out.

我恋#小黄人 2024-09-02 00:07:07

执行您所描述的操作的标准方法是使用“添加服务引用”对话框构建 WCF 服务引用。然后,Visual Studio 会为您构建一个代理,其中包含一系列与您的 .NET 类具有相同字段和属性的 Silverlight 类。当然,这不会传输任何业务逻辑,但无论如何您都不应该使用类似的类来进行线上数据传输。 (换句话说,数据传输对象应该尽可能简单。)

您还可以使用RIA 服务 作为“添加服务引用”对话框的更动态的替代方案,但我的理解是,RIA 服务此时与双工服务不能很好地配合。

在 Silverlight 4 世界中,如果您首先在 Silverlight 中编译程序集,则可以进行有限数量的程序集共享: http://blogs.msdn.com/clrteam/archive/2009/12/01/sharing-silverlight-assemblies-with -net-apps.aspx

The standard way of doing what you describe is to build your WCF service references using the Add Service Reference dialog box. What happens then is that Visual Studio builds a proxy for you that includes a series of Silverlight classes that have the same fields and properties as your .NET class. This doesn't transfer any of the business logic, of course, but you shouldn't be using a class like that for over-the-wire data transfer anyway. (In other words, data transfer objects should be as dumb as possible.)

You can also use RIA services as a more dynamic alternative to the "Add Service Reference" dialog box, but my understanding is that RIA services don't work well at this point with duplex services.

In the Silverlight 4 world, it's possible to do a limited amount of assembly sharing, if you start by compiling the assembly in Silverlight: http://blogs.msdn.com/clrteam/archive/2009/12/01/sharing-silverlight-assemblies-with-net-apps.aspx

堇色安年 2024-09-02 00:07:07

其余的帖子自然是正确的,但我认为我应该进行更新:-

  • 可移植库 处于测试版 (CTP)
  • .NET 4.0 可以使用 SL4 目标 DLL

The rest of the posts were naturally correct, but jsut thought I'd throw in an update:-

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