Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.12" />
</ItemGroup>
</Project>
34 changes: 26 additions & 8 deletions src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/InjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Extensions.Http;

namespace Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent;

Expand All @@ -13,38 +16,53 @@ public static class InjectExtensions
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="baseUri">The base uri for api.</param>
/// <typeparam name="T">The type of service agent</typeparam>
/// <param name="policy">The polly policy for underlying httpclient.</param>
/// <typeparam name="TClient">The type of service agent</typeparam>
/// <returns></returns>
public static IHttpClientBuilder AddServiceAgent<T>(this IServiceCollection services, string baseUri)
where T : CqrsServiceAgent
public static IHttpClientBuilder AddServiceAgent<TClient>(
this IServiceCollection services,
string baseUri,
IAsyncPolicy<HttpResponseMessage>? policy = null)
where TClient : class
{
return services.AddHttpClient<T>(
policy ??= GetDefaultPolicy();
return services.AddHttpClient<TClient>(
h =>
{
h.BaseAddress = new Uri(baseUri);
h.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/cqrs"));
});
}).AddPolicyHandler(policy);
}

/// <summary>
/// Inject a service agent to services.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="baseUri">The base uri for api.</param>
/// <param name="policy">The polly policy for underlying httpclient.</param>
/// <typeparam name="TClient">The type of api client.</typeparam>
/// <typeparam name="TImplementation">The type of service agent</typeparam>
/// <returns></returns>
public static IHttpClientBuilder AddServiceAgent<TClient, TImplementation>(
this IServiceCollection services,
string baseUri)
string baseUri,
IAsyncPolicy<HttpResponseMessage>? policy = null)
where TClient : class
where TImplementation : CqrsServiceAgent, TClient
where TImplementation : class, TClient
{
policy ??= GetDefaultPolicy();
return services.AddHttpClient<TClient, TImplementation>(
h =>
{
h.BaseAddress = new Uri(baseUri);
h.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/cqrs"));
});
}).AddPolicyHandler(policy);
}

private static IAsyncPolicy<HttpResponseMessage> GetDefaultPolicy()
{
return HttpPolicyExtensions.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == HttpStatusCode.TooManyRequests)
.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(1500));
}
}