How to temporarily replace a NuGet reference with a local build

We've had great success using this tool. It's easy to use and works.

VS 2019 https://marketplace.visualstudio.com/items?itemName=RicoSuter.NuGetReferenceSwitcherforVisualStudio2019

VS 2017 https://marketplace.visualstudio.com/items?itemName=RicoSuter.NuGetReferenceSwitcherforVisualStudio2017

VS 2015 https://marketplace.visualstudio.com/items?itemName=RicoSuter.NuGetReferenceSwitcherforVisualStudio2015


You can create your own Nuget feed (simple local folder + some configurations)

Read more here Hosting Your Own NuGet Feeds


I found the following workaround useful for me:

First I disable "NuGet Package Restore" from the context menu of the Solution.

After that I go to the %HOMEPATH%\.nuget\packages folder, and search for the package I want to replace. From this package I take the version number, and use this exact version number to build the dll I want to exchange.

After that I can exchange the dll in the packages folder with this newly built dll. Building the project now uses this new dll.

After having this set up once, I can easily build new dlls, and copy them to the packages folder.


In VS2019, this is how you can conditionally switch on build between project reference and package reference. We use this for our docker containers

  1. Open your project solution "Solutions\Bookstore\Bookstore.sln" that has the project "Bookstore.csproj" that will reference the nuget

  2. Add a solution folder for example "nuget" and add existing project "Solutions\Library\library.csproj"

  3. Add conditional item group to the project file "Bookstore.csproj"

    <ItemGroup Condition="Exists('..\..\Library\library.csproj')">
      <ProjectReference Include="..\..\Library\library.csproj" />
    </ItemGroup>
    <ItemGroup Condition="!Exists('..\..\Library\library.csproj')">
      <PackageReference Include="library" Version="1.0.0" />
    </ItemGroup>
    
  4. In solution explorer, right click Bookstore solution > properties > configuration properties. For the configuration 'Release' uncheck the project 'library.csproj'. Now when your code is built in debug, it will build the project reference. When it is built in release it won't try to build it.

  5. When we publish make sure you use 'Release'. This works because when you publish it will not be able to find "Solutions\Library\library.csproj" from the path we configured in Bookstore.csproj "..\..\Library\library.csproj".

  6. You don't have to use docker for this to work but as an example here is the commands our dockerfile will run

    WORKDIR "/Solutions/Bookstore"
    RUN dotnet restore
    RUN dotnet build -c Release -o /app
    FROM build AS publish
    RUN dotnet publish "Bookstore.csproj" -c Release -o /app
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app .
    

Tags:

C#

Nuget