Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify dependencies on other projects with .NET CLI?

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?

like image 996
user1620696 Avatar asked Apr 24 '16 23:04

user1620696


People also ask

How do I add a project reference in net core project?

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.

How do I reference a DLL in .NET core?

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 .


2 Answers

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

like image 138
Leonardo Emanuel Alifraco Avatar answered Oct 22 '22 13:10

Leonardo Emanuel Alifraco


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"]
}
like image 42
Nate Barbettini Avatar answered Oct 22 '22 14:10

Nate Barbettini