views:

90

answers:

2

The setting:

I got a silverlight application and a webservice both want to use the same code.

Here my problem:

  • If make an normal dll the silverlight Application say's it can not reference it.
  • If I make an Silverlight dll the Webservice can not reference it.

I don't like duplicate code how can I share code between my Silverlight app and my webservice?

A: 

Make two dll projects, one silverlight dll and one normal dll. Have them both reference a third project with the common code which produces a static lib. Having common code as a static lib is good anyway, since you can link it in with any sort of project.

disown
-1 The concept of a "Static lib" is alien in the world .NET. All assemblies will be dependent on the external framework and even the most basic mscorlib differs between Silverlight and .NET
AnthonyWJones
Eh, what? Static libs work in .NET. I wasn't aware that Silverlight wasn't running on the CLR, so I'll give you that point. From you article it does seem like it should be possible to reference Silverlight dll:s in a .NET project, but then again you probably will become dependent on both the CLR and the Silverlight runtime. Is the purpose of your conditional compilation (source sharing) to avoid the dependency on the Silverlight runtime for the .NET app?
disown
+1  A: 

There are several approaches you can take to avoid duplication.

The most fundemental approach would be to use two different projects one targeting the .NET Framework, the other targeting Silverlight. Thus their references point at the appropriate set of assemblies and the Silverlight project defines the #SILVERLIGHT conditional compilation symbol. However they share the same set of code files, that is one will be using linked files. You would toggle around parts the need to be done differently using conditional compilation.

WCF RIA Services also offers a mechanism where you place code in *.shared.cs files in the server side project that will be automatically duplicated into a "Generated_Code" folder in the client side project.

There is a way to share a single set of code and a single output dll between both Silverlight 4 and .NET 4.0. However you will need to limit the references to a very narrow set of files. See Sharing Silverlight Assemblies with .NET Apps

AnthonyWJones
thx this has clarified the most I needed to no. Ill looking more into this after my vacation. thankyou for the helpfull hints.
psibot