Search code examples
c#.netvisual-studionuget

Use Project Reference in Debug and Nuget in Release


I would like to work in my project (A) and a dependent Nuget package (B) at the same time, without the need to release the nuget package on each change.

Is it possible to do a project-reference the Nuget project (B) from the Solution (A) when building Debug. And when building Release use the Nuget package from Source?


Solution

  • One way is to manually edit the csproj file. If you have currently referenced the NuGet package, you will have a part in the csproj file like this:

    ....
    <ItemGroup>
      <Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
        <HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
        <Private>True</Private>
      </Reference>
      <Reference Include="System" />
      <Reference Include="System.Core" />
      <Reference Include="System.Xml.Linq" />
      <Reference Include="System.Data.DataSetExtensions" />
      <Reference Include="Microsoft.CSharp" />
      <Reference Include="System.Data" />
      <Reference Include="System.Xml" />
    </ItemGroup>
    ....
    

    In this example, log4net is used. For your NuGet package, the public key token, version and so on is different. You can now change it to:

      <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
        <Reference Include="log4net">
          <HintPath>Debug\log4net.dll</HintPath>
          <Private>True</Private>
        </Reference>
      </ItemGroup>
      <ItemGroup Condition=" '$(Configuration)' == 'Release' ">
        <Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
          <HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
          <Private>True</Private>
        </Reference>
      </ItemGroup>
    

    The Condition attribute in the ItemGroup element is doing the job between debug and release.