diff --git a/src/SIL.Machine.AspNetCore/Configuration/IMachineBuilderExtensions.cs b/src/SIL.Machine.AspNetCore/Configuration/IMachineBuilderExtensions.cs index b676afb68..26ea5f053 100644 --- a/src/SIL.Machine.AspNetCore/Configuration/IMachineBuilderExtensions.cs +++ b/src/SIL.Machine.AspNetCore/Configuration/IMachineBuilderExtensions.cs @@ -316,7 +316,11 @@ public static IMachineBuilder AddServalTranslationEngineService( IEnumerable? engineTypes = null ) { - builder.Services.AddGrpc(options => options.Interceptors.Add()); + builder.Services.AddGrpc(options => + { + options.Interceptors.Add(); + options.Interceptors.Add(); + }); builder.AddServalPlatformService(connectionString ?? builder.Configuration.GetConnectionString("Serval")); engineTypes ??= builder.Configuration?.GetSection("TranslationEngines").Get() diff --git a/src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs b/src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs new file mode 100644 index 000000000..5da0e4c7d --- /dev/null +++ b/src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs @@ -0,0 +1,35 @@ +namespace SIL.Machine.AspNetCore.Services; + +public class CancellationInterceptor : Interceptor +{ + private readonly ILogger _logger; + + public CancellationInterceptor(ILogger logger) + { + _logger = logger; + } + + public override async Task UnaryServerHandler( + TRequest request, + ServerCallContext context, + UnaryServerMethod continuation + ) + { + try + { + return await continuation(request, context); + } + catch (Exception ex) + { + if (ex is OperationCanceledException) + { + _logger.LogInformation("An operation was canceled."); + return null; + } + else + { + throw; + } + } + } +}