This post was most recently updated on July 26th, 2024.
2 min read.This is something that comes up pretty often – when you’re building an application, or maybe a library you’re sharing as a NuGet package to your friends: You’ll need to share a dependency or two with your particular package, and you don’t want your end users having to reference those libraries, too.
Sounds easy, right? But alas, it’s not as simple as one would like :)
(Well, unless you’re happy running dotnet publish every time – that should work, but I want to have these files available right after dotnet build instead)
Problem
Sounds easy, right? There’s a problem, though – you can’t just set “Copy Local” to “true” anymore. That’s simply not available for your dependencies.
And forget all of that <IncludeAssets /> nonsense inside your PackageReferences – probably not going to help either.
At least I was getting a bit frustrated. Couldn’t find a way to include the DLLs without building some super questionable build conditions and/or scripts. Or, well, running dotnet publish all the time.
However, there’s another solution available!
Solution
The solution includes modifying your .csproj file (because of course it does!) – let’s see how:
Time needed: 5 minutes
How to copy referenced assemblies to the bin folder on build?
- Open your project file
Simple enough, just select “Edit Project File” by clicking your project using the right mouse button in the Solution Explorer.
- Add CopyLocalLockFileAssemblies to your .csproj file
Okay – so you need to add this inside your <PropertyGroup>…</PropertyGroup:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
This will copy all of the NuGet dependencies to your output directory.
The documentation says this:
If you set this property to true, any NuGet package dependencies are copied to the output directory. That means you can use the output of dotnet build to run your plugin on any machine. - Reload project and rebuild
After reloading and rebuilding you should be good!
And that’s it!
How did it go for you? Let me know in the comments section below!
References
- This was actually neatly documented at Clive’s Vade Mecum 2.0, here:
- Documentation:
- “Performing cleanup” – Excel is stuck with an old, conflicted file and will never recover. - November 12, 2024
- How to add multiple app URIs for your Entra app registration? - November 5, 2024
- How to access Environment Secrets with GitHub Actions? - October 29, 2024