Using NuGet Pre-release versions for local development
Does your company maintain separate source code repositories for shared libraries? For instance, do you have a models library on github that builds and publishes to nuget.org? When you need to make a change to your main project, that uses these nuget packages, which in turn also requires changes to be made to the library, how can you test it all locally without repeatedly publishing multiple versions?
The scenario is this: There are one or more libraries that are published to public or private nuget feeds. The project you are working on makes use of those packages. The task you are doing, requires changes to one or more of those hosted nuget packages. So, you need to update the library and use the new code from the published package.
One way you might do this, is to make the changes to the library, have it reviewed and ultimately built and published to the nuget feed. Then you can update to the new version in your main project and use the new code from the package. This can be fine when making a simple change, like adding one or a few simple properties. But, what if you then again need to make more changes, or some more advanced work in that library that requires testing and validating it works as you want, with your main project? Obviously, this can make it more difficult to support multiple changes and function properly with your main project.
Create a local nuget. This involves adding a nuget feed that points to a local directory on your computer, I won’t cover that fully here. It would however look something like this in your nuget.config file <add key="LocalDev" value="C:\Projects\LocalNuget" />
. Update the version in the libraries .csproj file <Version>1.1.{nextNumber}-alpha</Verson>
. The library project likely already builds a new .nuget file when the solution is built. Copy this file into your LocalNuget folder.
Now that your alpha package has been build, you can upgrade your main projects reference using dotnet add package {myPackageName} --prerelease
this will allow your project to fine and use your -alpha package. for testing update your project, via your local nuget, to use your -alpha version.
Now you can happily continue on your way developing your main project without publishing multiple nuget packages out into the world! If along the way you find that you need more work in the library project, do so, and just increment the prerelease marker to -beta and so on. NuGet gives preference in reverse alphabetical order, so if you have an alpha and a beta, it will use beta.
Once work is complete and tested, you can simply remove the prerelease marker from your version number, send your library code for PR. You can wait until that process is finished to update your main project, or update the package versions and send it out for its PR as well!
I hope you found this helpful! Happy coding!
Thank you!!1