Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 219
Matt Brooks Avatar asked May 20 '15 16:05

Matt Brooks


People also ask

What can you do using the project JSON file for an ASP NET application?

json” file in ASP.NET 5 ASP.NET Core 1.0. This new file the way to define your dependencies, managing your runtime frameworks, compilation settings, adding scripts to execute at different events (Prebuild, Postbuild etc.)

What is Project JSON file in ASP NET MVC?

json file used by NuGet is a subset of that found in ASP.NET Core projects. In ASP.NET Core project. json is used for project metadata, compilation information, and dependencies. When used in other project systems, those three things are split into separate files and project.

How do I open project JSON in asp net core?

Right click your project and select "Edit ProjectName. csproj" to view the file.


1 Answers

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.

like image 160
Victor Hurdugaci Avatar answered Sep 18 '22 13:09

Victor Hurdugaci