Visual Studio 2010 - 如何强制项目引用使用精确路径NOT GAC或Program Files?

时间:2023-01-06 07:31:11

We're forever having this problem, we have a number of solutions and an adjacent /Components/ folder. All the DLLs we want to reference are in this folder. Some of them we've built from source to use a specific version number that only existins in the Components binary but when a user on a different machine gets-latest of everything from TFS and so has the exact on disk structure Visual Studio STILL changes the references to ones that are installed in Program Files, the GAC or elsewhere.

我们永远遇到这个问题,我们有许多解决方案和相邻的/ Components /文件夹。我们要引用的所有DLL都在此文件夹中。其中一些我们从源代码构建使用只存在于Components二进制文件中的特定版本号,但是当另一台机器上的用户获取TFS中的所有内容时,因此具有精确的磁盘结构Visual Studio STILL会更改对程序文件,GAC或其他地方安装的引用的引用。

Have tried manually editing the proj file to include HintPath, e.g.

尝试手动编辑proj文件以包含HintPath,例如

<Reference Include="Foo, Version=5.5.5.5, Culture=neutral, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\Components\Foo.dll</HintPath>
</Reference>

to no avail. How do we FORCE visual studio to respect this path?

无济于事。我们如何强制视觉工作室尊重这条道路?

3 个解决方案

#1


6  

Setting "SpecificVersion" to true, in addition to specifying the "hintPath" seemed like a solution because it "prevents visual studio from using multiple target rules for assembly resolution".

除了指定“hintPath”之外,将“SpecificVersion”设置为true似乎是一种解决方案,因为它“阻止了visual studio使用多个目标规则进行程序集解析”。

However, once foo.dll is unavailable (while building or loading a project) Visual Studio magic kicks in and changes the assembly target path to the nearest 'matching' assembly.

但是,一旦foo.dll不可用(在构建或加载项目时),Visual Studio魔术就会启动并将组装目标路径更改为最近的“匹配”程序集。

After this point, it doesn't matter whether the original foo.dll is restored to its location (at the target pathname) or even CHANGED! - Visual studio still refers to its newly found match. This is very undesirable.

在此之后,原始foo.dll是否恢复到其位置(在目标路径名处)或甚至是已更改都无关紧要! - Visual Studio仍然指其新发现的匹配。这是非常不受欢迎的。

Possible solutions:

  1. Strong Name foo.dll, but then foo.dll may only reference other strong named assemblies (often undesirable).

    强名称foo.dll,但是foo.dll可能只引用其他强名称程序集(通常是不合需要的)。

  2. Customize assembly resolution by registering for an event in the parent application. This allows you to define exactly where to find the target assembly at runtime - but this seems like far too much effort to resolve this simple problem.

    通过在父应用程序中注册事件来自定义程序集分辨率。这允许您准确定义在运行时找到目标程序集的位置 - 但这似乎是解决这个简单问题的过多努力。

My work around for this problem (easily) was to set LOCAL COPY to FALSE, and add a post build step to the project, which manually copies the target assembly to the target bin folder. The bad part about this is the amount of duplication (and decoupling) created between the post-build step and the References configuration of the project.

我解决这个问题(很容易)就是将LOCAL COPY设置为FALSE,并在项目中添加一个后期构建步骤,该步骤手动将目标程序集复制到目标bin文件夹。关于这一点的不好的部分是在构建后步骤和项目的References配置之间创建的重复(和解耦)的数量。

Please Microsoft - add an option to the Reference property page that that will prioritize the hintPath (which we explicitly specify) over the surprise magic path... or at the very least, throw a warning/error if the two differ from one another!

请微软 - 在Reference属性页面添加一个选项,该选项将优先考虑hintPath(我们明确指定)在惊人的魔术路径上...或者至少,如果两者彼此不同,则抛出警告/错误!

#2


1  

We have the same setup - we reference 3rd party assemblies (and some of our own) in a separate folder, and hints in a .csproj file work just fine for us.

我们有相同的设置 - 我们在一个单独的文件夹中引用第三方程序集(以及我们自己的一些程序集),并且.csproj文件中的提示对我们来说效果很好。

Visual Studio will first try to find the dll in the HintPath and only if it doesn't find it, then it looks further - the binaries next to the executing assembly, solution folder, or the GAC. Also, remember that the Reference Paths tab in the project properties is treated the same way as HintPath, and that the listed order of folder hints matters.

Visual Studio将首先尝试在HintPath中找到dll,并且只有在找不到它时,它才能进一步查看 - 执行程序集,解决方案文件夹或GAC旁边的二进制文件。此外,请记住,项目属性中的“引用路径”选项卡的处理方式与HintPath相同,并且列出的文件夹提示顺序很重要。

If this is still bugging you, maybe you should think about raising your own local NuGet server (which supposedly isn't all that complicated), from which the assemblies will get collected automatically on solution load. It's a bit of an overkill, but it also handles the issues with multiple versions of dll's being used across different solutions. We still haven't done this so I can't recommend it first-hand, but it's on our to-do list.

如果这仍然困扰着你,也许你应该考虑提高你自己的本地NuGet服务器(据说这并不是那么复杂),在解决方案加载时会自动收集程序集。这有点过分,但它也处理了在不同解决方案中使用的多个dll版本的问题。我们还没有这样做,所以我不能直接推荐它,但它在我们的待办事项清单上。

#3


1  

The hintpath can sometimes be a bit difficult. Visual Studio isn't as intelligent as we need it to be sometimes and that's why we can edit the csproj file in XML. The way to do this is by doing the following:

提示路径有时可能有点困难。 Visual Studio不像我们有时需要它那样聪明,这就是我们可以用XML编辑csproj文件的原因。执行此操作的方法是执行以下操作:

1.) Right-Click the project in solution explorer and select "Unload project". // This will make the project inactive in solution explorer.

1.)右键单击解决方案资源管理器中的项目,然后选择“卸载项目”。 //这将使项目在解决方案资源管理器中处于非活动状态

2.) Right-Click the project again, while it is inactive, and select "Edit YourProject.ext" where ext is either vbproj or csproj. This will open an XML editor in Visual studio. //If you need to search within the file, you can press Ctrl-F, but remember you will have to adjust the "Look in:" to be "Current document", otherwise it will not be searched.

2.)再次右键单击项目,当它处于非活动状态时,选择“Edit YourProject.ext”,其中ext是vbproj或csproj。这将在Visual Studio中打开XML编辑器。 //如果您需要在文件中搜索,可以按Ctrl-F,但请记住您必须将“查找范围:”调整为“当前文档”,否则将无法搜索。

3.) When viewing/editing has been completed and changes saved, you close the XML file, Right-Click the project again and select "Reload project".

3.)查看/编辑完成并保存更改后,关闭XML文件,再次右键单击项目并选择“重新加载项目”。

I paraphrased these steps that I found from here: http://wiki.visualwebgui.com/pages/index.php/Visual_Studio_HintPath#Viewing_or_editing_the_project_file

我解释了从这里找到的这些步骤:http://wiki.visualwebgui.com/pages/index.php/Visual_Studio_HintPath#Viewing_or_editing_the_project_file

I hope this helps, it has helped me.

我希望这有帮助,它帮助了我。

#1


6  

Setting "SpecificVersion" to true, in addition to specifying the "hintPath" seemed like a solution because it "prevents visual studio from using multiple target rules for assembly resolution".

除了指定“hintPath”之外,将“SpecificVersion”设置为true似乎是一种解决方案,因为它“阻止了visual studio使用多个目标规则进行程序集解析”。

However, once foo.dll is unavailable (while building or loading a project) Visual Studio magic kicks in and changes the assembly target path to the nearest 'matching' assembly.

但是,一旦foo.dll不可用(在构建或加载项目时),Visual Studio魔术就会启动并将组装目标路径更改为最近的“匹配”程序集。

After this point, it doesn't matter whether the original foo.dll is restored to its location (at the target pathname) or even CHANGED! - Visual studio still refers to its newly found match. This is very undesirable.

在此之后,原始foo.dll是否恢复到其位置(在目标路径名处)或甚至是已更改都无关紧要! - Visual Studio仍然指其新发现的匹配。这是非常不受欢迎的。

Possible solutions:

  1. Strong Name foo.dll, but then foo.dll may only reference other strong named assemblies (often undesirable).

    强名称foo.dll,但是foo.dll可能只引用其他强名称程序集(通常是不合需要的)。

  2. Customize assembly resolution by registering for an event in the parent application. This allows you to define exactly where to find the target assembly at runtime - but this seems like far too much effort to resolve this simple problem.

    通过在父应用程序中注册事件来自定义程序集分辨率。这允许您准确定义在运行时找到目标程序集的位置 - 但这似乎是解决这个简单问题的过多努力。

My work around for this problem (easily) was to set LOCAL COPY to FALSE, and add a post build step to the project, which manually copies the target assembly to the target bin folder. The bad part about this is the amount of duplication (and decoupling) created between the post-build step and the References configuration of the project.

我解决这个问题(很容易)就是将LOCAL COPY设置为FALSE,并在项目中添加一个后期构建步骤,该步骤手动将目标程序集复制到目标bin文件夹。关于这一点的不好的部分是在构建后步骤和项目的References配置之间创建的重复(和解耦)的数量。

Please Microsoft - add an option to the Reference property page that that will prioritize the hintPath (which we explicitly specify) over the surprise magic path... or at the very least, throw a warning/error if the two differ from one another!

请微软 - 在Reference属性页面添加一个选项,该选项将优先考虑hintPath(我们明确指定)在惊人的魔术路径上...或者至少,如果两者彼此不同,则抛出警告/错误!

#2


1  

We have the same setup - we reference 3rd party assemblies (and some of our own) in a separate folder, and hints in a .csproj file work just fine for us.

我们有相同的设置 - 我们在一个单独的文件夹中引用第三方程序集(以及我们自己的一些程序集),并且.csproj文件中的提示对我们来说效果很好。

Visual Studio will first try to find the dll in the HintPath and only if it doesn't find it, then it looks further - the binaries next to the executing assembly, solution folder, or the GAC. Also, remember that the Reference Paths tab in the project properties is treated the same way as HintPath, and that the listed order of folder hints matters.

Visual Studio将首先尝试在HintPath中找到dll,并且只有在找不到它时,它才能进一步查看 - 执行程序集,解决方案文件夹或GAC旁边的二进制文件。此外,请记住,项目属性中的“引用路径”选项卡的处理方式与HintPath相同,并且列出的文件夹提示顺序很重要。

If this is still bugging you, maybe you should think about raising your own local NuGet server (which supposedly isn't all that complicated), from which the assemblies will get collected automatically on solution load. It's a bit of an overkill, but it also handles the issues with multiple versions of dll's being used across different solutions. We still haven't done this so I can't recommend it first-hand, but it's on our to-do list.

如果这仍然困扰着你,也许你应该考虑提高你自己的本地NuGet服务器(据说这并不是那么复杂),在解决方案加载时会自动收集程序集。这有点过分,但它也处理了在不同解决方案中使用的多个dll版本的问题。我们还没有这样做,所以我不能直接推荐它,但它在我们的待办事项清单上。

#3


1  

The hintpath can sometimes be a bit difficult. Visual Studio isn't as intelligent as we need it to be sometimes and that's why we can edit the csproj file in XML. The way to do this is by doing the following:

提示路径有时可能有点困难。 Visual Studio不像我们有时需要它那样聪明,这就是我们可以用XML编辑csproj文件的原因。执行此操作的方法是执行以下操作:

1.) Right-Click the project in solution explorer and select "Unload project". // This will make the project inactive in solution explorer.

1.)右键单击解决方案资源管理器中的项目,然后选择“卸载项目”。 //这将使项目在解决方案资源管理器中处于非活动状态

2.) Right-Click the project again, while it is inactive, and select "Edit YourProject.ext" where ext is either vbproj or csproj. This will open an XML editor in Visual studio. //If you need to search within the file, you can press Ctrl-F, but remember you will have to adjust the "Look in:" to be "Current document", otherwise it will not be searched.

2.)再次右键单击项目,当它处于非活动状态时,选择“Edit YourProject.ext”,其中ext是vbproj或csproj。这将在Visual Studio中打开XML编辑器。 //如果您需要在文件中搜索,可以按Ctrl-F,但请记住您必须将“查找范围:”调整为“当前文档”,否则将无法搜索。

3.) When viewing/editing has been completed and changes saved, you close the XML file, Right-Click the project again and select "Reload project".

3.)查看/编辑完成并保存更改后,关闭XML文件,再次右键单击项目并选择“重新加载项目”。

I paraphrased these steps that I found from here: http://wiki.visualwebgui.com/pages/index.php/Visual_Studio_HintPath#Viewing_or_editing_the_project_file

我解释了从这里找到的这些步骤:http://wiki.visualwebgui.com/pages/index.php/Visual_Studio_HintPath#Viewing_or_editing_the_project_file

I hope this helps, it has helped me.

我希望这有帮助,它帮助了我。