views:

18

answers:

2

I have a Silverlight app hosted in an Azure web role ASP project. The ASP project exposes a WCF service.

I would like to have one set of class definitions for the data types. Someone recommended making a third project (class library) and adding a reference to it from the SL and ASP. I started doing this, but the Silverlight project complained that you can only add references to Silverlight projects.

I then made a Silverlight class library and moved the data classes to it. However, I to add some .dll references, like to the Windows Azure storage client. Then the Silverlight class library tells me I can only add references to Silverlight 4-friendly .dlls, of which Windows Azure isn't one. Fantastic.

Is there something I can do to get around this, or am I stuck with a less elegant, redundant solution?

A: 

See if using linked files as per this answer does the trick for you.

slugster
Thanks for the suggestion, but I still don't see how that will let me get around the limitation of only being able to reference SL assemblies from an SL project.
Rosarch
@Ros - simple: you create a new Silverlight class library, but instead of copying the files containing the data object definitions, you add them as a linked file. This means you have the exact same source file for both your ASP.NET and Silverlight projects. Same files, just exposed through a Silverlight project so that your SL app can reference them.
slugster
+1  A: 

Multi-targeting is your best bet. There is an article explaining this in Visual Studio from Microsoft at: http://msdn.microsoft.com/en-us/library/ff921092(PandP.20).aspx

Basically, you create both your Silverlight and standard .NET class libraries, each with a different name, and then include the same files into each. Usually the files are actually only in one of the class libraries and then soft linked in the second one.

The key is to ensure that the code in your files is compatible with both runtimes. If there needs to be separate implementation for some of your methods depending on the runtime then you need to separate these with pragmas (i.e. #ifdef SILVERLIGHT...).

If you're only doing data structures, however, there should be no issues as long as Silverlight supports the objects you are using.

James