Expected diamond-depndency conflict if multi-versions referenced and third-party component does not update - does not affect HttpClientFactory [ System.TypeLoadException when referencing both Polly 7 and transitive Polly 6 ]
See original GitHub issueAssuming a solution with the following project/code structure:
PollySeven.MyHttpClient
depends on Microsoft.Extensions.Http.Polly
(which depends on Polly
v6).
PollySeven.MyHttpClient
uses RetryAsync()
extension method to register MyService
.
PollySeven
depends on PollySeven.MyHttpClient
and Polly
v7.
PollySeven
uses MyService
.
PollySeven
public static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddMyServices();
ServiceProvider provider = services.BuildServiceProvider(true);
var service = provider.GetRequiredService<MyService>();
// System.TypeLoadException:
// 'Could not load type 'Polly.RetrySyntaxAsync' from assembly
// 'Polly, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc'.'
}
PollySeven.MyHttpClient
public static IServiceCollection AddMyServices(this IServiceCollection services)
{
services.AddHttpClient<MyService>()
.AddTransientHttpErrorPolicy(p => p.RetryAsync());
return services;
}
When the application runs, I get System.TypeLoadException
.
The root cause of this issue might be similar to #611.
Expected behavior: Code executes successfully.
Actual behaviour:
System.TypeLoadException
is thrown:
Could not load type 'Polly.RetrySyntaxAsync' from assembly
'Polly, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc'.
Steps / Code to reproduce the problem:
Example repo: https://github.com/altso/PollySeven
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Polly policy not working using "AddPolicyHandler"
My idea is to use Polly to retry if the access_token is expired. I'm using Refit (v5.1.67) and Polly (v7.2.1) in a .NET...
Read more >HttpClientFactory in ASP.NET Core 2.1 (Part 4) - Steve Gordon
This post explores how we can easily combine IHttpClientFactory with Polly for transient fault handling in ASP.NET Core 2.1 to apply HTTP ...
Read more >Working with Polly – Using the Context to Obtain the Retry ...
In this post, we'll explore a use for the Polly Context object to share data between our code and the execution of a...
Read more >.NET Core: Use HttpClientFactory and Polly to build rock ...
In this post I will explain what is HttpClientFactory and Polly retry policies and why you should use them in your next project....
Read more >Polly with .NET 6, Part 2 - no dogma blog - Bryan Hogan
This post shows how to use Polly with .NET 6; injecting a HttpClientFactory with a Retry policy into a controller.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the clarification @reisenberger. It sounds like there is no way to mitigate this issue in Polly itself. Option (b) worked for me and hopefully will work for others as well.
@reisenberger thanks for looking into that. The issue arose in a couple of internal nuget packages and we fixed that by adding an explicit reference to Polly v7 in each of them.
I do not think option (a) is a way to go as you will introduce a breaking change between v7 and v8. I also think option (b) is better but not ideal as it requires additional work from other devs who might not be aware of the issue at all.
A colleague of mine suggested another approach. Let’s call it option ©:
© Bring back all the missing types and methods with the same exact signatures as they had in v6, but provide a bridge to v7 implementations. Something like this:
Please note:
Obsolete
andEditorBrowsable(EditorBrowsableState.Never)
to avoid new consumers of this api;this
keyword in front ofPolicyBuilder
to avoid compiler confusion around extension methods with the same name.If not mistaken, that should bring back the binary compatibility between v6 and v7.
Unfortunately, I do not know how to make the code above compile and work properly at the moment - just an idea, but I can take a deeper look if you think it’s worth it. Let me know.
Also, that would be less of an issue if
Microsoft.Extensions.Http.Polly
is updated toPolly
v7. I see the plans to upgrade in v3, but not sure if it’s planned for v2.x as well.