Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A How to define Polly policies when working with Refit library in ASP.NET Core?

Note: this is based on my question on SO and subsequent activity there I am trying to add resilience using Polly to HTTP calls that are performed using Refit library. I am using the Refit factory...

1 answer  ·  posted 2y ago by Alexei‭  ·  last activity 2y ago by Alexei‭

#1: Initial revision by user avatar Alexei‭ · 2022-10-20T10:49:40Z (about 2 years ago)
How to define Polly policies when working with Refit library in ASP.NET Core?
Note: this is based on my question on SO and subsequent activity [there](https://stackoverflow.com/q/74055782/2780791)

I am trying to add resilience using [Polly ](https://github.com/App-vNext/Polly)to HTTP calls that are performed using [Refit library](https://github.com/reactiveui/refit).

I am using the Refit factory method `RestService.For<>` and I have a working code, but I feel it is way more complex than it should be:

### The refit interface

```c#
[Headers("Authorization: Bearer")]
public interface IBarIntegration
{
	[Get("/api/ext/Foo/GetFooBriefInfo")]
	Task<ApiResponse<GetFooBriefInfoForFooDto>> GetFooBriefInfo(GetFooBriefInfoForFooInputDto inputData);
}
```

### Factory class that also configures the authentication

```c#
internal sealed class BarApiClientHelper : IBarApiClientHelper
{
	private readonly IOptionsSnapshot<BarApiSettings> _BarSettings;
	private readonly IAccessTokenHelperService _accessTokenHelperService;

	public BarApiClientHelper(IOptionsSnapshot<BarApiSettings> BarSettings, IAccessTokenHelperService accessTokenHelperService)
	{
		_BarSettings = BarSettings;
		_accessTokenHelperService = accessTokenHelperService;
	}

	public async Task<TApiClient> CreateApiClient<TApiClient>(CancellationToken token)
	{
		string baseUrl = _BarSettings.Value.BaseUrl;
		string accessToken = await _accessTokenHelperService.GetAccessToken(token);

		var refitClient = RestService.For<TApiClient>(baseUrl, new RefitSettings
		{
			AuthorizationHeaderValueGetter = () => Task.FromResult(accessToken)
		});

		return refitClient;
	}
}
```

### Example call

```c#
public async Task<GetFooBriefInfoForFooDto> GetFooBriefInfo(string upn, CancellationToken token)
{
	var apiClient = await _clientHelper.CreateApiClient<IBarIntegration>(token);
	var retrialPolicy = _registry.Get<AsyncRetryPolicy<IApiResponse>>(DefaultRetrialPolicy);

	var func = async () => (IApiResponse) await apiClient.GetFooBriefInfo(new GetFooBriefInfoForFooInputDto { FooContactUpn = upn });
	var FooInfo = (ApiResponse<GetFooBriefInfoForFooDto>) await retrialPolicy.ExecuteAsync(func);
	await FooInfo.EnsureSuccessStatusCodeAsync();

	return FooInfo.Content!;
}
```