@@ -122,83 +122,83 @@ public Task<PublishResult> PublishAsync(IMessage message, CancellationToken canc
122122 TaskCompletionSource < PublishResult > publishResultTcs =
123123 Utils . CreateTaskCompletionSource < PublishResult > ( ) ;
124124
125- try
125+ Message nativeMessage = ( ( AmqpMessage ) message ) . NativeMessage ;
126+
127+ void OutcomeCallback ( ILink sender , Message inMessage , Outcome outcome , object state )
126128 {
127- Message nativeMessage = ( ( AmqpMessage ) message ) . NativeMessage ;
129+ // Note: sometimes `inMessage` is null 🤔
130+ Debug . Assert ( Object . ReferenceEquals ( this , state ) ) ;
128131
129- void OutcomeCallback ( ILink sender , Message inMessage , Outcome outcome , object state )
132+ if ( false == Object . ReferenceEquals ( _senderLink , sender ) )
130133 {
131- // Note: sometimes `message` is null 🤔
132- Debug . Assert ( Object . ReferenceEquals ( this , state ) ) ;
133-
134- if ( false == Object . ReferenceEquals ( _senderLink , sender ) )
135- {
136- // TODO log this case?
137- }
138-
139- PublishOutcome publishOutcome ;
140- switch ( outcome )
141- {
142- case Rejected rejectedOutcome :
143- {
144- const OutcomeState publishState = OutcomeState . Rejected ;
145- publishOutcome = new PublishOutcome ( publishState ,
146- Utils . ConvertError ( rejectedOutcome . Error ) ) ;
147- _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . REJECTED ) ;
148- break ;
149- }
150- case Released :
151- {
152- const OutcomeState publishState = OutcomeState . Released ;
153- publishOutcome = new PublishOutcome ( publishState , null ) ;
154- _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . RELEASED ) ;
155- break ;
156- }
157- case Accepted :
158- {
159- const OutcomeState publishState = OutcomeState . Accepted ;
160- publishOutcome = new PublishOutcome ( publishState , null ) ;
161- _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . ACCEPTED ) ;
162- break ;
163- }
164- default :
165- {
166- throw new NotSupportedException ( ) ;
167- }
168- }
134+ // TODO log this case?
135+ }
169136
170- // TODO cancellation token
171- if ( _metricsReporter is not null && stopwatch is not null )
172- {
173- stopwatch . Stop ( ) ;
174- _metricsReporter . Published ( stopwatch . Elapsed ) ;
175- }
137+ PublishOutcome publishOutcome ;
138+ switch ( outcome )
139+ {
140+ case Rejected rejectedOutcome :
141+ {
142+ const OutcomeState publishState = OutcomeState . Rejected ;
143+ publishOutcome = new PublishOutcome ( publishState ,
144+ Utils . ConvertError ( rejectedOutcome . Error ) ) ;
145+ _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . REJECTED ) ;
146+ break ;
147+ }
148+ case Released :
149+ {
150+ const OutcomeState publishState = OutcomeState . Released ;
151+ publishOutcome = new PublishOutcome ( publishState , null ) ;
152+ _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . RELEASED ) ;
153+ break ;
154+ }
155+ case Accepted :
156+ {
157+ const OutcomeState publishState = OutcomeState . Accepted ;
158+ publishOutcome = new PublishOutcome ( publishState , null ) ;
159+ _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . ACCEPTED ) ;
160+ break ;
161+ }
162+ default :
163+ {
164+ throw new NotSupportedException ( ) ;
165+ }
166+ }
176167
177- var publishResult = new PublishResult ( message , publishOutcome ) ;
178- publishResultTcs . SetResult ( publishResult ) ;
168+ // TODO cancellation token
169+ if ( _metricsReporter is not null && stopwatch is not null )
170+ {
171+ stopwatch . Stop ( ) ;
172+ _metricsReporter . Published ( stopwatch . Elapsed ) ;
179173 }
180174
181- /*
182- * Note: do NOT use SendAsync here as it prevents the Closed event from
183- * firing on the native connection. Bizarre, I know!
184- */
185- _senderLink . Send ( nativeMessage , OutcomeCallback , this ) ;
175+ var publishResult = new PublishResult ( message , publishOutcome ) ;
176+ publishResultTcs . SetResult ( publishResult ) ;
177+ }
186178
187- return publishResultTcs . Task ;
179+ /*
180+ * Note: do NOT use SendAsync here as it prevents the Closed event from
181+ * firing on the native connection. Bizarre, I know!
182+ */
183+ try
184+ {
185+ _senderLink . Send ( nativeMessage , OutcomeCallback , this ) ;
188186 }
189- catch ( AmqpException ex )
187+ catch ( AmqpException amqpException )
190188 {
191189 stopwatch ? . Stop ( ) ;
192190 _metricsReporter ? . PublishDisposition ( IMetricsReporter . PublishDispositionValue . REJECTED ) ;
193- var publishOutcome = new PublishOutcome ( OutcomeState . Rejected , Utils . ConvertError ( ex . Error ) ) ;
191+ var publishOutcome = new PublishOutcome ( OutcomeState . Rejected , Utils . ConvertError ( amqpException . Error ) ) ;
194192 var publishResult = new PublishResult ( message , publishOutcome ) ;
195193 publishResultTcs . SetResult ( publishResult ) ;
196- return publishResultTcs . Task ;
197194 }
198- catch ( Exception e )
195+ catch ( Exception ex )
199196 {
200- throw new PublisherException ( $ "{ ToString ( ) } Failed to publish message, { e } ") ;
197+ var publisherException = new PublisherException ( $ "{ ToString ( ) } Failed to publish message", ex ) ;
198+ publishResultTcs . SetException ( publisherException ) ;
201199 }
200+
201+ return publishResultTcs . Task ;
202202 }
203203
204204 public override async Task CloseAsync ( )
0 commit comments