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 existingcsproj
into aspnet5project.json
- maintain two versions of your shared project - a
project.json
for aspnet5 and acsproj
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:
-
Add a
global.json
file in the some top-level directory that contains your projects. In theprojects
section, list directories that contain your source-code, for example:{ "projects": [ "src", "test" ], "sdk": { "version": "1.0.0-rc1-final" } }
-
In the same directory that contains
global.json
executednu wrap
for each existing.csproj
project.
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
.
-
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.
- 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 oldcsproj
.
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.