fix: preserve SDK role identity and continuation authority lifecycle

This commit is contained in:
Joseph Doherty
2026-11-10 10:29:23 -05:00
parent a5bed28765
commit b71620145b
5 changed files with 105 additions and 43 deletions
@@ -202,6 +202,8 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task StartAsync(StandardServer server, CancellationToken cancellationToken)
{
if (server is not OtOpcUaSdkServer sdkServer)
throw new ArgumentException("The server must support application authentication before its listeners open.", nameof(server));
_server = server;
// 1.5.378 requires an ITelemetryContext on the ApplicationInstance ctor (the parameterless ctor
// is obsolete). DefaultTelemetry.Create wires the SDK's internal logging; an empty builder keeps
@@ -213,27 +215,32 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
ConfigSectionName = "OtOpcUa",
};
_ = await BuildConfigurationAsync(cancellationToken);
await EnsureApplicationCertificateAsync(cancellationToken).ConfigureAwait(false);
await _application.StartAsync(server).ConfigureAwait(false);
AttachUserAuthenticator();
PopulateServerArray();
try
{
_ = await BuildConfigurationAsync(cancellationToken);
await EnsureApplicationCertificateAsync(cancellationToken).ConfigureAwait(false);
AttachUserAuthenticator(sdkServer);
await _application.StartAsync(server, cancellationToken).ConfigureAwait(false);
PopulateServerArray();
}
catch
{
await DisposeAsync().ConfigureAwait(false);
throw;
}
_logger.LogInformation("OPC UA server started on opc.tcp://{Host}:{Port}",
_options.PublicHostname, _options.OpcUaPort);
}
private void AttachUserAuthenticator()
private void AttachUserAuthenticator(OtOpcUaSdkServer server)
{
var registry = _server?.CurrentInstance?.IdentityRegistry
?? throw new InvalidOperationException("OPC UA identity registry unavailable after start.");
foreach (var type in new[] { UserTokenType.Anonymous, UserTokenType.UserName })
{
var adapter = new ApplicationTokenAuthenticator(this, type);
registry.Register(adapter);
_identityAuthenticators.Add(adapter);
}
server.ConfigureIdentityAuthenticators(_identityAuthenticators);
}
private sealed class ApplicationTokenAuthenticator(OpcUaApplicationHost host, UserTokenType tokenType)
@@ -634,22 +641,14 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
/// <returns>A task that represents the asynchronous operation.</returns>
public async ValueTask DisposeAsync()
{
try
{
if (_server?.CurrentInstance?.IdentityRegistry is { } registry)
foreach (var authenticator in _identityAuthenticators) registry.Unregister(authenticator);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "OpcUaApplicationHost: detaching identity authenticators threw");
}
_identityAuthenticators.Clear();
// Authenticators remain installed until the SDK closes every listener and disposes its registry.
if (_application is { } application)
{
_application = null;
try { await application.DisposeAsync().ConfigureAwait(false); }
catch (Exception ex) { _logger.LogWarning(ex, "OpcUaApplicationHost: disposal threw"); }
}
_identityAuthenticators.Clear();
_server = null;
}
}