@@ -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
@@ -116,34 +90,77 @@ private static MessageContext<TMessage> BuildTypedMetadata<TMessage>(
11690 ISendEndpointProvider ? sendEndpointProvider ,
11791 CancellationToken cancellationToken )
11892 {
119- var props = ea . BasicProperties ;
120- var headers = props . Headers ?? new Dictionary < string , object ? > ( ) ;
121- var sentTime = GetSentTime ( props ) ;
93+ var metadata = ExtractMetadata ( ea ) ;
12294 return new MessageContext < TMessage >
12395 {
12496 Message = message ,
12597 Publisher = publisher ,
12698 SendEndpointProvider = sendEndpointProvider ,
12799 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" )
140- ?? ( 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 )
100+ MessageId = metadata . MessageId ,
101+ CorrelationId = metadata . CorrelationId ,
102+ RequestId = metadata . RequestId ,
103+ RoutingKey = metadata . RoutingKey ,
104+ Headers = metadata . Headers ,
105+ Redelivered = metadata . Redelivered ,
106+ RetryCount = metadata . RetryCount ,
107+ ConversationId = metadata . ConversationId ,
108+ InitiatorId = metadata . InitiatorId ,
109+ SourceAddress = metadata . SourceAddress ,
110+ DestinationAddress = metadata . DestinationAddress ,
111+ ResponseAddress = metadata . ResponseAddress ,
112+ FaultAddress = metadata . FaultAddress ,
113+ SentTime = metadata . SentTime ,
114+ ExpirationTime = metadata . ExpirationTime ,
144115 } ;
145116 }
146117
118+ /// <summary>
119+ /// Extracts the transport metadata common to both the fault snapshot and the bare-JSON typed context from a
120+ /// single AMQP delivery: the property/header paths, the response-address fallback to <c>ReplyTo</c>, and the
121+ /// TTL anchored to the delivery's timestamp.
122+ /// </summary>
123+ private static DeliveryMetadata ExtractMetadata ( BasicDeliverEventArgs ea )
124+ {
125+ var props = ea . BasicProperties ;
126+ var headers = props . Headers ?? new Dictionary < string , object ? > ( ) ;
127+ var sentTime = GetSentTime ( props ) ;
128+ return new DeliveryMetadata (
129+ MessageId : props . MessageId ,
130+ CorrelationId : props . CorrelationId ,
131+ RequestId : props . CorrelationId ,
132+ RoutingKey : ea . RoutingKey ,
133+ Headers : AmqpHeaderValueNormalizer . Normalize ( headers ) ,
134+ Redelivered : ea . Redelivered ,
135+ RetryCount : RabbitMqConstants . GetRetryCount ( headers ) ,
136+ ConversationId : RabbitMqConstants . GetHeaderString ( headers , "ConversationId" ) ,
137+ InitiatorId : RabbitMqConstants . GetHeaderString ( headers , "InitiatorId" ) ,
138+ SourceAddress : RabbitMqConstants . GetHeaderUri ( headers , "SourceAddress" ) ,
139+ DestinationAddress : RabbitMqConstants . GetHeaderUri ( headers , "DestinationAddress" ) ,
140+ ResponseAddress : RabbitMqConstants . GetHeaderUri ( headers , "ResponseAddress" )
141+ ?? ( string . IsNullOrEmpty ( props . ReplyTo ) ? null : new Uri ( $ "queue:{ props . ReplyTo } ") ) ,
142+ FaultAddress : RabbitMqConstants . GetHeaderUri ( headers , "FaultAddress" ) ,
143+ SentTime : sentTime ,
144+ ExpirationTime : RabbitMqConstants . TryParseExpiration ( props . Expiration , sentTime ) ) ;
145+ }
146+
147147 private static DateTimeOffset ? GetSentTime ( IReadOnlyBasicProperties props )
148148 => props . Timestamp . UnixTime > 0 ? DateTimeOffset . FromUnixTimeSeconds ( props . Timestamp . UnixTime ) : null ;
149+
150+ private readonly record struct DeliveryMetadata (
151+ string ? MessageId ,
152+ string ? CorrelationId ,
153+ string ? RequestId ,
154+ string RoutingKey ,
155+ IReadOnlyDictionary < string , object ? > Headers ,
156+ bool Redelivered ,
157+ int RetryCount ,
158+ string ? ConversationId ,
159+ string ? InitiatorId ,
160+ Uri ? SourceAddress ,
161+ Uri ? DestinationAddress ,
162+ Uri ? ResponseAddress ,
163+ Uri ? FaultAddress ,
164+ DateTimeOffset ? SentTime ,
165+ DateTimeOffset ? ExpirationTime ) ;
149166}
0 commit comments