I have a Silverlight app I’ve been working on. This app relies on a custom class called Customer. Instances of this class are returned from my web service. I’ve need to add a method called CalculateLoyalty() to this class definition. I want CalculateLoyalty to be available on both the server and client-side (my Silverlight app).
Currently, I can use CalculateLoyalty just fine on the server. 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!
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
Call synchronous WCF operation contract methods asynchronously on silverlight
Even if you do this, I have to recommend against putting logic on your DTOs. I’d recommend creating a
LoyaltyCalculatorclass and passing aCustomerto this. 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.
You don’t need to do this, but I recommend adding 2 new projects.
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.
Add class
LoyaltyCalculatortoMpApp.Shared, orMyApp.Webif you don’t want to make the shared libraries. Go ahead and implement this class here.Now in
MyApp.Shared.Silverlight, orMyAppif you don’t want to make the shared libraries, select Add -> Existing Item. Browse to and selectLoyaltyCalculator.cs. Do Not Double Click It!!! Instead, click the little down / more arrow on the Add button. Now select Add As Link.LoyaltyCalculatoris now available to both your server and client and you only have to maintain one copy.