在csproj文件中使用AssemblySearchPaths

时间:2013-10-11 03:40:40

标签: msbuild dependency-management

我正在尝试设置我的csproj文件,通过添加以下内容来搜索父目录中的依赖项:

<PropertyGroup>
    <AssemblySearchPaths>
       ..\Dependencies\VS2012TestAssemblies\; $(AssemblySearchPaths)
   </AssemblySearchPaths>
</PropertyGroup>

我将它添加为第一个具有所有Reference声明的ItemGroup之前的最后一个PropertyGroup元素。

不幸的是,这导致所有其他引用无法解析,例如:

ResolveAssemblyReferences:
         Primary reference "Microsoft.CSharp".
     9>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(1578,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.CSharp". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. 
For SearchPath "..\Dependencies\VS2012TestAssemblies\".
                 Considered "..\Dependencies\VS2012TestAssemblies\Microsoft.CSharp.winmd", but it didn't exist.
                 Considered "..\Dependencies\VS2012TestAssemblies\Microsoft.CSharp.dll", but it didn't exist.
                 Considered "..\Dependencies\VS2012TestAssemblies\Microsoft.CSharp.exe", but it didn't exist.

有没有一种简单的方法让我告诉msbuild在哪里搜索我的项目的依赖项?我意识到我可以使用/ p:ReferencePath,但是我更喜欢在csproj文件本身中使用编译逻辑而不是让TFS Team Builds指示在哪里查看,更不用说我希望能够在其他地方编译开发者机器。

我确实尝试将$(AssemblySearchPaths)移到列表中的第一位,但这没有帮助。

2 个答案:

答案 0 :(得分:11)

您是否可以在Target“BeforeResolveReferences”中更改“AssemblySearchPaths”属性的值,看看是否能解决您的问题?

    <Target Name="BeforeResolveReferences">
<CreateProperty
    Value="..\Dependencies\VS2012TestAssemblies;$(AssemblySearchPaths)">
    <Output TaskParameter="Value"
        PropertyName="AssemblySearchPaths" />
</CreateProperty>
</Target>

答案 1 :(得分:0)

Seems like there was a fix recently这样也可以:

<PropertyGroup>
  <ReferencePath>MY_PATH;$(ReferencePath)</ReferencePath>
</PropertyGroup>

这将使该文件夹中的程序集也显示在“添加引用...”窗口中:)

Visual Studio - Reference Manager


并且由于您也可能不希望将程序集复制到输出文件夹中,因此这里有一个有关如何实现此目的的示例:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->

  <PropertyGroup>
    <!-- Add paths to ReferencePath. E.g. here it is Unity. -->
    <ReferencePath>C:\Program Files\Unity\Hub\Editor\$(UNITY_VERSION)\Editor\Data\Managed\UnityEngine;$(ReferencePath)</ReferencePath>
  </PropertyGroup>

  <Target Name="DontCopyReferencePath" AfterTargets="ResolveAssemblyReferences">
    <!-- Don't copy files indirectly referenced by ReferencePath -->
    <ItemGroup>
      <!-- Collect paths to allow for batching -->
      <ReferencePaths_ Include="$(ReferencePath)" />
      <!-- Use batching to remove all files which should not be copied. -->
      <ReferenceCopyLocalPaths Remove="@(ReferencePaths_ -> '%(Identity)\*.*')" />
    </ItemGroup>
  </Target>

  <!-- ... -->
</Project>