JohnZaj
JohnZaj

Reputation: 3230

using AssemblySearchPaths in csproj files

I am trying to set up my csproj files to search for dependencies in a parent directory by adding:

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

I added this as the last PropertyGroup element right before the first ItemGroup which has all of the Reference declarations.

Unfortunately this is causing all of the other references to fail to resolve, for example:

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.

Is there a simple way for me to tell msbuild to where to search for my project's dependencies? I realize I can use /p:ReferencePath, however I prefer to have compilation logic in the csproj files themselves rather than have TFS Team Builds dictate where to look, not to mention that I'd like this to be able to be compiled on other developers machines.

I did try moving $(AssemblySearchPaths) to be first in list, but that did not help.

Upvotes: 12

Views: 9231

Answers (2)

FooBarTheLittle
FooBarTheLittle

Reputation: 459

Seems like there was a fix recently Thus this works as well:

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

This makes the assemblies in that folder to also show up in the "Add References..." window :)

Visual Studio - Reference Manager


And since you also might not want the assemblies to be copied into the output-folder, here an example on how to achieve this:

<?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>

Upvotes: 1

Isaiah4110
Isaiah4110

Reputation: 10100

Can you change the value of the "AssemblySearchPaths" property within the Target "BeforeResolveReferences" and see if that solves your issue?

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

Upvotes: 20

Related Questions