views:

57

answers:

3

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?

+2  A: 

In Reusing .NET Assemblies in Silverlight the author describes several techniques that may be helpful. I have used the file level sharing and found it to work very well. (http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight)

A similar article is Sharing Entities between WCF and Silverlight. (http://10rem.net/blog/2009/07/13/sharing-entities-between-wcf-and-silverlight)

DaveB
+1  A: 

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.

DancesWithBamboo
+1  A: 

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

Ken Smith