This post was most recently updated on April 28th, 2022.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)
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!
Let’s get to it, then!
Time needed: 2 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
This to your <PropertyGroup>:
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
… and you should be good!
And that’s it!
How did it go for you? Let me know in the comments section below!
- This was actually neatly documented at Clive’s Vade Mecum 2.0, here:
- Using role claims to target WebSockets - May 24, 2022
- The simplest fixes to “500 (Internal Server Error)” from Azurite - May 17, 2022
- How to solve “Npgsql: 42883: function create_hypertable(…) does not exist”? - May 10, 2022