views:

363

answers:

2

Hi..I am beginner in silverlight..I have created a silverlight project. When a new silverlight project is created it is automatically creating a silverlight application and one web application. I have added a class in both applications. Now I want to access a method of web application class into silverlight application class. Is it possible? I have tried to add web application reference to silverlight but VS is not allowing. Is there any another way to do??

A: 

Yes it is possible, but not in the normal way. A Silverlight assembly can only reference another Silverlight assembly (this is a limitation of VS2008, i don't know if it has been changed in VS2010).

What you need to do is add an existing file to your Silverlight project, when the file browse dialog opens you navigate to the class file you want to reuse, but instead of just clicking the Add button, click on the little down arrow on the button and choose Add as link - now the file will "exist" in both projects and can be used in both.

If you are going to do this repeatedly though, you will want to move those shared class files out into a separate assembly, do a project reference from your web app, and have the equivalent Silverlight class library mirroring it (sharing the files as links), and then project reference that Silverlight class library from your Silverlight app.

slugster
Slugster: Thanks for reply. The class which i have added is unable to access System.Data.SqlClient and other namespaces also.
Srikanth
+1  A: 

What you need is called WCF. A really simple tutorial that should get you going is found here: How to call WCF methods from Silverlight controls

Fundementally WCF allows the silverlight client to make method calls on a class instance hosted on the web site.

AnthonyWJones