Fix codegen for WriteAggregate with OnMissing.ThrowException and batch queries#2390
Merged
jeremydmiller merged 1 commit intomainfrom Mar 31, 2026
Merged
Conversation
…hing When using [WriteAggregate(OnMissing = ThrowException)] with UseIdentityMapForAggregates and a Validate() middleware method, the codegen generated guard frames that referenced stream_entity before it was declared. The batch query enrolled the LoadAggregateFrame but the variable declaration (var stream_entity = await ...BatchItem) was not emitted until after the guard frames had already tried to use stream_entity.Aggregate. The fix adds GenerateCodeForBatchResolution() to LoadAggregateFrame which is called by MartenBatchFrame after the batch execute completes, ensuring the stream variable is declared before any guard frames reference it. Fixes #2387 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2387
[WriteAggregate(OnMissing = ThrowException)]withUseIdentityMapForAggregates = trueand aValidate()middleware method, the Marten batch query policy enrolled theLoadAggregateFramein a batch but didn't emit the variable declaration (var stream_entity = await stream_entity_BatchItem) until after guard frames had already tried to referencestream_entity.Aggregate, causingCS0841: Cannot use local variable before it is declared.GenerateCodeForBatchResolution()toLoadAggregateFrame, called byMartenBatchFrameimmediately afterbatchQuery.Execute(). This ensures the stream variable is declared before any guard frames (null checks, Validate calls) reference it. The originalGenerateCode()skips re-declaration when batch resolution has already run.Key conditions to reproduce
All three must be present:
[WriteAggregate(OnMissing = OnMissing.ThrowException)]on a message handler (not HTTP endpoint)UseIdentityMapForAggregates = true(no snapshot projection)Validate()middleware method that takes the aggregate as a parameterTest plan
🤖 Generated with Claude Code