'Build C# project process does not copy dependency assemblies to output folder
I am testing a code targeting .NET standard 2.0, in Visual Studio 2022. The code depends on Polly and Microsoft.Extensions.Http.Polly. The program fails to start with error message :
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Polly, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc' or one of its dependencies. The system cannot find the file specified.
I found that the error occurred because the build project process does not copy the Polly.dll to folder NetRetry\bin\Debug\netstandard2.0. But why are these assemblies not copied to output folder? I can find these assemblies in .nuget\packages folder.
To reproduce this error
Program.cs
using Microsoft.Extensions.Http;
using Polly;
using System;
using System.Net.Http;
using System.Threading;
class Program
{
static void Main(string[] args)
{
var h = Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode != System.Net.HttpStatusCode.OK)
.Or<Exception>()
.RetryAsync(3, (r, count) =>
{
Console.WriteLine($"Log retry {count}");
r.Result.Dispose();
});
var handler = new PolicyHttpMessageHandler(h);
handler.InnerHandler = new HttpClientHandler();
HttpClient c = new HttpClient(handler);
var result = c.GetAsync("https://www.google.com/non-existing", CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
}
}
NetRetry.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.3" />
<PackageReference Include="Polly" Version="7.2.3" />
</ItemGroup>
</Project>
I tried retargeting project to .NET 5.0 and build the project. This time the assemblies are all copied to folder NetRetry\bin\Debug\net5.0, and the program runs normally.
Solution 1:[1]
Manually enable CopyLocalLockFileAssemblies will solve this.
Add to PropertyGroup
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stanleyerror |
