在开发中轻松覆盖NuGet DLL(VS 2015)

时间:2015-10-26 19:23:38

标签: dll msbuild nuget visual-studio-2015 nuget-package

我们在VS 2015中有3个包含C#项目的解决方案。其中一个创建一个NuGet包,另一个引用该NuGet包中的DLL。 (DLL包含应用程序两个部分之间的接口。)TeamCity构建服务器构建DLL和NuGet包,自动设置其版本。

在开发中,我希望能够轻松测试DLL的更改,而无需提交它们并等待构建包。最好的方法是什么?理想情况下,我想说:如果存在C:\Build\My.dll,则无论其版本如何都使用它,否则请使用version 1.2.3.4 MyDLL NuGet package。有没有办法指定并让它“幸存”NuGet升级?

我尝试暂时删除对该DLL的引用并直接添加引用到我想要的版本,但问题是它在许多项目中被引用并且对所有项目执行此操作非常麻烦。

2 个答案:

答案 0 :(得分:1)

看起来可以通过删除现有的NuGet引用在BeforeBuild步骤中完成。这样,NuGet引用项本身不变,可以在升级期间由NuGet自由替换,但在构建期间,我会使用我的DLL版本(如果存在)。

无论版本如何,删除引用也需要轻微的修改。

  <Target Name="BeforeBuild">
    <!-- Override NuGet package with a local copy in dev, if available -->
    <PropertyGroup>
        <ReplacementLocalDllPath>Some\Path\My.dll</ReplacementLocalDllPath>
    </PropertyGroup>
    <ItemGroup Condition="Exists($(ReplacementLocalDllPath))">
      <!-- Remove any version of the DLL to be replaced -->
      <ReferenceToBeRemoved Include="@(Reference)" Condition="$([System.String]::Copy(&quot;%(Reference.Filename)&quot;).StartsWith('My'))" />
      <Reference Remove="@(ReferenceToBeRemoved)" />
      <!-- Add the local DLL, ignoring its version -->
      <Reference Include="My, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
        <SpecificVersion>False</SpecificVersion>
        <HintPath>$(ReplacementLocalDllPath)</HintPath>
      </Reference>
    </ItemGroup>
  </Target>

答案 1 :(得分:0)

您可以在csproj文件中使用条件引用来实现此目的。有关详细信息,请查看此MSDN链接:MSBuild Conditional Constructs