Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 360
medokin Avatar asked Jul 10 '17 09:07

medokin


People also ask

Can I use a NuGet package as a project reference?

Certainly, but there are some restrictions you need to know. First, the ID of the NuGet package should different from the name of the reference project, otherwise, the reference from NuGet will replace the project reference.

Is it possible to create a release NuGet package with debug symbols?

Ideally, what would happen then is that nuget pack SomePackage -Symbols against a release version would create a release nuget package, but a debug symbols package. And the VS plugin would be updated to be smart enough to see the association and pull in the Debug assemblies when running in a debugger and load those instead.

Is there a way to debug DLLs in NuGet?

IF nuget had a way to load debug dlls in debug mode and release dlls in release mode (from nuget packages), this would be the ultimate. Until then we are stuck with Nuget package switcher I concur, this would be a nice product enhancement.

Is it possible to debug and release builds in the same package?

When debugging into a library for diagnostic purposes, you really want a debug build with all the appropriate optimizations disabled. That's what debug builds are for, after all. That would be fine, but NuGet doesn't (as far as I can tell) allow both the release and debug builds to be published in a useful way, in the same package.


2 Answers

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 no change it to:

  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <Reference Include="log4net">
      <HintPath>Debug\log4net.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <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.

like image 72
Marco Stroppel Avatar answered Oct 16 '22 06:10

Marco Stroppel


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?

Certainly, but there are some restrictions you need to know.

First, the ID of the NuGet package should different from the name of the reference project, otherwise, the reference from NuGet will replace the project reference.(For example, TestProjectReferenceForDebug is the name of the project reference, if you want to use project reference and NuGet package at the same time, you could not use this project to create the NuGet package directly, so I created a same project with different name to create the NuGet package "TestNuGetForRelease"):

enter image description here

Second, you should use Condition attribute in the ItemGroup element, otherwise, there is an ambiguous reference between 'TestProjectReferenceForDebug' and 'TestNuGetForRelease', so we need add the Condition attribute in the ItemGroup element

  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
    <Reference Include="TestNuGetForRelease, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL" >
      <HintPath>..\packages\TestNuGetForRelease.1.0.0\lib\net462\TestNuGetForRelease.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
     <ProjectReference Include="..\TestProjectReferenceForDebug\TestProjectReferenceForDebug.csproj">
       <Project>{90424b17-2231-4d7d-997b-608115d9f4d9}</Project>
       <Name>TestProjectReferenceForDebug</Name>
     </ProjectReference>
  </ItemGroup>

Third, after we add the Condition attribute in the ItemGroup element with debug and release, we could use project reference in Debug and Nuget in Release, however, if we use those namespace in one .cs file at same time, we need to add those two namespace, then you will get an error "The referenced component 'xxx' could not be found". That because VS could not find those two namespace only in the "Release" or "Debug" model:

enter image description here

To resolve this error, we have to annotate the namespace which in another configuration model when you change the configuration model from Debug to Release.

enter image description here

like image 3
Leo Liu-MSFT Avatar answered Oct 16 '22 07:10

Leo Liu-MSFT