views:

44

answers:

4

Does anyone know how to do this? I built a backend c# class in asp.net but want to access these same classes without recreating them in silverlight. Is this a possibility?

+1  A: 

Well, ASP.NET itself isn't going to work (ditto many of the full libraries), but I'm assuming you just mean you local domain model etc.

IIRC you can try to simply reference it, but it may well generate a warning message. Of course you need to be exceptionally careful not to use anything that the other platform doesn't support...

IMO, the better option here is to create a second csproj that includes the same .cs files (or cheat with a wildcard/deep include). And build both. Same C#, different dll/platform.

Is isn't uncommon to find that you need a very small usage of #if directives, too.

Marc Gravell
@Marc same question that I asked avidgator above
cfarm54
@cfarm54 - same answer...
Marc Gravell
+3  A: 

You can reuse the cs files by adding them to your project AS LINK. Right click in your project and select Add Existing...Browse to your file and in the Open Button, use the pulldown arrow on the right to select Add As Link. You will see the file added to your project with an icon that with the little Windows Shortcut icon overlayed on it.

Just remember - the ASP.Net runs on the .Net runtime. Silverlight runs on the CoreCLR (Silverlight runtime.) Not everything that compiles in oone will compile in the other...

To separate things a little bit, #if directives can help, you can also use Partial Classes and partial methods (to add content that only runs on the server or on the client.)

avidgator
@avidgator so, does this mean it's almost smarter to build an identical class in the silverlight project since there is no certainty that everything will compile?
cfarm54
@cfarm45: the smartest way to go is almost always the one with the least code. If your restructure your domain classes so they don't depend on asp.net, you can reuse them. Copying code is always plan B.
jdv
@jdv ok i'll see what i can do
cfarm54
+1  A: 

WCF RIA Services may help you solve your problem. Silverlight does not use the same runtime as ASP.Net does and you cannot directly share assemblies containing model classes on the client and the server side. To solve that WCF RIA Services will transparently generate classes on the client side based on model classes on the server side. Obviously WCF RIA Services will also allow you to create, read, update and delete objects of these classes using a web service.

MSDN has more specific information about WCF RIA Services Client Code Generation.

Martin Liversage
@Martin how hard is this to learn?
cfarm54
@cfarm54: I don't know how to measure how hard it is, but you could try some of the samples on MSDN (follow my link) and see for yourself. If you don't need the web service then perhaps Marc Gravell's suggestion where source files are shared using links is simpler.
Martin Liversage
+2  A: 

RIA Services is definitely the way to go for sharing code between ASP.Net and Silverlight.

As well as the previously mentioned generation of domain service models, it also lets you share individual files between the web-app and Silverlight by simply inserting "shared" in to the filenames. e.g. "MyClass.shared.cs".

RIA services does not take long to get to terms with (and there are good tutorials about). Try this one.

Enough already