13

I am developing a private test harness that is split between a core library with a couple of extension plugins.

The extension projects depend on a core library via a private MyGet feed so i can just click pack and upload to the MyGet feed without much hassle. The issue i'm having is that any API update to the core lib isn't reflected on the extension libs unless i issue a completely new version of the core NuGet package.

That in itself isn't so bad but it has two productivity-killer implications: I can't debug core code in an extension code context and making my development iteration process dependant on MyGet is a huge slowdown.

How can i fix this? I've been manually switching between project and NuGet dependencies every time i want to generate new NuGet packages but i'm pretty sure someone else must have a better solution to this problem.

1

4 Answers 4

11

I've been using NuGet Reference Switcher to do this - it's a Visual Studio extension to handle exactly this problem:

The workflow using this is still not perfect, but it's better than manually switching projects. I'd love to hear other ideas though.

3
  • 4
    I ended up using conditional references based on whether the referenced project was reachable or not, using the package reference in the latter case. This suited my need to package my app on docker containers effectively Commented Feb 22, 2018 at 22:49
  • 5
    @Machinarus can you provide an example of that? Commented Aug 9, 2018 at 13:32
  • Note: Someone else even modified the reference switcher to VS2022. github.com/Schnazey/NuGetReferenceSwitcher
    – Anoni2
    Commented yesterday
6
  1. Add library via nuget
  2. Add the same library via project reference
  3. Edit project file and add conditions:
<ItemGroup>
<PackageReference Include="Library.Common" Version="3.0.1" Condition="'$(Configuration)'=='Release'"/>  
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Library.Common\Library.Common\Library.Common.csproj" Condition="'$(Configuration)'=='Debug'"/>
</ItemGroup>
  1. Use appropriate configuration for debugging with project reference and releasing with nuget reference
3
  • 2
    I had a lot of problems with this, there's some glitch in VS2022 when you switch between Debug to Release (and you're using a type of NuGet repo) it doesn't do the switch and results in compile errors. The NuGet Switcher gets my vote. Commented Apr 5, 2023 at 5:30
  • 1
    Pure csproj hacks get my vote. Introducing some non-Microsoft supported build tool is asking for trouble. Sure you don't want all your packages to get deleted? Microsoft, please add native support for this!
    – l33t
    Commented Apr 24, 2023 at 13:48
  • This idea looks nice, but is horrible once implemented: Usually you would use nuget package for external repositories (otherwise you would have used the project reference anyway ;) ). With this way you are required to have your projects within your .sln file configuration. As soon as you haven't checked out your external repo, your solution won't build (on CI). You need to create another build config in your solution, disable all projects to replace for each build config and keep a <configuration> entry in each .csproj of your solution, that mentions this new debug-configuration as well.
    – Anoni2
    Commented yesterday
1

So fed up with the situation I decided to make a tool to do it, sponsor me GitHub star if you like it: https://github.com/MeaningOfLights/NugetDebugSwitcher

enter image description here

Say you have a Solution with ProjectA that references a Library.Base DLL. In the ProjectA.csproj file we can see the reference:

<PackageReference Include="Library.Base" Version="2.1.0" />

What this tool will do is backup the Nuget Library.Base Package Lib folder. Then it creates a SymLink so the Nuget Library.Base Package Lib folder is a shortcut to the Project Reference Debug folder.

After simple clean of the solution you can then debug into the Library.Base. Hey presto!

Click the Remove Symlinks button to restore the backups and use the real NuGet DLLs. Code contributions welcome!

It's based off a Command Line Utility NuLink.

0

Those package switcher add-ons did not work, and the previous top-solution forced to use the package upon debug and have the project always within the solution available. I would like to document my solution hereby:

  1. Define a script that depends on a variable.

<PropertyGroup>
  <!-- Default: False. Switch requires execution of script -->
  <UseProject>true</UseProject>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Library.Common" Version="3.0.1" Condition="'$(UseProject)'=='false'" />
</ItemGroup>

<ItemGroup>
  <ProjectReference Include="..\..\Library.Common\Library.Common\Library.Common.csproj" Condition="'$(UseProject)'=='true'" />
</ItemGroup>

  1. Add two .ps1 scripts to add/remove your project dynamically.

    dotnet sln MySolution.sln add -s FromNugetPackage ....\Library.Common\Library.Common\Library.Common.csproj

    dotnet sln MySolution.sln add -s FromNugetPackage ....\Library.Common\Library.Common\Library.Common.csproj

To switch the packages, you still need to redefine the variable and run one of the two scipts, but I think that is the best solution yet. You can clearly optimize this, to switch the variable of course, but that's something I'll skip for now.

1
  • I struggled with the markup. If one could improve this, that would be great, thanks.
    – Anoni2
    Commented yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.