简体   繁体   English

在 Silverlight 和“plain vanilla”.Net 之间共享程序集

[英]Sharing assembiles between Silverlight and “plain vanilla” .Net

I'm using some generic classes as data containers, and am using them to pass data to and from a wcf service.我正在使用一些通用类作为数据容器,并使用它们将数据传入和传出 wcf 服务。 Because of the way WCF mangles generic names on the client side into a class named something like "ListOfBlah231546797646", I'm adding a reference to the real assembly as a "KnownType".由于 WCF 将客户端的通用名称修改为 class 命名为“ListOfBlah231546797646”之类的名称的方式,我将添加对真实程序集的引用作为“KnownType”。

Silverlight needs to consume these services, but can only reference "silverlight assemblies". Silverlight 需要消费这些服务,但只能引用“silverlight 组件”。 I've moved the classes to their own "silverlight assembly" and can reference them from silverlight, but when the service runs I get a "cannot find referenced assembly" error on the System.Runtime.Serialization assembly.我已将这些类移至它们自己的“silverlight 程序集”,并且可以从 silverlight 引用它们,但是当服务运行时,我在 System.Runtime.Serialization 程序集上收到“找不到引用的程序集”错误。

It turns out that Silverlight has it's own set of binaries, all labelled version 2.0.5.0.事实证明,Silverlight 有自己的一组二进制文件,都标记为版本 2.0.5.0。 These aren't in the service's GAC and therefore the exception is thrown.这些不在服务的 GAC 中,因此会引发异常。

Because of this I can't reference my "Silverlight Assembly" from my service's code.因此,我无法从我的服务代码中引用我的“Silverlight 程序集”。 Is there any way I can get around this issue, making the two flavors cross compatible when they get serialized?有什么办法可以解决这个问题,使两种口味在序列化时交叉兼容?

This question is similar, but none of the answers help.这个问题很相似,但没有一个答案有帮助。 Any ideas?有任何想法吗? similar question 类似的问题

The way I share code between Silverlight and normal CLR, is to use the "add as link" feature with C# projects.我在 Silverlight 和普通 CLR 之间共享代码的方式是使用 C# 项目的“添加为链接”功能。 So it ends up looking like this:所以它最终看起来像这样:

| SilverlightLib
|   File1.cs
|   File2.cs
| ClrLib
|   File1.cs <as link>
|   File2.cs <as link>

Then VS works fine, and both sets of code get compiled.然后 VS 工作正常,两组代码都被编译。 The annoying part is where the Silverlight framework doesn't line up.烦人的部分是 Silverlight 框架没有对齐的地方。 (WCF has some parts that don't exist in SL.) In that case, you'll need to use the preprocessor "#if SILVERLIGHT" to make the code target both platforms. (WCF 有一些在 SL 中不存在的部分。)在这种情况下,您需要使用预处理器“#if SILVERLIGHT”来使代码面向两个平台。

This has worked pretty well so far.到目前为止,这工作得很好。 This way, I can write code, test with VSTS, but still have it work on SL from the same source.这样,我可以编写代码,使用 VSTS 进行测试,但仍然可以从同一来源在 SL 上运行。 A few tips:一些提示:

  • Always edit from the SL project -- this way the editor will limit to SL, and you won't get surprises later on.始终从 SL 项目进行编辑——这样编辑器将限制为 SL,以后不会有意外。
  • Sometimes you have to close the opened file for Intellisense to update in the other project.有时您必须关闭打开的文件,Intellisense 才能在另一个项目中更新。

There are two ways that I've done this in the past.我过去有两种方法。

First and easiest.首先也是最简单的。 Add the WCF Service as a ServiceReference into Silverlight.将 WCF 服务作为 ServiceReference 添加到 Silverlight 中。 This will take care of regenerating all the class libraries and refreshing them when needed.这将负责重新生成所有 class 库并在需要时刷新它们。

Second, store two copies of the classes, one in silverlight and one in the .net 3.5 clr.其次,存储类的两份副本,一份在 silverlight 中,一份在 .net 3.5 clr 中。 Then ensure that the DataContract Names and Namespaces match.然后确保 DataContract 名称和命名空间匹配。 If you add the ServiceReference in silverlight then in explorer view the ServiceReference folder and look at the Reference.cs file you'll see the classes generated and can copy those.如果您在 silverlight 中添加 ServiceReference,然后在资源管理器中查看 ServiceReference 文件夹并查看 Reference.cs 文件,您将看到生成的类并可以复制它们。

Not sure if it is possible in your scenario, but have you thought about providing your objects serialized as Json to your silverlight client?不确定在您的场景中是否可行,但您是否考虑过将序列化为 Json 的对象提供给您的 silverlight 客户端? Then in your silverlight app you can use theJsonObject in silverlight.然后在您的 silverlight 应用程序中,您可以使用 silverlight 中的JsonObject That way you avoid having another set of model objects in your silverlight app.这样您就可以避免在 silverlight 应用程序中使用另一组 model 对象。

JsonObject user = (JsonObject)JsonObject.Load(responseStream);
bool isMember = user["IsMember"];
string name = user["Name"];
int age = user["Age"];

The example is from this msdn sample该示例来自此 msdn 示例

The nice thing about this approach is also that you have linq support in silverlight, and this is usable by your ajax clients as well.这种方法的好处还在于您在 silverlight 中支持 linq,您的 ajax 客户端也可以使用它。 It is also more secure than exposing your real objects to the silverlight app running on the client.它也比将您的真实对象暴露给客户端上运行的 silverlight 应用程序更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM