Move options validation to constructor (#826)
This works around the breaking change to BackgroundService in https://github.com/dotnet/runtime/pull/116283 which launches ExecuteAsync using Task.Run.
This commit is contained in:
parent
7628b3064f
commit
740a7d0346
2 changed files with 22 additions and 20 deletions
|
|
@ -4,16 +4,18 @@ using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace ModelContextProtocol.AspNetCore;
|
namespace ModelContextProtocol.AspNetCore;
|
||||||
|
|
||||||
internal sealed partial class IdleTrackingBackgroundService(
|
internal sealed partial class IdleTrackingBackgroundService : BackgroundService
|
||||||
StatefulSessionManager sessions,
|
|
||||||
IOptions<HttpServerTransportOptions> options,
|
|
||||||
IHostApplicationLifetime appLifetime,
|
|
||||||
ILogger<IdleTrackingBackgroundService> logger) : BackgroundService
|
|
||||||
{
|
{
|
||||||
// Workaround for https://github.com/dotnet/runtime/issues/91121. This is fixed in .NET 9 and later.
|
private readonly StatefulSessionManager _sessions;
|
||||||
private readonly ILogger _logger = logger;
|
private readonly IOptions<HttpServerTransportOptions> _options;
|
||||||
|
private readonly IHostApplicationLifetime _appLifetime;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
public IdleTrackingBackgroundService(
|
||||||
|
StatefulSessionManager sessions,
|
||||||
|
IOptions<HttpServerTransportOptions> options,
|
||||||
|
IHostApplicationLifetime appLifetime,
|
||||||
|
ILogger<IdleTrackingBackgroundService> logger)
|
||||||
{
|
{
|
||||||
// Still run loop given infinite IdleTimeout to enforce the MaxIdleSessionCount and assist graceful shutdown.
|
// Still run loop given infinite IdleTimeout to enforce the MaxIdleSessionCount and assist graceful shutdown.
|
||||||
if (options.Value.IdleTimeout != Timeout.InfiniteTimeSpan)
|
if (options.Value.IdleTimeout != Timeout.InfiniteTimeSpan)
|
||||||
|
|
@ -23,14 +25,22 @@ internal sealed partial class IdleTrackingBackgroundService(
|
||||||
|
|
||||||
ArgumentOutOfRangeException.ThrowIfLessThan(options.Value.MaxIdleSessionCount, 0);
|
ArgumentOutOfRangeException.ThrowIfLessThan(options.Value.MaxIdleSessionCount, 0);
|
||||||
|
|
||||||
|
_sessions = sessions;
|
||||||
|
_options = options;
|
||||||
|
_appLifetime = appLifetime;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var timeProvider = options.Value.TimeProvider;
|
var timeProvider = _options.Value.TimeProvider;
|
||||||
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(5), timeProvider);
|
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(5), timeProvider);
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
|
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
|
||||||
{
|
{
|
||||||
await sessions.PruneIdleSessionsAsync(stoppingToken);
|
await _sessions.PruneIdleSessionsAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
|
@ -40,7 +50,7 @@ internal sealed partial class IdleTrackingBackgroundService(
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await sessions.DisposeAllSessionsAsync();
|
await _sessions.DisposeAllSessionsAsync();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|
@ -48,7 +58,7 @@ internal sealed partial class IdleTrackingBackgroundService(
|
||||||
{
|
{
|
||||||
// Something went terribly wrong. A very unexpected exception must be bubbling up, but let's ensure we also stop the application,
|
// Something went terribly wrong. A very unexpected exception must be bubbling up, but let's ensure we also stop the application,
|
||||||
// so that it hopefully gets looked at and restarted. This shouldn't really be reachable.
|
// so that it hopefully gets looked at and restarted. This shouldn't really be reachable.
|
||||||
appLifetime.StopApplication();
|
_appLifetime.StopApplication();
|
||||||
IdleTrackingBackgroundServiceStoppedUnexpectedly();
|
IdleTrackingBackgroundServiceStoppedUnexpectedly();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,11 +57,7 @@ public class StreamableHttpServerConformanceTests(ITestOutputHelper outputHelper
|
||||||
base.Dispose();
|
base.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !NET10_0
|
|
||||||
[Fact]
|
[Fact]
|
||||||
#else
|
|
||||||
[Fact(Skip = "https://github.com/modelcontextprotocol/csharp-sdk/issues/823")]
|
|
||||||
#endif
|
|
||||||
public async Task NegativeNonInfiniteIdleTimeout_Throws_ArgumentOutOfRangeException()
|
public async Task NegativeNonInfiniteIdleTimeout_Throws_ArgumentOutOfRangeException()
|
||||||
{
|
{
|
||||||
Builder.Services.AddMcpServer().WithHttpTransport(options =>
|
Builder.Services.AddMcpServer().WithHttpTransport(options =>
|
||||||
|
|
@ -73,11 +69,7 @@ public class StreamableHttpServerConformanceTests(ITestOutputHelper outputHelper
|
||||||
Assert.Contains("IdleTimeout", ex.Message);
|
Assert.Contains("IdleTimeout", ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !NET10_0
|
|
||||||
[Fact]
|
[Fact]
|
||||||
#else
|
|
||||||
[Fact(Skip = "https://github.com/modelcontextprotocol/csharp-sdk/issues/823")]
|
|
||||||
#endif
|
|
||||||
public async Task NegativeMaxIdleSessionCount_Throws_ArgumentOutOfRangeException()
|
public async Task NegativeMaxIdleSessionCount_Throws_ArgumentOutOfRangeException()
|
||||||
{
|
{
|
||||||
Builder.Services.AddMcpServer().WithHttpTransport(options =>
|
Builder.Services.AddMcpServer().WithHttpTransport(options =>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue