Skip to content

Commit

Permalink
Hardening SubscriptionWrapper against ServiceProvider weirdness. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 29, 2024
1 parent 98a4fb3 commit dcf019f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
31 changes: 31 additions & 0 deletions src/DaemonTests/Subscriptions/subscriptions_end_to_end.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,37 @@ public void Dispose()
}
}

public class FilteredSubscription2: SubscriptionBase, IAsyncDisposable
{
public FilteredSubscription2()
{
IncludeType<AEvent>();
IncludeType<BEvent>();
StreamType = typeof(SimpleAggregate);
IncludeArchivedEvents = true;
}

public override Task<IChangeListener> ProcessEventsAsync(EventRange page, ISubscriptionController controller, IDocumentOperations operations,
CancellationToken cancellationToken)
{
return Task.FromResult(Substitute.For<IChangeListener>());
}

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;
Expand Down
18 changes: 7 additions & 11 deletions src/Marten/Subscriptions/SubscriptionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ public ScopedSubscriptionServiceWrapper(IServiceProvider provider)
_provider = provider;
SubscriptionName = typeof(T).Name;

if (typeof(T).CanBeCastTo<SubscriptionBase>())
{
using var scope = _provider.CreateScope();
var sp = scope.ServiceProvider;

var subscription = sp.GetRequiredService<T>().As<SubscriptionBase>();
IncludedEventTypes.AddRange(subscription.IncludedEventTypes);
StreamType = subscription.StreamType;
IncludeArchivedEvents = subscription.IncludeArchivedEvents;
}

var scope = _provider.CreateAsyncScope();
var sp = scope.ServiceProvider;

var subscription = sp.GetRequiredService<T>().As<SubscriptionBase>();
IncludedEventTypes.AddRange(subscription.IncludedEventTypes);
StreamType = subscription.StreamType;
IncludeArchivedEvents = subscription.IncludeArchivedEvents;
scope.SafeDispose();
}

public override async Task<IChangeListener> ProcessEventsAsync(EventRange page, ISubscriptionController controller,
Expand Down

0 comments on commit dcf019f

Please sign in to comment.