简体   繁体   English

引用的.NET标准项目中的多个DLL

[英]Multiple DLLs from referenced .NET Standard projects

I've created three .NET Standard class librariy C# projects with Visual Studio 2017 and default settings. 我用Visual Studio 2017和默认设置创建了三个.NET Standard类图书馆C#项目。

Projects: 项目:

  • MainProject MainProject
  • TimeProject TimeProject
    • Dependencies -> MainProject 依赖关系 - > MainProject
  • ClockProject ClockProject
    • Dependencies -> TimeProject 依赖关系 - > TimeProject

Each of them must have its own output directory like: 每个都必须有自己的输出目录,如:

<OutputPath>C:\Projects\DataControl\Build\MainProject</OutputPath>
<OutputPath>C:\Projects\DataControl\Build\TimeProject</OutputPath> 
<OutputPath>C:\Projects\DataControl\Build\ClockProject</OutputPath>

The project DLL files are placed the output directories but the problem is that the referenced project DLLs are also placed in the output directory (TimeProject.dll and MainProject.dll) 项目DLL文件放在输出目录,但问题是引用的项目DLL也放在输出目录(TimeProject.dll和MainProject.dll)

Output directories (Copy Local = true): 输出目录(Copy Local = true):

  • \\Build\\MainProject: \\建立\\ MainProject:
    • MainProject.dll MainProject.dll
  • \\Build\\TimeProject \\编译\\ TimeProject
    • MainProject.dll MainProject.dll
    • TimeProject.dll TimeProject.dll
  • \\Build\\ClockProject \\编译\\ ClockProject
    • MainProject.dll MainProject.dll
    • TimeProject.dll TimeProject.dll
    • ClockProject.dll ClockProject.dll

If I changed the property Copy Local to false, the DLL from the directly referenced project disappears. 如果我将属性Copy Local更改为false,则直接引用项目中的DLL将消失。 Thats better, but the nested referenced DLL remains -> MainProject.dll in ClockProject. 这更好,但嵌套引用的DLL仍然是 - > ClockProject中的MainProject.dll。

Output directories (Copy Local = false): 输出目录(Copy Local = false):

  • \\Build\\MainProject: \\建立\\ MainProject:
    • MainProject.dll MainProject.dll
  • \\Build\\TimeProject \\编译\\ TimeProject
    • TimeProject.dll TimeProject.dll
  • \\Build\\ClockProject \\编译\\ ClockProject
    • MainProject.dll MainProject.dll
    • ClockProject.dll ClockProject.dll

I want to prevent that the ClockProject creates a MainProject.dll in its output directory because the MainProject.dll already exists in the output directory of the MainProject. 我想防止ClockProject在其输出目录中创建MainProject.dll,因为MainProject.dll已经存在于MainProject的输出目录中。

I've a huge project and under this circumstance I have a lot of the same DLLs in my project folders. 我有一个庞大的项目,在这种情况下,我的项目文件夹中有很多相同的DLL。 During the start of my program, however, a logic also searches the subfolders for DLLs and then several DLLs of the same project are found, this leads to version conflicts. 但是,在程序启动期间,逻辑还会在子文件夹中搜索DLL,然后找到同一项目的几个DLL,这会导致版本冲突。 In general, I want to keep this logic, so I am looking for a solution that will only generate the project DLL, not the referenced ones. 一般来说,我想保留这个逻辑,所以我正在寻找一个只生成项目DLL而不是引用项目的解决方案。

Finally, my csproj file. 最后,我的csproj文件。 The other two project files look similar. 其他两个项目文件看起来很相似。

ClockProject.csproj: ClockProject.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>C:\Projects\DataControl\Build\ClockProject</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\TimeProject\TimeProject.csproj">
      <Private>false</Private>
    </ProjectReference>
  </ItemGroup>

</Project>

I found the following solution: 我找到了以下解决方案:

The ClockProject requires the MainProject and the TimeProject. ClockProject需要MainProject和TimeProject。

If I reference only the TimeProject in the ClockProject then it works (because the TimeProject references the MainProject), but the MainProject.dll is copied to the ClockProject Output folder, which I do not want. 如果我只引用ClockProject中的TimeProject然后它可以工作(因为TimeProject引用了MainProject),但MainProject.dll被复制到ClockProject Output文件夹,这是我不想要的。

However, if I reference BOTH projects in the ClockProject, the Main- and TimeProject and set both to CopyLocal = false, then only the ClockProject.dll will be copied into the Output folder of the ClockProject. 但是,如果我在ClockProject,Main-和TimeProject中引用BOTH项目并将它们都设置为CopyLocal = false,那么只有ClockProject.dll将被复制到ClockProject的Output文件夹中。

For projects like this I go to each project properties and set a Common Output directory. 对于这样的项目,我转到每个项目属性并设置一个Common Output目录。 That way all of the projects dump their builds into one directory. 这样,所有项目都将其构建转储到一个目录中。

Since that is not possible, in your references select the MainProject.dll reference, look at the properties and set Copy Local to False. 由于这是不可能的,在您的引用中选择MainProject.dll引用,查看属性并将Copy Local设置为False。

You've designed it the wrong way around and that's why you're getting version conflicts. 你以错误的方式设计它,这就是你遇到版本冲突的原因。

You need to redesign the solution so project's have a dependency tree with the right hierarchy: 您需要重新设计解决方案,以便项目具有具有正确层次结构的依赖关系树:

Main Project <- Time Project <- Clock Project 主要项目< - 时间项目< - 时钟项目

To fix your solution: 要修复您的解决方案:

  • Delete all the references in all projects. 删除所有项目中的所有引用。
  • Build ClockProject by itself (or MainProject if that's the confusing way you want to go). 单独构建ClockProject(或MainProject,如果这是你想要的混乱方式)。
  • Open TimeProject, reference Clock Project and build TimeProject (or from TimeProject ref Main and build). 打开TimeProject,引用Clock Project并构建TimeProject(或者从TimeProject ref Main和build)。
  • Open MainProject and reference TimeProject (or from ClockProject reference TimeProject). 打开MainProject并引用TimeProject(或从ClockProject引用TimeProject)。

At each step you may need to refactor things around. 在每一步,您可能需要重构周围的事情。 Referencing Main from every project is a big no-no . 从每个项目引用Main是一个很大的禁忌 You reference small, modular, encapsulated libraries and build up the dependency tree for the MainProject in the solution. 您可以引用小型模块化封装库,并在解决方案中为MainProject构建依赖关系树。

暂无
暂无

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

相关问题 引用.NET Core XUnit项目中的标准dll - Referencing standard dlls from a .NET Core XUnit project 从.Net Standard项目和本机dll创建nuget包 - Create a nuget package from .Net Standard project and Native dlls 将 class 库项目升级到 .net 标准 2.1,其中 .net 框架 4.X 项目已引用该标准 - Upgrading class library project to .net standard 2.1 where this has been referenced by .net framework 4.X projects .NET部署项目不使用但引用的dll需要的dll - .NET deploying dlls that project does not use but referenced dlls require .NET 中另一个项目使用的项目中引用的 DLL - DLLs referenced in a project used by another project in .NET 在.net中的两个项目之间共享dll - Sharing dlls between two projects in .net 在postsharp中的AssemblyLoadException,来自引用的DLL的参数的问题? - AssemblyLoadException in postsharp, problem with arguments from referenced DLLs? .Net 解决方案中的多个项目 - 结合所有项目的构建工件 - Multiple Projects in .Net Solution - Combine Build Artifacts from all projects .Net解决方案中的多个控制台应用程序项目要在一个控制台应用程序项目中引用 - .Net multiple console apps projects in a solution to be a referenced in one console app project 从 .net 核心集成测试项目加载引用的程序集不包括来自测试的 API 的引用项目 - Load referenced assemblies from a .net core Integration Test project doesn't include referenced projects from tested API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM