Skip to content

[Bug]: This resilience pipeline has been disposed and cannot be used anymore #2577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aseem-shaikh opened this issue Apr 9, 2025 · 3 comments
Labels

Comments

@aseem-shaikh
Copy link

Describe the bug

services.AddResiliencePipeline<string, IApiResponse>(DataProvider.ResiliencePipelineName, (builder, context) =>
{
var logger = context.ServiceProvider.GetRequiredService<ILogger>();
builder.AddRetry(new ()
{
ShouldHandle = new PredicateBuilder()
.HandleResult(static response =>
{
return (int)response.StatusCode >= 500;
}),
MaxRetryAttempts = 2,
DelayGenerator = static args =>
{
var delay = args.AttemptNumber switch
{
0 => TimeSpan.FromSeconds(10),
_ => TimeSpan.FromMinutes(2)
};
return new ValueTask<TimeSpan?>(delay);
},
OnRetry = args =>
{
logger.LogWarning("Retry attempt: {attemptNumber} for {url}", args.AttemptNumber, args.Outcome.Result?.Error?.Uri);
return default;
},

 });

});

and injecting like
public DataProvider(
ILogger logger,
[FromKeyedServices(ResiliencePipelineName)] ResiliencePipeline apiRetryPipeline)
and getting an exception This resilience pipeline has been disposed and cannot be used anymore
where DataProvider DI as singleton . How should I control lifetime resilience pipeline for singleton or any other way to resolved above exception

Expected behavior

No response

Actual behavior

No response

Steps to reproduce

No response

Exception(s) (if any)

No response

Polly version

No response

.NET Version

No response

Anything else?

No response

@martincostello
Copy link
Member

This looks similar to #2019 (comment).

It's hard to make guesses at what the problem might be from the small snippet you've provided as there's no detail about how you setup your DI container.

If the comments in the attached issue don't help, please provide a minimal reproducible example as a GitHub repository that demonstrates the issue you're experiencing so we can look into this further.

@aseem-shaikh
Copy link
Author

Image

@martincostello

I am registering DI like below
private static void AddResiliencePipelines(this IServiceCollection services)
{
services.AddResiliencePipeline<string, IApiResponse>(DataProvider.ResiliencePipelineName, (builder, context) =>
{
var logger = context.ServiceProvider.GetRequiredService<ILogger>();
builder.AddRetry(new ()
{
ShouldHandle = new PredicateBuilder()
.HandleResult(static response =>
{
return (int)response.StatusCode >= 500;
}),
MaxRetryAttempts = 2,
DelayGenerator = static args =>
{
var delay = args.AttemptNumber switch
{
0 => TimeSpan.FromSeconds(10),
_ => TimeSpan.FromMinutes(2)
};
return new ValueTask<TimeSpan?>(delay);
},
OnRetry = args =>
{
logger.LogWarning("Retry attempt: {attemptNumber} for {url}", args.AttemptNumber, args.Outcome.Result?.Error?.Uri);
return default;
},
});
});
}

And in my class I am injecting like

public const string ResiliencePipelineName = "data-provider-resilience-pipeline";

public DataProvider(
ILogger logger,
[FromKeyedServices(ResiliencePipelineName)] ResiliencePipeline apiRetryPipeline)

and calling like below (using Refit to calling API)

var response = await _apiRetryPipeline.ExecuteAsync(async token => await orderManagementApi.GetCargoStuffing(cargoStuffingNumber));

Is there way I can control lifecycle explicitly as Singleton while registering DI

@martincostello
Copy link
Member

You might find this project useful to look at for hints - it uses Polly and Refit: https://github.com/martincostello/polly-sandbox

Thanks for the additional code, but it's not something we can just clone and debug (and it's hard to read when you don't use codefences), which is why I asked for a minimal reproducible example as a GitHub repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants