Sharing code between aspnet5 and "old" .net

If you want to share some common code between aspnet5 and regular csproj projects, you basically have three options: * create a nuget from the common library and push it to a shared nuget repository
* use dnu wrap to wrap existing csproj into aspnet5 project.json * maintain two versions of your shared project - a project.json for aspnet5 and a csproj for regular .net

The last option is the most error-prone and harder to maintain, so I will just ignore it.

Shared nuget package

Sharing a library through nuget has many benefits: * versioning * easy restoration (devs don't need to compile anything, just restore nuget packaget) * less projects to compile - shorter compilation time

If the shared code doesn't change to often, I would go with a shared nuget repository. But if the shared code is constantly changed, going through pack-push-restore process could be painful and time-consuming.

Wrapping csproj with dnu wrap

There is one other way - dnu has a handy command named wrap. It wraps your existing .csproj files into project.json files that can be then referenced by aspnet5 projects.

You can do something like this:

  1. Add a global.json file in the some top-level directory that contains your projects. In the projects section, list directories that contain your source-code, for example:

    {
        "projects": [ "src", "test" ],
        "sdk": {
            "version": "1.0.0-rc1-final"
        }
    }
    
  2. In the same directory that contains global.json execute dnu wrap for each existing .csproj project.

    dnu wrap src/my.project/my.project.csproj  
    

This should create a directory wrap containing project.json files that wrap .csprojs. A sample file looks like this:

<!-- language: lang-json -->

    {
        "version": "1.0.0-*",
        "frameworks": {
            "net45": {
                "wrappedProject": "../../src/my.project/my.project.csproj",
                "bin": {
                    "assembly": "../../src/my.project/obj/{configuration}/my.project.dll",
                    "pdb": "../../src/my.project/obj/{configuration}/my.project.pdb"
                }
            }
        }
    }

Note that wrap directory is also added to projects section in global.json.

  1. In your solution, add a new aspnet project and add a reference to the wrapped project. Just add:

    "my.project": ""
    

to dependencies section. Aspnet should automatically pick up global.json file in root directory, and will look for projects in all directories listed there, including wrap directory.

  1. Now you're good to go - you can use all classes from my.project, step into them while debugging, go to definition, etc. Note that in your solution, you still have the old csproj.

You can find a sample code here: https://github.com/heavymetaldev/aspnet5-wrap.

I suppose this may get a little complicated if you have some custom things in your projects, like conditional compilation, custom includes, etc.