3

Hi I would like to use the same class library from my Silverlight application and WCF based service. I created a Silverlight C# class library and found the WCF service does not allow adding reference to Silverlight Project types. So

  • What should i do to make this work?

  • Can Silverlight invoke methods on Silverlight Class library if it communicates with WCF service?

  • Is silverlight always this hard?

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

3 Answers3

4

Portable Class Library

http://msdn.microsoft.com/en-us/library/gg597391.aspx

Lex Li
  • 60,503
  • 9
  • 116
  • 147
1

Try making 2 class libraries, one for Silverlight and one for WCF. Both use the same C# source code files. In the second project, you can add the files as link (In Visual Studio in solution explorer: add existing item, and then in the drop-down Add button, choose "Add as link".

That's how we solved it for shared code. You have to limit yourself to library calls that exist in both worlds though.

chris166
  • 4,769
  • 4
  • 24
  • 25
  • What difference does it make If i copy the files to both the project(silverlight class library, C# class library) than adding a link. by the way you should have looked at my previous question too here http://stackoverflow.com/questions/10477345/c-sharp-class-library-silverlight-class-library-asmx-web-services-wth btw i think WCF Ria Class library was designed to solve this problem. – Deeptechtons May 09 '12 at 11:06
  • Also the `List` returned from the service when converted to SL based class library object does not expose Methods , i can see only the properties Why is this so ? – Deeptechtons May 09 '12 at 11:35
1

Now it is easy :D - just use the .shared trick. It allows you to share the same code between server and client. Look here: http://msdn.microsoft.com/en-us/library/ee707371(v=vs.91).aspx

PS.: You can even add conditional directives on your .shared classes, like this:

#if SILVERLIGHT
        MessageBox.Show("yay, I will run only on silverlight");
#endif
Leo
  • 7,379
  • 6
  • 28
  • 44