What are my options for sharing code between DNX / ASP.NET 5 projects (project.json / xproj) and other C# projects (csproj) within a single solution?

Matt Brooks picture Matt Brooks · May 20, 2015 · Viewed 7.8k times · Source

Scenario

I'm experimenting with Visual Studio 2015 RC, specifically looking at switching to the new ASP.NET 5 framework, project structure and the new DNX that runs ASP.NET 5 applications.

My employer has many existing solutions targeting .NET Framework 4.5.2. In our existing Visual Studio solutions we might have the following projects:

[Solution] Sample.sln
    [Folder] src
        [Project] ClassLibrary.csproj
        [Project] WindowsService.csproj
        [Project] WebApplication.csproj
    [Folder] test
        [Project] ClassLibrary.UnitTests.csproj
        ...

In this scenario, ClassLibrary.csproj is a C# class library containing shared code. It is a dependency of / referenced by both WindowsService.csproj and WebApplication.csproj.

We aren't specifically trying to target .NET Core, dnxcore50. At this stage we are happy targeting .NET Framework, dnx451. However we are absolutely trying to leverage the new features of ASP.NET 5 and associated project structure.

Below are some options I've thought of but both have issues.

Option 1

When we replace the WebApplication.csproj project above with a new ASP.NET 5 DNX project WebApplication-dnx then we can still reference the ClassLibrary.csproj from both this new DNX project and the existing WindowsService.csproj project. However some issues arise:

  1. This approach means any changes to code in ClassLibrary.csproj need a rebuild to be visible in the running WebApplication-dnx. This isn't surprising but means we do not get full compile-from-source benefits for WebApplication-dnx.

  2. We cannot easily target other frameworks, e.g. dnxcore50. As above, this isn't specifically a goal at this stage.

Option 2

If we replace ClassLibrary.csproj with a DNX class library project^ ClassLibrary-dnx then the problems in option 1 do not apply. This approach would seem to be more in-keeping with the way the .NET runtime and associated technologies such as ASP.NET will be packaged moving forward.

However I cannot find a way to reference ClassLibrary-dnx from WindowsService.csproj. If this approach is viable I imagine the solution has something to do with the project level-option for Produce outputs on build and then referencing the .nupkg or perhaps even the .dll that is produced during the build. However, I cannot see a clean way of achieving this via the tooling.

^ DNX class library projects are called Class Library (Package) in VS 2015 RC. This project type was previously called ASP.NET Class Library in the CTPs.

Summary

Based on the information above I'm looking for:

  1. Some feedback to my scenario options and issues.

  2. Suggestions for other options I have not thought of.

  3. Perhaps an indication of which approach should be used moving forward.

Answer

Victor Hurdugaci picture Victor Hurdugaci · May 21, 2015

Take a look at what EntityFramework does.

They target 3 TFMs: net45, .NETPortable,Version=v4.5,Profile=Profile7, frameworkAssemblies and they have both csproj and xproj in the same folder.

Basically, for each project you have two project files.

However I cannot find a way to reference ClassLibrary-dnx from WindowsService.csproj.

Unfortunately, that's not possible, yet. You can only reference csproj from xproj, not the other way around. You have two alternatives: (1) have both xproj and csproj like EF does or (2) reference the NuGet package from csproj.

If you want to do alternative (2) then you can set the output of the xproj to a folder and add that as a NuGet feed.