Compare commits

...

6 Commits

Author SHA1 Message Date
Developer 02
a77c70f655 refactor(AuthClient): Hochgestuft auf 1.3.3 2025-03-12 10:50:35 +01:00
Developer 02
031f830b8f refactor(AuthClient): Detaillierte Protokollierung für AuthClient hinzugefügt. 2025-03-12 10:47:15 +01:00
Developer 02
5f9efa3bb0 refactor(AuthClient): Ausführlichere Protokollierung hinzufügen. 2025-03-12 10:27:06 +01:00
Developer 02
d46dbbb877 refactor(ClientParams): Die Eigenschaft NextRetryDelay wurde entfernt, um die Logik zu vereinfachen. 2025-03-12 09:48:27 +01:00
Developer 02
e194cd8054 feat: Implementierung der Wiederholungslogik für den Verbindungsaufbau in AuthClient
- Hinzugefügt: `TryStartConnectionAsync`-Methode zur Durchführung von Verbindungsversuchen mit Wiederholungslogik.
- `StartAsync` aktualisiert, um wiederholt zu versuchen, die Verbindung herzustellen, bis sie erfolgreich ist oder `RetryDelay` erschöpft ist.
2025-03-12 09:32:10 +01:00
Developer 02
d21e0c06e7 chore(API): Hochgestuft auf 1.1.0 2025-03-12 08:58:26 +01:00
4 changed files with 63 additions and 28 deletions

View File

@ -28,7 +28,19 @@ public class AuthClient : IAuthClient, IHostedService
_connection.On<string, string, string>(nameof(ReceivePublicKeyAsync), ReceivePublicKeyAsync);
_connection.Reconnected += async cnnId => await GetAllPublicKeysAsync();
_connection.Reconnected += async cnnId =>
{
_logger?.LogInformation("Auth-client reconnected. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
await GetAllPublicKeysAsync();
_nOfAttempts = 0;
};
_connection.Reconnecting += (ex) =>
{
logger?.LogError(ex, "Auth-client disconnected. Attempt to reconnect every {time} seconds.", _params.RetryDelay!.Value.TotalSeconds);
_nOfAttempts += 1;
return Task.CompletedTask;
};
_logger = logger;
}
@ -39,11 +51,43 @@ public class AuthClient : IAuthClient, IHostedService
public async Task StartAsync(CancellationToken cancellationToken = default)
{
await _connection.StartAsync(cancellationToken);
while(!await TryStartConnectionAsync(cancellationToken))
{
if (_params.RetryDelay is not null)
await Task.Delay(_params.RetryDelay.Value.Milliseconds, cancellationToken);
else
return;
}
IsConnected = true;
await GetAllPublicKeysAsync();
}
private int _nOfAttempts = 0;
private async Task<bool> TryStartConnectionAsync(CancellationToken cancellationToken = default)
{
try
{
_nOfAttempts += 1;
await _connection.StartAsync(cancellationToken);
_logger?.LogInformation("Auth-client connection successful. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
_nOfAttempts = 0;
return true;
}
catch(HttpRequestException ex)
{
if(_nOfAttempts < 2)
{
if (_params.RetryDelay is null)
_logger?.LogError(ex, "Auth-client connection failed. {message}", ex.Message);
else
_logger?.LogError(ex, "Auth-client connection failed and will be retried every {time} seconds. The status of being successful can be followed from the information logs.\n{message}", _params.RetryDelay.Value.TotalSeconds, ex.Message);
}
return false;
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _connection.StopAsync(cancellationToken);

View File

@ -7,39 +7,30 @@ public class ClientParams
{
public string Url { get; set; } = string.Empty;
private readonly Lazy<IRetryPolicy?> _lazyRetryPolicy;
/// <summary>
/// Controls when the client attempts to reconnect and how many times it does so.
/// </summary>
public IRetryPolicy? RetryPolicy => _lazyRetryPolicy.Value;
public IRetryPolicy? RetryPolicy { get; private set; }
/// <summary>
/// To simplify the assignment of <seealso cref="RetryPolicy"/>
/// </summary>
public Func<RetryContext, TimeSpan?>? NextRetryDelay { get; set; }
private TimeSpan? _retryDelay;
/// <summary>
/// To be able to serilize the simple <seealso cref="RetryPolicy"/>
/// </summary>
public TimeSpan? RetryDelay { get; set; }
public TimeSpan? RetryDelay
{
get => _retryDelay;
set
{
RetryPolicy = new RetryPolicy(ctx => RetryDelay);
_retryDelay = value;
}
}
public event ClientEvent OnPublicKeyReceived = ClientEvents.UpdatePublicKeys;
internal void TriggerOnPublicReceivedEvent(AuthClient client, string issuer, string audience, string key, ILogger? logger = null)
=> OnPublicKeyReceived(client, issuer, audience, key, logger);
public ClientParams()
{
_lazyRetryPolicy = new(() =>
{
if (RetryDelay is not null)
return new RetryPolicy(ctx => RetryDelay);
else if(NextRetryDelay is not null)
return new RetryPolicy(NextRetryDelay);
return null;
});
}
public List<ClientPublicKey> PublicKeys { get; set; } = new();
}

View File

@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>DigitalData.Auth.Client</PackageId>
<Version>1.2.1.1</Version>
<Version>1.3.3</Version>
<Description>DigitalData.Auth.Client is a SignalR-based authentication client that enables applications to connect to a central authentication hub for real-time message exchange. It provides seamless connection management, automatic reconnection (RetryPolicy), and event-driven communication (ClientEvents). The package includes dependency injection support via DIExtensions, allowing easy integration into ASP.NET Core applications. With built-in retry policies and secure message handling, it ensures a reliable and scalable authentication client for real-time authentication workflows.</Description>
<Company>Digital Data GmbH</Company>
<Product>Digital Data GmbH</Product>
@ -14,8 +14,8 @@
<PackageIcon>auth_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/DigitalData.Auth</RepositoryUrl>
<PackageTags>Digital Data Auth Authorization Authentication</PackageTags>
<AssemblyVersion>1.2.1.1</AssemblyVersion>
<FileVersion>1.2.1.1</FileVersion>
<AssemblyVersion>1.3.3</AssemblyVersion>
<FileVersion>1.3.3</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -4,9 +4,9 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Version>1.1.0</Version>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
</PropertyGroup>
<ItemGroup>