简体   繁体   English

检查解决方案中的二进制引用

[英]Check binary references in a solution

I'm looking for a way to detect problems with assembly references in a large Visual Studio solution: 我正在寻找一种方法来检测大型Visual Studio解决方案中的程序集引用问题:

  • Binary references to bad locations, like a path not in source control or in the output of another project 对错误位置的二进制引用,例如不在源代码管理或另一个项目的输出中的路径
  • Binary references to multiple versions of the same assembly across projects in the solution 解决方案中跨项目的对同一装配体多个版本的二进制引用
  • Binary references without a path, that may be redirected to the GAC 没有路径的二进制引用,可以将其重定向到GAC
  • Binary references that should have been project references 二进制引用应该是项目引用

The whole story 整个故事

I work on a large C# project with almost at 200 projects. 我从事的大型C#项目涉及将近200个项目。 One of the problems that is creeping in over time is that references to assemblies are added but not always to the same version or to the correct location. 随着时间的流逝,存在的问题之一是添加了对程序集的引用,但并不总是引用相同的版本或正确的位置。

For example, a project may get a reference to System.Web.Mvc without a hint path, making it reference what ever version is in the GAC. 例如,一个项目可能没有提示路径就获得了对System.Web.Mvc的引用,从而使其引用了GAC中的任何版本。 Visual Studio (and Resharper) will also offer to add a missing reference but may do so by adding a reference to the output folder of another project. Visual Studio(和Resharper)还将提供添加丢失的引用的方法,但是可以通过将引用添加到另一个项目的输出文件夹来实现。

Now the recent Windows Update catastrophy left some team members dead in the water, unable to build the solution. 现在,最近的Windows Update灾难性事件使一些团队成员陷入水中,无法建立解决方案。 As you can imagine, this bumped up the priority of assembly reference management for us. 可以想象,这为我们提高了装配参考管理的优先级。

To detect some of the most obvious problems I've already setup an msbuild file that can be included in every csproj file and will detect bad references. 为了检测一些最明显的问题,我已经设置了一个msbuild文件,该文件可以包含在每个csproj文件中,并将检测错误的引用。

However, new project files will need to be edited manually to include that script. 但是,新项目文件将需要手动编辑以包括该脚本。 So that will inevitably be forgotten. 因此,这将不可避免地被遗忘。

What I would really like is to check all project files in a solution for 'bad' references during the continuous build, so that all projects will be checked always. 我真正想要的是在连续构建过程中检查解决方案中的所有项目文件是否存在“错误”引用,以便始终检查所有项目。

I've been googling for a solution like this for some time and found lots of static analysis and code analysis tools but nothing to analyze project files in a solution. 我一直在寻找类似的解决方案已有一段时间了,发现了很多静态分析和代码分析工具,但是没有什么可以分析解决方案中的项目文件。

So, before I go off and roll my own solution, is there a way to do this already? 因此,在我提出自己的解决方案之前,已经有办法做到这一点吗?

Update 更新资料

In order to clean up the code base I've created a bit of ScriptCS code that'll scan all csproj files for referenced to assemblies in Nuget packages and fix them. 为了清理代码库,我创建了一些ScriptCS代码,它将扫描所有csproj文件以引用Nuget程序包中的程序集并进行修复。 It's up on GitHub . 在GitHub上

You can create a NuGet package where the sole purpose is incorporating a custom .targets file into a project. 您可以创建一个NuGet包,其唯一目的是将自定义.targets文件合并到项目中。 I recently used this strategy to solve another problem (error messages for missing .snk files). 我最近使用此策略解决了另一个问题(缺少.snk文件的错误消息)。

If you create a similar package, it's easy to right click on your solution node and verify that it is installed in all of your C# projects. 如果创建类似的程序包,则很容易在解决方案节点上单击鼠标右键,并确认该程序包已安装在所有C#项目中。

If your analysis is more complicated and requires the use of an assembly (custom build tasks) in addition to the .targets file, you can use an approach like I use for the Antlr4 NuGet package, which contains build tasks, resources, and custom .props and .targets files, but no actual assemblies that are referenced by the project it gets installed in. 如果您的分析更加复杂,并且除了.targets文件之外还需要使用程序集(自定义构建任务),则可以使用与Antlr4 NuGet包类似的方法,其中包含构建任务,资源和custom 。 props.targets文件,但没有安装该项目所引用的实际程序集。

Instead of adding it to all projects in your solution, why not create some kind of test (unit test, build file, whatever) that can take a project file as input, parse it, and throw an error if OE or more references are incorrect. 与其将其添加到解决方案中的所有项目中,不如创建某种测试(单元测试,构建文件等),以将项目文件作为输入,对其进行分析,如果OE或更多引用不正确,则引发错误。 Much easier than adding (and checking out, committing etc) custom build steps to project files. 比向项目文件添加(以及检出,提交等)自定义构建步骤要容易得多。

Even if you would use a nuget package as proposed earlier, you'd still have to check manually whether all your projects (200 projects? Really?) Reference the package. 即使您使用了先前提出的nuget程序包,您仍然必须手动检查是否所有项目(200个项目?真的吗?)都引用了该程序包。

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

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