diff --git a/src/EventSourcingTests/Projections/using_explicit_code_for_live_aggregation.cs b/src/EventSourcingTests/Projections/using_explicit_code_for_live_aggregation.cs index 02f72b871a..6cf2140834 100644 --- a/src/EventSourcingTests/Projections/using_explicit_code_for_live_aggregation.cs +++ b/src/EventSourcingTests/Projections/using_explicit_code_for_live_aggregation.cs @@ -36,6 +36,26 @@ public async Task using_a_custom_projection_for_live_aggregation() aggregate.CCount.ShouldBe(3); aggregate.Id.ShouldBe(streamId); } + + [Fact] + public async Task using_a_custom_projection_for_live_aggregation_with_query_session() + { + StoreOptions(opts => + { + opts.Projections.Add(new ExplicitCounter(), ProjectionLifecycle.Live); + }); + + var streamId = theSession.Events.StartStream(new AEvent(), new AEvent(), new BEvent(), new CEvent(), new CEvent(), new CEvent()).Id; + await theSession.SaveChangesAsync(); + + using var query = theStore.QuerySession(); + + var aggregate = await query.Events.AggregateStreamAsync(streamId); + aggregate.ACount.ShouldBe(2); + aggregate.BCount.ShouldBe(1); + aggregate.CCount.ShouldBe(3); + aggregate.Id.ShouldBe(streamId); + } } #region sample_using_simple_explicit_code_for_live_aggregation diff --git a/src/Marten/Events/Aggregation/CustomProjection.cs b/src/Marten/Events/Aggregation/CustomProjection.cs index 4fee3e0727..894b758f1f 100644 --- a/src/Marten/Events/Aggregation/CustomProjection.cs +++ b/src/Marten/Events/Aggregation/CustomProjection.cs @@ -357,7 +357,7 @@ async ValueTask ILiveAggregator.BuildAsync(IReadOnlyList eve { if (!events.Any()) return default; - var documentSessionBase = session.As(); + var documentSessionBase = session as DocumentSessionBase ?? (DocumentSessionBase)session.DocumentStore.LightweightSession(); var slice = new EventSlice(default, session, events); await ApplyChangesAsync(documentSessionBase, slice, cancellation).ConfigureAwait(false);