35 lines
1.4 KiB
C#
35 lines
1.4 KiB
C#
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;
|
|
}
|
|
} |