Sharing Libraries between Silverlight, Web Projects and Services RRS feed

  • Question

  • I've noticed that the IDE will not allow a silverlight class library to be added to a Web Application or Class Library. I assume the same is true for other non-silverlight projects.

    Likewise a regular class library cannot be added to a silverlight application.

    This makes sharing code between the client a server problematic. It would be great to use the same code base. For example, having the same validation code would be great. The same Data Transfer Objects would be great.

    Please see my post for further information.



    Wednesday, September 24, 2008 5:00 PM


  • The reason for this is that Silverlight code runs on the client side on top of Silverlight framework which is only a subset of the full .Net framework, while Server side code runs on the full .Net framework. They have different set the libraries.  Even classes have the same name space and same class name, but their functions could be stripped down for Silverlight version,  so they are not totally the same.

    For sharing data object, that's not a problem with WCF. Because usually data object contains some simple types with both ends (Server and Silverlight) can understand. If you define data object in your Service side and expose them through your Service function call, when you add Service Reference to Silverlight, a proxy class will be generated, so Silverlight can access those classes.

    For some Utility classes such as validation functions, you could share them by using Vistual Studio "add as Link"  feature to add the existing file from your Server side project to Silverlight project. But you need to make sure all the name spaces,  object types and functions you used in your class are still available in Silverlight. Otherwise, you still can not use them.

    To do this, right click your Silverlight project select "Add Existing Items", Select the file from other projects folder that you want to add to the SL project, Right click the the "Add" button to select "Add as Link" option,  a copy of the file will be create in your project.





    Wednesday, September 24, 2008 11:15 PM