简体   繁体   English

NuGet在解决方案之间共享项目时

[英]NuGet when sharing projects between solutions

I have several solutions that share certain common projects, such as one that contains all the necesary things to access my data model. 我有一些共享某些特定项目的解决方案,例如,其中包含访问我的数据模型的所有必要内容。

解决方案资源管理器情况

The actual problem is that trying to install packages with NuGet in one proyect with dependencies (ex. Iesi.Collections), creates a reference to the dll in the current solution directory and not in the proyect directory. 实际的问题是,尝试在具有依赖项(例如Iesi.Collections)的proyect中安装NuGet软件包,并在当前解决方案目录而不是proyect目录中创建对该dll的引用。 Then, when I try to go to another of my solutions, the shared project fails to compile (because of the missing assembly). 然后,当我尝试使用其他解决方案时,共享项目无法编译(由于缺少程序集)。

第一路径参考 第二路径参考

I have searched all around for using NuGet in shared projects between solutions in an independent way. 我已经四处搜寻以独立方式在解决方案之间的共享项目中使用NuGet。

So the question is... How can I manage obtain an independent behaviour and references between solutions in NuGet? 因此,问题是...如何在NuGet中解决方案之间的独立行为和引用?

One way to solve this issue is to add a .nuget\\NuGet.config file to all solutions, and set the repositoryPath to a decicated local folder. 解决此问题的一种方法是将.nuget\\NuGet.config文件添加到所有解决方案,然后将repositoryPath设置为专用的本地文件夹。

From this link , assuming you're using VS2015: 通过此链接 ,假设您使用的是VS2015:

  1. Create a .nuget folder in the root of the solution (on the file system) (if you're having trouble creating a .nuget folder using windows explorer, you can do so by entering .nuget. instead) 在解决方案的根目录(在文件系统上)中创建一个.nuget文件夹(如果您在使用Windows资源管理器创建.nuget文件夹时遇到问题,可以通过输入.nuget.来实现)。

  2. Inside that folder, create a file NuGet.config. 在该文件夹中,创建一个文件NuGet.config。

  3. In Visual Studio 2015, right click on the solution and add a new solution directory called “.nuget” 在Visual Studio 2015中,右键单击解决方案,然后添加一个名为“ .nuget”的新解决方案目录。

  4. Right click on that folder and select to add a new an existing file and select the NuGet.config file created in (2). 右键单击该文件夹,然后选择添加一个新的现有文件,然后选择在(2)中创建的NuGet.config文件。

  5. Add content like this inside the NuGet.config file: 在NuGet.config文件中添加如下内容:

 <?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> <config> <add key="repositoryPath" value="..\\..\\..\\..\\NugetPackages" /> </config> </configuration> 
  1. Restart Visual Studio 重新启动Visual Studio

Note that you'll have to fix existing references, for instance by removing and then adding all packages again. 请注意,您必须修复现有的引用,例如,先删除然后再添加所有软件包。

I personally use an absolute path for the packages, since not all of my solutions are in the same folder hierarchy - if you're in a team you might want to discuss this first. 我个人使用软件包的绝对路径,因为并非我的所有解决方案都在同一文件夹层次结构中-如果您是团队成员,则可能首先要讨论。

An alternative approach to this problem is modifying package reference path in .csproj file like by utilising $(SolutionDir) macro - this will point the reference to correct directory in both solutions: 解决此问题的另一种方法是像使用$(SolutionDir)宏一样修改.csproj文件中的包引用路径-这将在两种解决方案中将引用指向正确的目录:

<ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
        <HintPath>
            $(SolutionDir)packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll
        </HintPath>
    </Reference>
    ...
</ItemGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM