12

Let's imagine I already have a project building .NET 3.5 assembly. Now I want to build this assembly for Silverlight, and moreover, maintain its Silverlight version with minimal efforts further.

What is not acceptable:

  • Creating separate project for Silverlight build

What is acceptable:

  • Adding custom directives allowing me to target either Silverlight or .NET dependently on e.g. MSBuild properties.
  • Adding special build configurations for Silverlight
  • Adding #ifdef Silverlight / #endif sections to the source code.
  • Generally any other modification of .csproj / .cs.

So basically, I'd like to maintain a single project, but target two frameworks. I don't want to maintain two separate projects, because this may lead to mistakes like forgetting to include a new file. If there are many project and big team, this is really important to exclude such mistakes.

If this is completely impossible, any solution providing similar benefits is acceptable.

Alex Yakunin
  • 6,330
  • 3
  • 33
  • 52
  • The question is unclear. Do you want to build an assembly and then use that assembly's methods etc. in a Silverlight app? Since most of your real work in Silverlight is done via communication with webservices then I'm not sure you have an issue. Perhaps you have a tiering problem? Meaning you have tiers that don't have clear definitions between them. – jcollum Jul 03 '09 at 18:07
  • 1
    Silverlight is .NET 3.5. I believe you mean Silverlight and WPF. Also, it's very similar to this question: http://stackoverflow.com/questions/208123/what-is-the-best-practice-for-compiling-silverlight-and-wpf-in-one-project – Scott Whitlock Jul 03 '09 at 18:20
  • 1
    Silverlight is .NET 3.5? Seems impossible, since as you've just said, its projects reference different mscorlib. Moreover, it is completely different execution platform... – Alex Yakunin Jul 03 '09 at 19:29
  • "Do you want to build an assembly and then use that assembly's methods etc. in a Silverlight app" - yes, exactly. Let's imagine I want to share e.g. the same BLL \ DAL code between .NET and Silverlight app. – Alex Yakunin Jul 03 '09 at 19:30
  • "I believe you mean Silverlight and WPF" - no, I don't mean WPF. I'm not interested in any presentation code at all. I'm talking about logic-only code. – Alex Yakunin Jul 03 '09 at 19:31
  • 1
    So let's assume I want to build an assembly containing Calculator class with a single object Calculate(string expression) method, and I need to build 2 versions of this assembly: one for .NET 3.5 and another one - for Silverlight. – Alex Yakunin Jul 03 '09 at 19:33

5 Answers5

8

Have you also ruled out linking to the files inside the your project from a Silverlight project? That's a fairly common approach to sharing an implementation between Silverlight and the full CLR. Sharing Code Between .NET and Silverlight Platforms

Also, according to Justin Angel you can reference and use a Silverlight class library from the full CLR. I haven't tried this myself, and it leaves some questions unanswered, but it does make the scenario straightforward: http://silverlight.net/blogs/justinangel/archive/2008/12/29/using-silverlight-dlls-on-the-desktop.aspx

OdeToCode
  • 4,906
  • 23
  • 18
  • Is there any tool allowing me to maintain the original project and Silverlight project with links (or at least the links there) in sync automatically? – Alex Yakunin Jul 03 '09 at 18:58
  • 1
    In Prism (http://compositewpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=19170) there is a Project Linker. Overview here: http://www.global-webnet.net/blogengine/post/2009/01/10/Project-Linker-sharing-single-code-base-between-Silverlight-and-Desktop-applications.aspx – OdeToCode Jul 03 '09 at 20:48
  • Great - I'll wait for other answers, if any. For now it seems that's the best option I have... – Alex Yakunin Jul 03 '09 at 21:07
1

I concur with Scott. Save yourself a lot of pain. Two projects that share the same codebase is the way to go. You'll need it to use VStudio in both environments, to use different libs, to include/exculde files, to do so many things...easily!

The reason's for having two projects far outweight the excuses for having one.

Ray
  • 2,974
  • 20
  • 26
1

The MSDN contains detailed information about platform Multi-targeting: Multi-targeting on MSDN

jdearana
  • 1,089
  • 12
  • 17
0

You have to have two projects because the mscorlib references are different for the two platforms.

Check out this question: http://www.google.ca/search?hl=en&q=targetting+silverlight+and+wpf&meta=&aq=f&oq=

If all you want to do is have a regular old .NET library that shared between the two, then I suggest creating two projects (one for Silverlight, one for regular) and including the same files in both projects. This is much easier to understand for other developers.

Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
  • Can I disable \ enable necessary options e.g. with Condition=" '$(SilverlightBuild)' != '' " ? – Alex Yakunin Jul 03 '09 at 18:53
  • 1
    You can if you want to. But you won't have to because they're incorporated in the two project files. e.g. Each .csproj #defines SL or WPF or NET35. Each csproj includes/excludes whichever files/libs it needs. Also don't link files, put the csproj in the same dir and just "Add -> Existing Item". – Ray Jul 03 '09 at 22:56
  • Concerning "can I disable" - I mean "can I maintain a single project using MSBuild Condition atribute?" – Alex Yakunin Jul 04 '09 at 07:13
0

I think what you need to do is tier this out properly. Your Silverlight code should be only for the UI and the communication with backend WCF services. Those services would run your .NET 3.5 code (the code that you want to share). That way you have sharing and n-tier as well.

If you're doing heavy calculation on the client-side in your Silverlight code and then submitting that to the server (and probably the db) then I think you're opening up a security hole.

You haven't given a compelling reason why a separate project would need to access code in the Silverlight project.

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • I'm aware about the architecture of Silverlight \ RIA apps, and I'm asking the question because I know exactly what I need. So the advice is good in general, but here it doesn't suit at all. – Alex Yakunin Jul 03 '09 at 20:10
  • There can be many many reasons to do heavy calculations on the clients - e.g. imagine I'd like to sort some data (let's say, 1MB) locally. I already delivered it to the client, and cost of its sorting is much smaller than the cost of getting sorted data directly from the server. – Alex Yakunin Jul 03 '09 at 20:12
  • It's just an example. Another one is Paint.NET-like program. Lots of filtering effects simply must be handled locally in this case. P.S. Don't tell me about Silverlight 3.0 bitmaps & GPU acceleration, I'm aware about this. In short, I need an exact answer. – Alex Yakunin Jul 03 '09 at 20:14