简体   繁体   English

在同一解决方案中的两个不同项目上使用相同的 Nuget package

[英]Same Nuget package on two different projects in same solution

I have two API projects within the same solution (one in which is mine, and another in which I need to reference and should not be changing).我在同一个解决方案中有两个 API 项目(一个是我的,另一个是我需要参考且不应更改的)。 Both of these API projects add reference to the Ninject and Ninject.Web.Common package.这两个 API 项目都添加了对NinjectNinject.Web.Common package 的引用。 Here is how the packages are setup:以下是软件包的设置方式:

Ninject.Web.Common In Web Api A - \packages\Ninject.Web.Common.3.3.2\lib\net45\Ninject.Web.Common.dll Ninject.Web.Common In Web Api A - \packages\Ninject.Web.Common.3.3.2\lib\net45\Ninject.Web.Common.dll
Ninject.Web.Common in Web Api B - \packages\Ninject.Web.Common.3.2.3.0\lib\net45-full Ninject.Web.Common in Web Api B - \packages\Ninject.Web.Common.3.2.3.0\lib\net45-full

(note the versions are different 3.3.2 and 3.2.3 ) (注意版本不同3.3.23.2.3

What's happening is when I try to run my solution I get发生的事情是当我尝试运行我得到的解决方案时

Could not load type 'Ninject.Web.Common.OnePerRequestHttpModule' from assembly 'Ninject.Web.Common, Version=3.3.2.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7'.

on my Web Api B project (note it's trying to reference the 3.3.2 version from my Web Api A project). on my Web Api B project (note it's trying to reference the 3.3.2 version from my Web Api A project).

How can I keep both projects the same and have them reference their own respective version of Ninject?如何保持两个项目相同并让它们引用各自版本的 Ninject?

You can typically only resolve one version of a given dependency in your dependency graph ( nuget restore is mainly for calculating which version to use given your dependency graph).您通常只能在依赖关系图中解析给定依赖关系的一个版本( nuget restore主要用于计算给定依赖关系图使用哪个版本)。

This is usually fine, but your problem is that OnePerRequestHttpModule was removed between versions 3.2.3 and 3.3.2 of Ninject.Web.Common https://www.fuget.org/packages/Ninject.Web.Common/3.3.2/lib/netstandard2.0/diff/3.2.3/ and WebApiB wanted to use it. This is usually fine, but your problem is that OnePerRequestHttpModule was removed between versions 3.2.3 and 3.3.2 of Ninject.Web.Common https://www.fuget.org/packages/Ninject.Web.Common/3.3.2/ lib/netstandard2.0/diff/3.2.3/和 WebApiB 想使用它。

Your options here are to modify WebApiB so it doesn't use OnePerRequestHttpModule any more, or to downgrade the version of Ninject.Web.Common in WebApiA to 3.2.3您在此处的选择是修改 WebApiB 使其不再使用OnePerRequestHttpModule ,或者将 WebApiA 中的 Ninject.Web.Common 的版本降级到 3.2.3

You can try one of these approaches:您可以尝试以下方法之一:

  1. Create post build step which register the multi-versioned assemblies into the GAC.创建将多版本程序集注册到 GAC 的构建后步骤。 The CLR will look in the GAC before checking the output folder.在检查 output 文件夹之前,CLR 将在 GAC 中查找。 Since both assemblies are installed in the GAC, the runtime will find and load the correct versions.由于这两个程序集都安装在 GAC 中,因此运行时将找到并加载正确的版本。
  2. Install both assemblies to the GAC with gacutil.exe.使用 gacutil.exe 将这两个程序集安装到 GAC。 However, it's not very practical for development environments as the installation has to be repeated on every machine.但是,这对于开发环境来说不是很实用,因为必须在每台机器上重复安装。 Once installed, set the references` CopyLocal property to False, since there's no need for them to be in the output folder.安装后,将引用的 CopyLocal 属性设置为 False,因为它们不需要位于 output 文件夹中。
  3. Perform custom resolution via AppDomain.AssemblyResolve event.通过AppDomain.AssemblyResolve事件执行自定义解析。
  4. Copy assemblies to different folders and specify codebase element .将程序集复制到不同的文件夹并指定代码库元素 By editing a web application configuration file, you can point the application to the appropriate DLL versions even if they're not directly in the bin folder.通过编辑 web 应用程序配置文件,您可以将应用程序指向相应的 DLL 版本,即使它们不在 bin 文件夹中也是如此。
     <configuration>
              <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly>
                  <assemblyIdentity name="SomeStrongNameLib" culture="neutral" publicKeyToken="6a770f8bdf3d476a" />
                  <codeBase version="1.0.0.0" href="StrongNameLibV10/SomeStrongNameLib.dll"/>
                  <codeBase version="1.1.0.0" href="StrongNameLibV11/SomeStrongNameLib.dll"/>
                </dependentAssembly>
              </assemblyBinding>
          </runtime>
        </configuration>
  1. Use binding redirects , but I think that this is not suitable in your case.使用绑定重定向,但我认为这不适合您的情况。
  2. Use extern Nuget aliases.使用extern Nuget 别名。

Reference:参考:
Using two different versions of same the nuget package 使用相同的两个不同版本 nuget package
https://michaelscodingspot.com/how-to-resolve-net-reference-and-nuget-package-version-conflicts/ https://michaelscodingspot.com/how-to-resolve-net-reference-and-nuget-package-version-conflicts/
https://devnet.kentico.com/articles/referencing-multiple-versions-of-the-same-assembly-in-a-single-application https://devnet.kentico.com/articles/referencing-multiple-versions-of-the-same-assembly-in-a-single-application

I see you are on 4.5 FW but:我看到你在 4.5 FW 但是:

If you are already on the newer csproj format or you can upgrade to it (this is just to change the format of csproj, the project itself will still target what it was targeting )如果您已经在使用较新的 csproj 格式或者您可以升级到它(这只是为了更改 csproj 的格式,项目本身仍将针对它的目标

https://natemcmaster.com/blog/2017/03/09/vs2015-to-vs2017-upgrade/ https://natemcmaster.com/blog/2017/03/09/vs2015-to-vs2017-upgrade/

then you could stop the dependecies flowing to the top (the link is for dotnet core, but if the project format is the newer one from above, then the suggestion is still valid):那么您可以停止依赖项流向顶部(该链接用于 dotnet 核心,但如果项目格式是上面较新的格式,那么该建议仍然有效):

The way to change the default behavior is to add更改默认行为的方法是添加

<PrivateAssets>all</PrivateAssets>

for each package/project dependency inclusion.对于每个包/项目依赖项包含。

https://curia.me/net-core-transitive-references-and-how-to-block-them/ https://curia.me/net-core-transitive-references-and-how-to-block-them/

this should work also on some Full.Net FW pojects.这也应该适用于一些 Full.Net FW 项目。

Once you managed to stop the flowing of dependencjes you can add to your project your reference to the needed version of your dependency一旦您设法停止依赖项的流动,您就可以将您对所需版本的依赖项的引用添加到您的项目中

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

相关问题 如何为在同一 VS 解决方案中使用不同 package 源的项目修复 Nuget 恢复 - How to fix Nuget restore for projects using different package sources within same VS solution 使用相同 NuGet 包的两个不同版本 - Using two different versions of same the NuGet package 具有相同nuget包的项目引用了不同版本的程序集 - Projects with same nuget package referencing different version of assembly .Net Standard 2.0 生成 NuGet package 包括同一解决方案中的项目和 NuGet 包 - .Net Standard 2.0 Generate NuGet package including projects in the same solution and NuGet packages 在同一个解决方案中发布两个项目 - publish two projects in same solution 如果两个 nuget package 指向同一个 Dll 怎么办 - What if two nuget package point to the same Dll 为具有多个项目的解决方案创建 nuget 包 - Create nuget package for a solution with multiple projects UWP App 和 Package 在同一个解决方案中 - 两个应用程序 - UWP App and Package in the same Solution - two apps MSBuild用两个不同的项目构建相同的DLL - MSBuild Build same DLL with two different projects 解决方案中不同项目下相同的DbContext行为不同 - Same DbContext behaving differently under different projects in a solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM