39 lines
1.5 KiB
C#
39 lines
1.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
|
|
|
/// <summary>Coordinates endpoint cleanup after ServiceLevel binding fails during startup.</summary>
|
|
internal static class FailedServiceLevelBindingCleanup
|
|
{
|
|
/// <summary>Stops and disposes the endpoint, returning whether ownership can be released.</summary>
|
|
/// <param name="stop">The SDK stop operation.</param>
|
|
/// <param name="dispose">The application-host disposal operation.</param>
|
|
/// <param name="logger">The cleanup logger.</param>
|
|
/// <returns>Whether cleanup completed successfully.</returns>
|
|
internal static async Task<bool> StopAndDisposeAsync(
|
|
Func<Task> stop, Func<ValueTask> dispose, ILogger logger)
|
|
{
|
|
var completed = true;
|
|
try
|
|
{
|
|
await stop().ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
completed = false;
|
|
logger.LogError(ex, "OtOpcUaServerHostedService: SDK stop failed after binding failure; retaining endpoint ownership for shutdown retry");
|
|
}
|
|
// Attempt application cleanup even when stopping faults; keep ownership on either failure.
|
|
try
|
|
{
|
|
await dispose().ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
completed = false;
|
|
logger.LogError(ex, "OtOpcUaServerHostedService: endpoint disposal failed after binding failure; retaining endpoint ownership for shutdown retry");
|
|
}
|
|
return completed;
|
|
}
|
|
}
|