It is common to break a large solution is multiple projects for a question of organization of the code base and this was easily done in the earlier versions of the .NET Framework from inside Visual Studio.
How the same can be done with .NET CLI? Suppose we have the following simplified scenario for example:
- Solution Folder
- global.json
- src
- LibProject
- ConsoleProject
Suppose now that the ConsoleProject
depends on the LibProject
. Intuitively I belive that this means that in the ConsoleProject
the project.json
will have to contain a dependencies
section like this:
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-*"
},
"LibProject": "1.0.0-*"
}
But if we do this, when we try to restore the dependencies for the ConsoleProject
or when we try to build it we can't do it. When we try to restore we get the message
Unable to resolve 'LibProject (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'.
I understand the reason. When restoring, NuGet is trying to find this as a package on the specified feeds on the NuGet.config
. But it shouldn't do this, it should use the one on the sibling folder.
In the previous versions of .NET Core, we would add the reference through VS and then, if we would try to build the ConsoleProject
, VS would first build LibProject
and use the corresponding DLL.
How the same kind of thing is done here? How do we reference another project in the same solution and how do we restore/build/run with the .NET CLI considering this kind of dependency?
One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.
NET dll directly (and of course still enjoy the benefits of intellisense), basically the same thing as doing Project > Add Reference > Browse in Visual Studio. And that's it! You can still use the GUI in Visual Studio, so this post is targeted more towards devs that either edit the csproj by hand or build for .
After you define the projects the solution have on the global.json file you can just reference them on the project.json by name without a specific version.
Example:
global.json
{
"projects":[
"ConsoleProject",
"LibProject"
]
}
ConsoleProject/project.json
{
"dependencies":{
"LibProject":"",
}
}
You can find a better example in here: http://forums.dotnetfoundation.org/t/referencing-another-project-in-net-core/1298/2
Or in this repository: https://github.com/cartermp/dnx-apps/tree/master/multiple-projects
According to the documentation on writing libraries, the new correct way of specifying a dependency on another project in the solution is:
{
"dependencies":{
"AwesomeLibrary.Core": {
"version": "1.0.0",
"target": "project"
}
}
}
The target: project
bit tells NuGet that it shouldn't look in a package source.
This assumes you have the following directory structure for your solution:
AwesomeLibrary
|__global.json
|__/src
|__/AwesomeLibrary.Core
|__Source Files
|__project.json
|__/AwesomeLibrary.Other
|__Source Files
|__project.json
/test
(... etc)
With a global.json
file like:
{
"projects":["src", "test"]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With