简体   繁体   English

Silverlight-使用自定义方法序列化部分类

[英]Silverlight - Serialize Partial Class with a custom method

I have a Silverlight app I've been working on. 我有一个正在开发的Silverlight应用程序。 This app relies on a custom class called Customer . 此应用程序依赖于一个名为Customer的自定义类。 Instances of this class are returned from my web service. 该类的实例从我的Web服务返回。 I've need to add a method called CalculateLoyalty() to this class definition. 我需要向该类定义添加一个称为CalculateLoyalty()的方法。 I want CalculateLoyalty to be available on both the server and client-side (my Silverlight app). 我希望CalculateLoyalty在服务器端和客户端(我的Silverlight应用程序)上均可用。

Currently, I can use CalculateLoyalty just fine on the server. 目前,我可以在服务器上正常使用CalculateLoyalty Unfortunately, the method doesn't seem to get passed across the wire. 不幸的是,这种方法似乎并没有通过。 I have a hunch its some serialization thing. 我预感它会进行一些序列化。 How do I add a method to my class definition on the server-side and ensure that it is available on the client-side? 如何在服务器端的类定义中添加方法,并确保该方法在客户端可用?

Thank you! 谢谢!

Methods are not serialized, only data (property/field values) are, so you must be using a different version of the .cs file on the server than you are on the client. 方法未序列化,仅数据(属性/字段值)被序列化,因此您必须在服务器上使用与客户端不同的版本的.cs文件。 Are you sharing the source code between your web service and silverlight projects? 您是否正在Web服务和Silverlight项目之间共享源代码?

If you are on .NET 4.5/VS2012, you may be able to create a "Portable class library" with your class in it that can be referenced from both your .NET and Silverlight projects. 如果您使用的是.NET 4.5 / VS2012,则可以使用其中的类创建一个“可移植类库”,该类可从.NET和Silverlight项目中引用。

When you generate a service reference, it only copies public properties and fields. 生成服务引用时,它仅复制公共属性和字段。 You can share classes between your server and client and avoid using a service reference. 您可以在服务器和客户端之间共享类,并避免使用服务引用。 I'm not going to go into detail with how to do this, but here are some related questions that explain what needs to be done. 我不会详细介绍如何执行此操作,但是以下是一些相关问题,这些问题解释了需要执行的操作。

Create WCF Client without auto generated proxy 创建没有自动生成代理的WCF客户端
Call synchronous WCF operation contract methods asynchronously on silverlight 在Silverlight上异步调用同步WCF操作协定方法

Even if you do this, I have to recommend against putting logic on your DTOs. 即使您这样做,我也建议不要在DTO上放置逻辑。 I'd recommend creating a LoyaltyCalculator class and passing a Customer to this. 我建议创建一个LoyaltyCalculator类,并将Customer传递给该类。 In fact, you can do this even if you use generate your client through the Add Service Reference option. 实际上,即使您通过“ 添加服务引用”选项使用生成客户端,也可以执行此操作。

Your defult Silverlight solution will have 2 projects. 您丰富的Silverlight解决方案将包含2个项目。

  • MyApp - This is your Silverlight project. MyApp-这是您的Silverlight项目。
  • MyApp.Web - This is the host web project. MyApp.Web-这是宿主Web项目。

You don't need to do this, but I recommend adding 2 new projects. 您不需要这样做,但是我建议添加2个新项目。

  • MyApp.Shared - A .NET Class Library MyApp.Shared-一个.NET类库
  • MyApp.Shared.Silverlight - A Silverlight Class Library. MyApp.Shared.Silverlight-一个Silverlight类库。

At this point, you will want to add a project reference to the appropriate class library to both your Silverlight project and your Web project. 此时,您将要在Silverlight项目和Web项目中都添加对相应类库的项目引用。

Add class LoyaltyCalculator to MpApp.Shared , or MyApp.Web if you don't want to make the shared libraries. 如果不想创建共享库, MyApp.WebLoyaltyCalculator添加到MpApp.SharedMyApp.Web Go ahead and implement this class here. 继续并在这里实施该课程。

Now in MyApp.Shared.Silverlight , or MyApp if you don't want to make the shared libraries, select Add -> Existing Item . 现在,在MyApp.Shared.SilverlightMyApp如果您不想创建共享库,请选择MyApp.Shared.Silverlight > Existing Item Browse to and select LoyaltyCalculator.cs . 浏览并选择LoyaltyCalculator.cs Do Not Double Click It!!! 不要双击它!!! Instead, click the little down / more arrow on the Add button. 而是单击“添加”按钮上的向下/更多箭头。 Now select Add As Link . 现在,选择添加为链接

LoyaltyCalculator is now available to both your server and client and you only have to maintain one copy. LoyaltyCalculator现在可用于您的服务器和客户端,并且您只需维护一个副本。

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

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