.Net Core project keeps referencing other project

2019-09-14 14:22发布

问题:

I seem to be having the same problem as this unanswered question.

NOTE: Ordinarily, I'd avoid the repost, but the lack of an answer and the low traffic seemed to warrant a refresh into the SO feed.

The setup

  1. Create two .Net Core projects (A & B) in two separate solutions in the same folder. From A, create a Nuget package. (You can upload it to Nuget, MyGet, or just keep locally. Just make sure the source is configured.)

  2. In B, add the Nuget package.

The result

Project A has now been added to the solution for Project B.

The questions

  1. Why is this happening?
  2. How can I get VS to recognize that it should be referencing the Nuget package, not the project?

Other stuff

I'm running VS 2015 Pro Update 3 on Windows 10.

In my real scenario, my Nuget package is at a later version. When I open the project.json file, I get a compiler warning on the ProjectA dependency that says I'm depending on version 1.1, but version 1.0 is found.

I've created an example on GitHub for you to play around with.

回答1:

You must place the solutions into separate directories as .NET Core Projects in VS2015 has unique file system requirements, and will behave poorly when you put them in the same directory (each solution will generate a hidden .vs folder, and they will end up overlapping).

For example you could change the directory structure to this:

.\Solutions\A\ <-- Directory for Solution A
.\Solutions\B\ <-- Directory for Solution B
.\A\ <-- Project A
.\B\ <-- Project B

This would mean that the hidden .vs folders for each solution will no longer overlap (ie, the solutions do not share this)

.\Solutions\A\ProjectA.sln
.\Solutions\A\global.json
.\Solutions\A\.vs\

.\Solutions\B\ProjectB.sln
.\Solutions\B\global.json
.\Solutions\B\.vs\


回答2:

Local projects take precedence over NuGet packages. I don't know at what level this is baked-in, though.

The solution is to explicitly specify in your project.json that you want to reference a NuGet package and not the project in the same solution.

"YourNuGetPackageName": {
    "target": "package",
    "version": "VersionOfYourNuGetPackage"
}

You can have a look at an example here.