From dcf019f5929f81661f77459b49815aa81641c7ac Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Tue, 29 Oct 2024 09:12:31 -0500 Subject: [PATCH] Hardening SubscriptionWrapper against ServiceProvider weirdness. Closes GH-3523 --- .../Subscriptions/subscriptions_end_to_end.cs | 31 +++++++++++++++++++ .../Subscriptions/SubscriptionWrapper.cs | 18 +++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/DaemonTests/Subscriptions/subscriptions_end_to_end.cs b/src/DaemonTests/Subscriptions/subscriptions_end_to_end.cs index 7cc3808d99..338a5dae2e 100644 --- a/src/DaemonTests/Subscriptions/subscriptions_end_to_end.cs +++ b/src/DaemonTests/Subscriptions/subscriptions_end_to_end.cs @@ -498,6 +498,37 @@ public void Dispose() } } +public class FilteredSubscription2: SubscriptionBase, IAsyncDisposable +{ + public FilteredSubscription2() + { + IncludeType(); + IncludeType(); + StreamType = typeof(SimpleAggregate); + IncludeArchivedEvents = true; + } + + public override Task ProcessEventsAsync(EventRange page, ISubscriptionController controller, IDocumentOperations operations, + CancellationToken cancellationToken) + { + return Task.FromResult(Substitute.For()); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + // TODO release managed resources here + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } +} + public class SimpleSubscription: ISubscription { public static int InstanceCounter = 0; diff --git a/src/Marten/Subscriptions/SubscriptionWrapper.cs b/src/Marten/Subscriptions/SubscriptionWrapper.cs index ad420fbe07..0c75417474 100644 --- a/src/Marten/Subscriptions/SubscriptionWrapper.cs +++ b/src/Marten/Subscriptions/SubscriptionWrapper.cs @@ -35,18 +35,14 @@ public ScopedSubscriptionServiceWrapper(IServiceProvider provider) _provider = provider; SubscriptionName = typeof(T).Name; - if (typeof(T).CanBeCastTo()) - { - using var scope = _provider.CreateScope(); - var sp = scope.ServiceProvider; - - var subscription = sp.GetRequiredService().As(); - IncludedEventTypes.AddRange(subscription.IncludedEventTypes); - StreamType = subscription.StreamType; - IncludeArchivedEvents = subscription.IncludeArchivedEvents; - } - + var scope = _provider.CreateAsyncScope(); + var sp = scope.ServiceProvider; + var subscription = sp.GetRequiredService().As(); + IncludedEventTypes.AddRange(subscription.IncludedEventTypes); + StreamType = subscription.StreamType; + IncludeArchivedEvents = subscription.IncludeArchivedEvents; + scope.SafeDispose(); } public override async Task ProcessEventsAsync(EventRange page, ISubscriptionController controller,