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

It is well known that breaking a big solution is a few projects to the question of creating a code base, and this was easily done in early versions of the .NET Framework from within Visual Studio.

How can this be done with the .NET CLI? Suppose we have the following simplified scenario, for example:

- Solution Folder - global.json - src - LibProject - ConsoleProject 

Suppose now that ConsoleProject dependent on LibProject . Intuitively, I suppose this means that the ConsoleProject in project.json should 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 ConsoleProject or when we try to build it, we cannot do it. When we try to recover, we get a message

Cannot resolve "LibProject (> = 1.0.0)" to ".NETCoreApp, Version = v1.0".

I understand the reason. When recovering, NuGet tries to find this as a package in the specified feeds on NuGet.config . But he should not do this, he should use the one that is in the sibling folder.

In previous versions of .NET Core, we added the link via VS, and then, if we tried to build ConsoleProject , VS first built LibProject and used the appropriate DLL.

How is the same done here? How do we refer to another project in the same solution and how can we restore / build / run with the .NET CLI with this dependency in mind?

+5
source share
2 answers

Once you have identified the projects that the solution has in the global.json file, you can simply reference them to project.json by name without a specific version.

Example:

global.json

 { "projects":[ "ConsoleProject", "LibProject" ] } 

ConsoleProject / project.json

 { "dependencies":{ "LibProject":"", } } 

You can find the best example 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

+4
source

According to the documentation for writing libraries , the new correct way to determine the dependence on another project in the solution:

 { "dependencies":{ "AwesomeLibrary.Core": { "version": "1.0.0", "target": "project" } } } 

The target: project bit tells NuGet that it should not look at the source of the package.

This assumes that for your solution there is the following directory structure:

 AwesomeLibrary |__global.json |__/src |__/AwesomeLibrary.Core |__Source Files |__project.json |__/AwesomeLibrary.Other |__Source Files |__project.json /test (... etc) 

With global.json file, for example:

 { "projects":["src", "test"] } 
+2
source

Source: https://habr.com/ru/post/1247833/


All Articles