Skip to content

Commit feee0db

Browse files
committed
Fixed multiple invocations of AddOAuthClient.
1 parent 76abcf1 commit feee0db

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth/DependencyInjection/OAuthHttpClientAuthenticationBuilderExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) The AppCore .NET project.
33

44
using System;
5+
using System.Linq;
56
using AppCoreNet.Diagnostics;
67
using AppCoreNet.Extensions.Http.Authentication.OAuth;
78
using Microsoft.Extensions.DependencyInjection;
@@ -16,6 +17,10 @@ namespace AppCoreNet.Extensions.DependencyInjection;
1617
/// </summary>
1718
public static class OAuthHttpClientAuthenticationBuilderExtensions
1819
{
20+
internal sealed class OAuthCoreServiceGuard
21+
{
22+
}
23+
1924
/// <summary>
2025
/// Adds OAuth client authentication services.
2126
/// </summary>
@@ -27,13 +32,18 @@ public static IHttpClientAuthenticationBuilder AddOAuthCore(this IHttpClientAuth
2732

2833
IServiceCollection services = builder.Services;
2934

35+
if (builder.Services.Any(sd => sd.ServiceType == typeof(OAuthCoreServiceGuard)))
36+
return builder;
37+
38+
services.AddTransient<OAuthCoreServiceGuard>();
39+
3040
services.AddDistributedMemoryCache();
3141

3242
services.AddOptions<OAuthTokenCacheOptions>();
33-
services.TryAddSingleton<IOAuthTokenCache, OAuthTokenCache>();
34-
services.TryAddSingleton<IOAuthTokenService, OAuthTokenService>();
43+
services.AddSingleton<IOAuthTokenCache, OAuthTokenCache>();
44+
services.AddSingleton<IOAuthTokenService, OAuthTokenService>();
3545

36-
services.TryAddTransient<IOAuthOptionsProvider, OAuthOptionsProvider>();
46+
services.AddTransient<IOAuthOptionsProvider, OAuthOptionsProvider>();
3747
services.AddHttpClient<IOAuthTokenClient, OAuthTokenClient>();
3848

3949
return builder;

0 commit comments

Comments
 (0)