Reputation: 11375
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?
Upvotes: 6
Views: 4558
Reputation: 53600
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"]
}
Upvotes: 2
Reputation: 2211
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
Upvotes: 4