简体   繁体   English

我可以在安装 NuGet 包时解决添加这些.dll 的问题吗?

[英]Can I work around adding these .dlls when installing NuGet package?

I started off by reading through this suggested question similar to mine, but there was no resolution: Why does MSTest.TestAdapter adds its DLLs into my NuGet package?我首先通读了这个与我类似的建议问题,但没有解决办法: Why does MSTest.TestAdapter adds its DLLs into my NuGet package?

Quick Problem Description快速问题描述

I wrote a NuGet package, and each time I install it, NUnit and NUnit3TestAdapter .dll 's get added to the project I installed on.我写了一个 NuGet 包,每次安装它时, NUnitNUnit3TestAdapter .dll都会添加到我安装的项目中。 I want to find a solution that fixes this issue.我想找到解决此问题的解决方案。

Repro steps复制步骤

I have pushed two git repositories that reproduce the issue I am describing.我推送了两个重现我所描述问题的 git 存储库。

ClientLibrary / MainFramework (project from which I generated NuGet package) - https://github.com/harbourc/client-library-repro-nuget-issue ClientLibrary / MainFramework (我从中生成 NuGet 包的项目)- https://github.com/harbourc/client-library-repro-nuget-issue

TargetProject (project that package is to be installed on) - https://github.com/harbourc/target-project-repro-nuget-issue TargetProject (要安装包的项目)- https://github.com/harbourc/target-project-repro-nuget-issue

You can clone both repositories, restore their NuGet packages, and reproduce the issue as follows:您可以克隆这两个存储库,恢复它们的 NuGet 包,然后重现问题,如下所示:

  1. Locate ClientLibrary.1.0.0.nupkg in client-library-repro-nuget-issue/ClientLibrary/在 client-library-repro-nuget-issue/ClientLibrary/ 中找到 ClientLibrary.1.0.0.nupkg

  2. Open package manager console for target-project-repro-nuget-issue and run打开 target-project-repro-nuget-issue 的包管理器控制台并运行

Install-Package C:\Path\To\client-library-repro-nuget-issue\ClientLibrary\ClientLibrary.1.0.0.nupkg
  1. Note the NUnit and NUnit3TestAdapter .dll 's that are added into TargetProject -- even though TargetProject already has NUnit and NUnit3TestAdapter installed.请注意添加到TargetProject中的NUnitNUnit3TestAdapter .dll即使TargetProject已经安装了NUnitNUnit3TestAdapter

Longer Overview更长的概述

I have created my own NuGet package for internal use, called ClientLibrary , and I am attempting to install it on to another project, called TargetProject .我创建了自己的 NuGet 包供内部使用,称为ClientLibrary ,我正试图将它安装到另一个项目,称为TargetProject Here is a quick breakdown of the structure:这是结构的快速分解:

  • FullSolution.sln
    • MainFramework.csproj
    • ClientLibrary.csproj --> .nupkg generated from this ClientLibrary.csproj --> .nupkg生成

Separate project:独立项目:

  • TargetProject.sln
    • TargetProject.csproj --> install .nupkg onto this TargetProject.csproj --> 在此安装.nupkg

ClientLibrary has a reference to MainFramework , and uses many methods from MainFramework . ClientLibrary有对MainFramework的引用,并使用MainFramework中的许多方法。

When installing ClientLibrary.1.0.0.nupkg onto TargetProject , the following .dll 's are getting added to TargetProject :ClientLibrary.1.0.0.nupkg安装到TargetProject时,以下.dll将添加到TargetProject

nunit.engine.api.dll
nunit.engine.dll
NUnit3.TestAdapter.dll
NUnit3.TestAdapter.pdb

If I delete these .dll 's, everything works fine, because TargetProject already has those packages installed anyway.如果我删除这些.dll的,一切正常,因为TargetProject已经安装了这些包。 They are not necessary, it's just annoying to have to delete them when installing.它们不是必需的,安装时不得不删除它们很烦人。

Here is how I am adding ClientLibrary NuGet package to TargetProject :以下是我将ClientLibrary NuGet 包添加到TargetProject的方式:

  1. Build ClientLibrary and MainFramework projects to generate their.dlls构建ClientLibraryMainFramework项目以生成它们的.dll
  2. Change directory into ClientLibrary folder and run nuget spec将目录更改为ClientLibrary文件夹并运行nuget spec

.nuspec file is generated: .nuspec文件生成:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ClientLibrary</id>
    <version>1.0</version>
    <title>Client Library</title>
    <authors>Myself</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Client library for interacting with my application.</description>
    <dependencies>
      <group targetFramework=".NETFramework4.7.2" />
    </dependencies>
  </metadata>
</package>
  1. Run nuget pack -IncludeReferencedProjects -- Because ClientLibrary has a dependency on MainFramework (and several other packages used by MainFramework )运行nuget pack -IncludeReferencedProjects因为ClientLibrary依赖于MainFramework (以及MainFramework使用的其他几个包)

  2. Navigate to TargetProject , open Package Manager Console导航到TargetProject ,打开包管理器控制台

  3. Run Install-Package C:\Path\To\ClientLibrary.1.0.0.nupkg运行Install-Package C:\Path\To\ClientLibrary.1.0.0.nupkg

Installation runs successfully, and then those .dll 's I am complaining about get added.安装成功运行,然后添加了我抱怨的那些.dll

Problem:问题:

MainFramework has NUnit and NUnit3TestAdapter NuGet packages installed. MainFramework安装了NUnitNUnit3TestAdapter NuGet 包。 ClientLibrary does not. ClientLibrary没有。 So, the .dll 's seem to be added because they are installed on MainFramework , but NOT installed on ClientLibrary .因此,似乎添加了.dll ,因为它们安装在MainFramework上,但未安装在ClientLibrary上。 (Remember, ClientLibrary references MainFramework.dll .) (请记住, ClientLibrary引用MainFramework.dll 。)

There are other packages installed on both MainFramework and ClientLibrary , and these do not have .dll 's that get added to TargetProject upon installation, so I am assuming issue is caused by having packages present in MainFramework but NOT in ClientLibrary . MainFrameworkClientLibrary上还安装了其他包,这些包没有在安装时添加到TargetProject.dll ,所以我假设问题是由于MainFramework中存在包而不是ClientLibrary中存在包引起的。

I believe I can "fix" this issue by installing NUnit and NUnit3TestAdapter onto ClientLibrary , but ClientLibrary doesn't actually use those packages at all, so it seems unnecessary.我相信我可以通过在ClientLibrary上安装NUnitNUnit3TestAdapter来“解决”这个问题,但ClientLibrary实际上根本不使用这些包,所以它似乎没有必要。

How can I install ClientLibrary onto TargetProject without including the NUnit and NUnit3TestAdapter .dll 's, and without having to install NUnit and NUnit3TestAdapter onto ClientLibrary ?如何在不包括NUnitNUnit3TestAdapter .dll的情况下将ClientLibrary安装到TargetProject上,并且不必将NUnitNUnit3TestAdapter安装到ClientLibrary上? If possible, I would like to tell ClientLibrary.1.0.0.nupkg to use the NUnit and NUnit3TestAdapter packages that are already installed on TargetProject .如果可能的话,我想告诉ClientLibrary.1.0.0.nupkg使用已经安装在TargetProject上的NUnitNUnit3TestAdapter包。

If the answer is "Not possible", that is fine, but I would like an explanation -- my overall goal for this question is to gain a better understanding of how NuGet and dependencies work, and understand why this has been an issue in the first place.如果答案是“不可能”,那很好,但我想要一个解释——我对这个问题的总体目标是更好地理解 NuGet 和依赖项是如何工作的,并理解为什么这是一个问题第一名。 Thank you for reading.感谢您的阅读。

In general, it's best practice to keep all of your tests and corresponding NuGet packages in their own project.通常,最佳做法是将所有测试和相应的 NuGet 包保存在它们自己的项目中。 Then make sure none of the projects reference the test project.然后确保没有项目引用测试项目。

在此处输入图像描述

On the don't side, Client Library will include the NUnit dlls because they were added to a project that Client Library references.另一方面 Client Library 将包含 NUnit dll,因为它们已添加到 Client Library 引用的项目中。

Whereas on the do side, Client Library will not include the NUnit dlls because neither reference the test project.而在do方面,Client Library 将不包含 NUnit dll,因为它们都没有引用测试项目。

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

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