feat(DIExtensions): rename DependencyInjection
This commit is contained in:
35
DigitalData.Auth.Client/DependencyInjection.cs
Normal file
35
DigitalData.Auth.Client/DependencyInjection.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using DigitalData.Auth.Abstractions;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.Client;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddAuthHubClient(this IServiceCollection services, IConfiguration? configuration = null, Action<ClientParams>? options = null)
|
||||
{
|
||||
var clientParams = configuration?.Get<ClientParams>() ?? new ClientParams();
|
||||
options?.Invoke(clientParams);
|
||||
services
|
||||
.AddSingleton(Options.Create(clientParams))
|
||||
.AddSingleton<IAuthClient, AuthClient>()
|
||||
.TryAddSingleton<HubConnectionBuilder>();
|
||||
|
||||
services.AddHostedService(sp =>
|
||||
{
|
||||
var client = sp.GetRequiredService<IAuthClient>() as AuthClient;
|
||||
if (client is not null)
|
||||
return client;
|
||||
else throw new InvalidOperationException(
|
||||
"IAuthClient instance could not be resolved from the service provider. " +
|
||||
"This may indicate that the 'AddAuthHubClient' extension method was not called " +
|
||||
"or there was an issue with the dependency registration process."
|
||||
);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user