fix(runtime): resolve endpoint-scoped serializer on replay/retry path#3051
Merged
jeremydmiller merged 1 commit intoJun 9, 2026
Conversation
MassTransit and NServiceBus interop serializers wired via UseMassTransitInterop()/UseNServiceBusInterop() are registered only on the listener endpoint, never in the global content-type registry. On first receipt the envelope carries that serializer, but on a replay (scheduled retry, durable recovery) the runtime-only Serializer reference is gone, and HandlerPipeline.TryDeserializeEnvelope fell back to WolverineOptions.DetermineSerializer, which resolves only against the global registry. For 'application/vnd.masstransit+json' that misses and falls back to the default JSON serializer, which deserializes the un-unwrapped MassTransit envelope root and yields an all-default message (every field 0/null). Resolve the serializer from the originating endpoint first (the per-listener pipeline's _endpoint, else EndpointFor(envelope.Destination)) before the global fallback, mirroring DeadLetterEnvelope.TryReadData. This restores parity between first-receipt and replay deserialization.
This was referenced Jun 10, 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.
Problem
When consuming MassTransit-produced messages through
UseMassTransitInterop(), the first delivery deserializes correctly, but any retry / replay (scheduled retry, durable recovery) deserializes into an all-default message — every field0/null(e.g.OrderIdcomes back0, leading to spuriousNotFound-style failures).Root cause — the interop serializer is endpoint-scoped, the retry path resolves globally
MassTransit (and NServiceBus) interop wraps the real message inside an envelope and serializes it under a
messageproperty with content typeapplication/vnd.masstransit+json. The unwrapping is done byMassTransitJsonSerializer, whichUseMassTransitInterop()registers only on the listener endpoint (EnvelopeMapper.InteropWithMassTransit→_endpoint.DefaultSerializer = serializer) — never in the global content-type registry.Two deserialization paths then disagree:
message?envelope.Serializer, set byEnvelopeMapper.MapIncomingToEnvelopefrom the endpointenvelope.Serializerisnull(runtime-only, not persisted) → falls back toWolverineOptions.DetermineSerializer, which only checks the global registryOn replay,
HandlerPipeline.TryDeserializeEnvelopedid:The global registry has no entry for
application/vnd.masstransit+json, so it falls back to the default System.Text.Json serializer, which deserializes the un-unwrapped MassTransit envelope root (messageId,messageType,host, …) into the target type — leaving every real field at its default value.Fix
Resolve the serializer from the originating endpoint first, then fall back to the global registry — exactly mirroring the existing
DeadLetterEnvelope.TryReadDataprecedent (and restoring parity with first-receipt resolution inEnvelopeMapper.MapIncomingToEnvelope):serializerForuses the per-listener pipeline's_endpointwhen available, otherwiseEndpointFor(envelope.Destination)(the persistedDestinationset byEnvelope.MarkReceived), and only then falls back toDetermineSerializer.This fixes the issue for any endpoint-scoped serializer on the replay path — MassTransit interop, NServiceBus interop, and custom per-endpoint serializers — with no change to the global registry and no cross-endpoint content-type collisions.
Why endpoint-first (vs. registering the interop serializer globally)
EnvelopeMapper.InteropWithMassTransit); promoting it to the global registry would risk content-type collisions across multiple interop endpoints (last writer wins itsTranslateMassTransitToWolverineUri).Endpoint.TryFindSerializeralready falls back to the global registry, so "endpoint-first, then global" is the established read-path convention in the codebase.Tests
masstransit_interop_serializer_on_retry(broker-free,StubAllExternalTransports): forges a real MassTransit wire payload (message nested undermessage), rebuilds the envelope the way durable storage hands it back on a retry (ContentType+Destination+Data, noSerializer), runs it throughHandlerPipeline.TryDeserializeEnvelope, and asserts the message is unwrapped (OrderId == 92883). Confirmed RED before the fix (OrderIdwas0) and GREEN after, onnet9.0andnet10.0.No regression in the existing serialization / pipeline / interop / error-handling / scheduling suites in
CoreTests.