@@ -66,46 +66,20 @@ public static MessageContext<TMessage> CreateContext<TMessage>(
6666 /// </summary>
6767 public static MessageContextSnapshot CreateSnapshot ( BasicDeliverEventArgs ea )
6868 {
69- var context = BuildMetadata ( ea ) ;
69+ var metadata = ExtractMetadata ( ea ) ;
7070 return new MessageContextSnapshot
7171 {
72- MessageId = context . MessageId ,
73- RequestId = context . RequestId ,
74- CorrelationId = context . CorrelationId ,
75- ConversationId = context . ConversationId ,
76- InitiatorId = context . InitiatorId ,
77- SourceAddress = context . SourceAddress ,
78- DestinationAddress = context . DestinationAddress ,
79- ResponseAddress = context . ResponseAddress ,
80- FaultAddress = context . FaultAddress ,
81- RoutingKey = context . RoutingKey ,
82- RetryCount = context . RetryCount ,
83- } ;
84- }
85-
86- private static MessageContext BuildMetadata ( BasicDeliverEventArgs ea )
87- {
88- var props = ea . BasicProperties ;
89- var headers = props . Headers ?? new Dictionary < string , object ? > ( ) ;
90- var sentTime = GetSentTime ( props ) ;
91- return new MessageContext
92- {
93- MessageId = props . MessageId ,
94- CorrelationId = props . CorrelationId ,
95- RequestId = props . CorrelationId ,
96- RoutingKey = ea . RoutingKey ,
97- Headers = AmqpHeaderValueNormalizer . Normalize ( headers ) ,
98- Redelivered = ea . Redelivered ,
99- RetryCount = RabbitMqConstants . GetRetryCount ( headers ) ,
100- ConversationId = RabbitMqConstants . GetHeaderString ( headers , "ConversationId" ) ,
101- InitiatorId = RabbitMqConstants . GetHeaderString ( headers , "InitiatorId" ) ,
102- SourceAddress = RabbitMqConstants . GetHeaderUri ( headers , "SourceAddress" ) ,
103- DestinationAddress = RabbitMqConstants . GetHeaderUri ( headers , "DestinationAddress" ) ,
104- ResponseAddress = RabbitMqConstants . GetHeaderUri ( headers , "ResponseAddress" )
105- ?? ( string . IsNullOrEmpty ( props . ReplyTo ) ? null : new Uri ( $ "queue:{ props . ReplyTo } ") ) ,
106- FaultAddress = RabbitMqConstants . GetHeaderUri ( headers , "FaultAddress" ) ,
107- SentTime = sentTime ,
108- ExpirationTime = RabbitMqConstants . TryParseExpiration ( props . Expiration , sentTime )
72+ MessageId = metadata . MessageId ,
73+ RequestId = metadata . RequestId ,
74+ CorrelationId = metadata . CorrelationId ,
75+ ConversationId = metadata . ConversationId ,
76+ InitiatorId = metadata . InitiatorId ,
77+ SourceAddress = metadata . SourceAddress ,
78+ DestinationAddress = metadata . DestinationAddress ,
79+ ResponseAddress = metadata . ResponseAddress ,
80+ FaultAddress = metadata . FaultAddress ,
81+ RoutingKey = metadata . RoutingKey ,
82+ RetryCount = metadata . RetryCount ,
10983 } ;
11084 }
11185
@@ -115,35 +89,76 @@ private static MessageContext<TMessage> BuildTypedMetadata<TMessage>(
11589 IPublisher ? publisher ,
11690 ISendEndpointProvider ? sendEndpointProvider ,
11791 CancellationToken cancellationToken )
92+ {
93+ var metadata = ExtractMetadata ( ea ) ;
94+ return MessageContext . BuildTyped (
95+ message ,
96+ publisher ,
97+ sendEndpointProvider ,
98+ metadata . MessageId ,
99+ metadata . CorrelationId ,
100+ metadata . RequestId ,
101+ metadata . RoutingKey ,
102+ metadata . Headers ,
103+ metadata . Redelivered ,
104+ metadata . RetryCount ,
105+ metadata . ConversationId ,
106+ metadata . InitiatorId ,
107+ metadata . SourceAddress ,
108+ metadata . DestinationAddress ,
109+ metadata . ResponseAddress ,
110+ metadata . FaultAddress ,
111+ metadata . SentTime ,
112+ metadata . ExpirationTime ,
113+ cancellationToken ) ;
114+ }
115+
116+ /// <summary>
117+ /// Extracts the transport metadata common to both the fault snapshot and the bare-JSON typed context from a
118+ /// single AMQP delivery: the property/header paths, the response-address fallback to <c>ReplyTo</c>, and the
119+ /// TTL anchored to the delivery's timestamp.
120+ /// </summary>
121+ private static DeliveryMetadata ExtractMetadata ( BasicDeliverEventArgs ea )
118122 {
119123 var props = ea . BasicProperties ;
120124 var headers = props . Headers ?? new Dictionary < string , object ? > ( ) ;
121125 var sentTime = GetSentTime ( props ) ;
122- return new MessageContext < TMessage >
123- {
124- Message = message ,
125- Publisher = publisher ,
126- SendEndpointProvider = sendEndpointProvider ,
127- CancellationToken = cancellationToken ,
128- MessageId = props . MessageId ,
129- CorrelationId = props . CorrelationId ,
130- RequestId = props . CorrelationId ,
131- RoutingKey = ea . RoutingKey ,
132- Headers = AmqpHeaderValueNormalizer . Normalize ( headers ) ,
133- Redelivered = ea . Redelivered ,
134- RetryCount = RabbitMqConstants . GetRetryCount ( headers ) ,
135- ConversationId = RabbitMqConstants . GetHeaderString ( headers , "ConversationId" ) ,
136- InitiatorId = RabbitMqConstants . GetHeaderString ( headers , "InitiatorId" ) ,
137- SourceAddress = RabbitMqConstants . GetHeaderUri ( headers , "SourceAddress" ) ,
138- DestinationAddress = RabbitMqConstants . GetHeaderUri ( headers , "DestinationAddress" ) ,
139- ResponseAddress = RabbitMqConstants . GetHeaderUri ( headers , "ResponseAddress" )
126+ return new DeliveryMetadata (
127+ MessageId : props . MessageId ,
128+ CorrelationId : props . CorrelationId ,
129+ RequestId : props . CorrelationId ,
130+ RoutingKey : ea . RoutingKey ,
131+ Headers : AmqpHeaderValueNormalizer . Normalize ( headers ) ,
132+ Redelivered : ea . Redelivered ,
133+ RetryCount : RabbitMqConstants . GetRetryCount ( headers ) ,
134+ ConversationId : RabbitMqConstants . GetHeaderString ( headers , "ConversationId" ) ,
135+ InitiatorId : RabbitMqConstants . GetHeaderString ( headers , "InitiatorId" ) ,
136+ SourceAddress : RabbitMqConstants . GetHeaderUri ( headers , "SourceAddress" ) ,
137+ DestinationAddress : RabbitMqConstants . GetHeaderUri ( headers , "DestinationAddress" ) ,
138+ ResponseAddress : RabbitMqConstants . GetHeaderUri ( headers , "ResponseAddress" )
140139 ?? ( string . IsNullOrEmpty ( props . ReplyTo ) ? null : new Uri ( $ "queue:{ props . ReplyTo } ") ) ,
141- FaultAddress = RabbitMqConstants . GetHeaderUri ( headers , "FaultAddress" ) ,
142- SentTime = sentTime ,
143- ExpirationTime = RabbitMqConstants . TryParseExpiration ( props . Expiration , sentTime )
144- } ;
140+ FaultAddress : RabbitMqConstants . GetHeaderUri ( headers , "FaultAddress" ) ,
141+ SentTime : sentTime ,
142+ ExpirationTime : RabbitMqConstants . TryParseExpiration ( props . Expiration , sentTime ) ) ;
145143 }
146144
147145 private static DateTimeOffset ? GetSentTime ( IReadOnlyBasicProperties props )
148146 => props . Timestamp . UnixTime > 0 ? DateTimeOffset . FromUnixTimeSeconds ( props . Timestamp . UnixTime ) : null ;
147+
148+ private readonly record struct DeliveryMetadata (
149+ string ? MessageId ,
150+ string ? CorrelationId ,
151+ string ? RequestId ,
152+ string RoutingKey ,
153+ IReadOnlyDictionary < string , object ? > Headers ,
154+ bool Redelivered ,
155+ int RetryCount ,
156+ string ? ConversationId ,
157+ string ? InitiatorId ,
158+ Uri ? SourceAddress ,
159+ Uri ? DestinationAddress ,
160+ Uri ? ResponseAddress ,
161+ Uri ? FaultAddress ,
162+ DateTimeOffset ? SentTime ,
163+ DateTimeOffset ? ExpirationTime ) ;
149164}
0 commit comments