本文整理汇总了C#中BrokeredMessage.CompleteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# BrokeredMessage.CompleteAsync方法的具体用法?C# BrokeredMessage.CompleteAsync怎么用?C# BrokeredMessage.CompleteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BrokeredMessage
的用法示例。
在下文中一共展示了BrokeredMessage.CompleteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessMessage
private async Task ProcessMessage(BrokeredMessage message)
{
try
{
if (!this.IsValidMessage(message))
{
// Send the message to the Dead Letter queue for further analysis.
await message.DeadLetterAsync("Invalid message", "The message Id is invalid");
Trace.WriteLine("Invalid Message. Sending to Dead Letter queue");
}
// Simulate message processing.
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
Trace.WriteLine("Consumer " + RoleEnvironment.CurrentRoleInstance.Id + " : Message processed successfully: " + message.MessageId);
// Complete the message.
await message.CompleteAsync();
}
catch (Exception ex)
{
// Abandon the message when appropriate. If the message reaches the MaxDeliveryCount limit, it will be automatically deadlettered.
message.Abandon();
Trace.TraceError("An error has occurred while processing the message: " + ex.Message);
}
}
示例2: HandleDispatchCompletion
private async Task HandleDispatchCompletion(Task t, BrokeredMessage m)
{
if (t.IsFaulted)
{
var exception = t.Exception;
Logger.Error(exception, "Message dispatch failed");
await m.AbandonAsync(ExceptionDetailsAsProperties(exception));
return;
}
Logger.Debug("Dispatched message: {0} from {1}", m, m.ReplyTo);
await m.CompleteAsync();
}
示例3: WaitAndAbandonMessage
async Task IMessageSessionAsyncHandler.OnMessageAsync(MessageSession session, BrokeredMessage message)
{
if (_receiver.IsShuttingDown)
{
await WaitAndAbandonMessage(message).ConfigureAwait(false);
return;
}
using (var delivery = _tracker.BeginDelivery())
{
if (_log.IsDebugEnabled)
_log.DebugFormat("Receiving {0}:{1}({3}) - {2}", delivery.Id, message.MessageId, _receiver.QueuePath, session.SessionId);
var context = new ServiceBusReceiveContext(_receiver.InputAddress, message, _context, _sendEndpointProvider, _publishEndpointProvider);
context.GetOrAddPayload<MessageSessionContext>(() => new BrokeredMessageSessionContext(session));
context.GetOrAddPayload(() => _context);
try
{
await _context.PreReceive(context).ConfigureAwait(false);
await _receiver.ReceivePipe.Send(context).ConfigureAwait(false);
await context.CompleteTask.ConfigureAwait(false);
await message.CompleteAsync().ConfigureAwait(false);
await _context.PostReceive(context).ConfigureAwait(false);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receive completed: {0}", message.MessageId);
}
catch (Exception ex)
{
if (_log.IsErrorEnabled)
_log.Error($"Received faulted: {message.MessageId}", ex);
await message.AbandonAsync().ConfigureAwait(false);
await _context.ReceiveFault(context, ex).ConfigureAwait(false);
}
finally
{
context.Dispose();
}
}
}
示例4: CompleteProcessingMessageAsync
/// <summary>
/// This method completes processing of the specified message, after the job function has been invoked.
/// </summary>
/// <param name="message">The message to complete processing for.</param>
/// <param name="result">The <see cref="FunctionResult"/> from the job invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns>A <see cref="Task"/> that will complete the message processing.</returns>
public virtual async Task CompleteProcessingMessageAsync(BrokeredMessage message, FunctionResult result, CancellationToken cancellationToken)
{
if (result.Succeeded)
{
if (!MessageOptions.AutoComplete)
{
// AutoComplete is true by default, but if set to false
// we need to complete the message
cancellationToken.ThrowIfCancellationRequested();
await message.CompleteAsync();
}
}
else
{
cancellationToken.ThrowIfCancellationRequested();
await message.AbandonAsync();
}
}
示例5: WaitAndAbandonMessage
async Task IMessageSessionAsyncHandler.OnMessageAsync(MessageSession session, BrokeredMessage message)
{
if (_receiver.IsShuttingDown)
{
await WaitAndAbandonMessage(message).ConfigureAwait(false);
return;
}
var deliveryCount = _receiver.IncrementDeliveryCount();
if (_log.IsDebugEnabled)
_log.DebugFormat("Receiving {0}:{1}({3}) - {2}", deliveryCount, message.MessageId, _receiver.QueuePath, session.SessionId);
var context = new ServiceBusReceiveContext(_receiver.InputAddress, message, _receiver.ReceiveObserver);
context.GetOrAddPayload<MessageSessionContext>(() => new BrokeredMessageSessionContext(session));
try
{
await _receiver.ReceiveObserver.PreReceive(context).ConfigureAwait(false);
await _receiver.ReceivePipe.Send(context).ConfigureAwait(false);
await context.CompleteTask.ConfigureAwait(false);
await message.CompleteAsync().ConfigureAwait(false);
await _receiver.ReceiveObserver.PostReceive(context).ConfigureAwait(false);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receive completed: {0}", message.MessageId);
}
catch (Exception ex)
{
if (_log.IsErrorEnabled)
_log.Error($"Received faulted: {message.MessageId}", ex);
await message.AbandonAsync().ConfigureAwait(false);
await _receiver.ReceiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
}
finally
{
_receiver.DeliveryComplete();
}
}
示例6: ProcessMessageAsync
internal async Task ProcessMessageAsync(BrokeredMessage message, CancellationToken cancellationToken)
{
if (message == null)
{
Trace.TraceWarning("ProcessMessageAsync was called with a null BrokeredMessage");
return;
}
if (cancellationToken.IsCancellationRequested == false)
{
Trace.TraceInformation("Receiving BrokeredMessage with Correlation Id {0}", message.CorrelationId);
try
{
// Do actual processing of the message
var topicMessage = message.GetBody<TopicMessageModel>();
Trace.TraceInformation("Processing message for {0}, sent by {1}, disco? {2}", topicMessage.Message, topicMessage.Sender, topicMessage.Disco);
// start disco
var context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
context.Clients.All.addDisco(topicMessage.Message);
// after 10 mins, stop it
// Complete the message
await message.CompleteAsync();
}
catch (MessageLockLostException e)
{
Trace.TraceError("Completing a BrokeredMessage in ProcessMessageAsync throw a MessageLockLostException");
}
catch (MessagingException e)
{
Trace.TraceError("Completing a BrokeredMessage in ProcessMessageAsync throw a MessagingException");
}
catch (Exception e)
{
Trace.TraceError("An exception occured while trying to process a deployment request message");
}
message.Dispose();
}
}
示例7: Dispatch
private async Task Dispatch(BrokeredMessage message)
{
try
{
Exception exception = null;
try
{
_logger.Debug("Dispatching message: {0} from {1}", message, message.ReplyTo);
await _dispatcher.Dispatch(message);
_logger.Debug("Dispatched message: {0} from {1}", message, message.ReplyTo);
_logger.Debug("Completing message {0}", message);
await message.CompleteAsync();
_logger.Debug("Completed message {0}", message);
return;
}
catch (Exception exc)
{
exception = exc;
}
_logger.Error(exception, "Message dispatch failed");
try
{
_logger.Debug("Abandoning message {0} from {1}", message, message.ReplyTo);
await message.AbandonAsync(exception.ExceptionDetailsAsProperties(_clock.UtcNow));
_logger.Debug("Abandoned message {0} from {1}", message, message.ReplyTo);
}
catch (Exception exc)
{
_logger.Error(exc, "Could not call Abandon() on message {0} from {1}. Possible lock expiry?", message, message.ReplyTo);
}
}
catch (Exception exc)
{
_logger.Error(exc, "Unhandled exception in message pump");
}
}
示例8: cmdSend2_Click
protected async void cmdSend2_Click(object sender, EventArgs e)
{
byte[] dataArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
BrokeredMessage msg = new BrokeredMessage(dataArr);
msg.Properties.Add("suma", 1);
msg.ReplyToSessionId = Guid.NewGuid().ToString("N");
await m_tc.SendAsync(msg);
MessageSession session = m_qc.AcceptMessageSession(msg.ReplyToSessionId, TimeSpan.FromSeconds(60));
List<BrokeredMessage> lst = new List<BrokeredMessage>();
while (lst.Count < 2)
{
msg = await session.ReceiveAsync();
if (msg != null)
{
lst.Add(msg);
await msg.CompleteAsync();
}
}
lblInfo.Text = "";
foreach (var item in lst) lblInfo.Text += item.GetBody<string>();
}
示例9: ProcessMessageTask
private async Task ProcessMessageTask(BrokeredMessage receivedMessage)
{
try
{
// Process the message
Trace.WriteLine("Processing received messages");
if (!IsValid(receivedMessage))
{
await receivedMessage.DeadLetterAsync("Invalid message", "Message Id is invalid or there is no message body");
Trace.WriteLine("Invalid message. Sending to dead letter queue");
}
await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false);
var messageData = receivedMessage.GetBody<MessageData>();
//var roleInstanceId = RoleEnvironment.IsAvailable
// ? RoleEnvironment.CurrentRoleInstance.Id
// : string.Empty;
var traceMsg = string.Format("Received message with sequence #: {0}, Id: {1}, MessageBodyId:{2}, MessageData:{3}, PartitionKey:{4}, RowKey:{5} by Role:{6}",
receivedMessage.SequenceNumber, receivedMessage.MessageId, messageData.Id, messageData.Data, ""
, messageData.PartitionKey, messageData.RowKey);
//var traceMsg = string.Format("Received message with sequence #: {0}, Id: {1}, MessageBodyId:{2}, MessageData:{3} by Role:{4}",
// receivedMessage.SequenceNumber,receivedMessage.MessageId,messageData.Id,messageData.Data, RoleEnvironment.CurrentRoleInstance.Id);
Trace.WriteLine(traceMsg);
var insertOp = TableOperation.Insert(messageData);
_tableRef.Execute(insertOp);
await receivedMessage.CompleteAsync();
}
catch (Exception ex)
{
receivedMessage.Abandon();
Trace.TraceError("Exception processing message: {0}", ex.Message);
if (ex.InnerException != null)
{
Trace.TraceError("Inner Exception: {0}", ex.InnerException.Message);
}
}
}
示例10: NotificationReceived
private async Task NotificationReceived(BrokeredMessage m)
{
var update = m.GetBody<Update>(_xpsUpdate);
await _nodesUpdateTopic.SendAsync(new BrokeredMessage(new Update { Destination = update.Destination, Flight = update.Flight, Url = update.Url }, _xpsUpdate) { ContentType = typeof(Update).FullName });//.ConfigureAwait(false);
await m.CompleteAsync();//.ConfigureAwait(false);
}
示例11: Respond
async Task Respond(BrokeredMessage request, Func<BrokeredMessage, Task<BrokeredMessage>> handleRequest)
{
// evaluate ReplyTo
if (!string.IsNullOrEmpty(request.ReplyTo))
{
Uri targetUri;
if (Uri.TryCreate(request.ReplyTo, UriKind.RelativeOrAbsolute, out targetUri))
{
// make the URI absolute to this namespace
if (!targetUri.IsAbsoluteUri)
{
targetUri = new Uri(this.namespaceUri, targetUri);
}
var replyToken = GetReplyToken(targetUri);
if (replyToken == null)
{
await request.DeadLetterAsync("NoReplyToToken", "No 'tk' query parameter in ReplyTo field URI found");
return;
}
// truncate the query portion of the URI
targetUri = new Uri(targetUri.GetLeftPart(UriPartial.Path));
// now we're reasonably confident that the input message can be
// replied to, so let's execute the message processing
try
{
// call the callback
var reply = await handleRequest(request);
// set the correlation-id on the reply
reply.CorrelationId = request.MessageId;
var replyDestination = this.GetOrCreateReplyDestination(replyToken, targetUri);
var sender = await replyDestination.Factory.CreateMessageSenderAsync(targetUri.AbsolutePath.Substring(1));
await sender.SendAsync(reply);
await request.CompleteAsync();
}
catch (Exception e)
{
await request.DeadLetterAsync("ErrorHandlingMessage", e.Message);
}
}
else
{
await request.DeadLetterAsync("NoReplyTo", "No ReplyTo field found");
}
}
}
示例12: BookFlight
public static async Task BookFlight(BrokeredMessage message, MessageSender nextStepQueue, MessageSender compensatorQueue)
{
try
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
var via = (message.Properties.ContainsKey("Via")
? ((string) message.Properties["Via"] + ",")
: string.Empty) +
"bookflight";
if (message.Label != null &&
message.ContentType != null &&
message.Label.Equals(TravelBookingLabel, StringComparison.InvariantCultureIgnoreCase) &&
message.ContentType.Equals(ContentTypeApplicationJson, StringComparison.InvariantCultureIgnoreCase))
{
var body = message.GetBody<Stream>();
dynamic travelBooking = DeserializeTravelBooking(body);
// do we want to book a flight? No? Let's just forward the message to
// the next destination via transfer queue
if (travelBooking.flight == null)
{
await nextStepQueue.SendAsync(CreateForwardMessage(message, travelBooking, via));
// done with this job
await message.CompleteAsync();
}
else
{
lock (Console.Out)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Booking Flight");
Console.ResetColor();
}
// now we're going to simulate the work of booking a flight,
// which usually involves a call to a third party
// every 9th flight booking sadly goes wrong
if (message.SequenceNumber%9 == 0)
{
await message.DeadLetterAsync(
new Dictionary<string, object>
{
{"DeadLetterReason", "TransactionError"},
{"DeadLetterErrorDescription", "Failed to perform flight reservation"},
{"Via", via}
});
}
else
{
// every operation executed in the first 3 secs of any minute
// tanks completely (simulates some local or external unexpected issue)
if (DateTime.UtcNow.Second <= 3)
{
throw new Exception("O_o");
}
// let's pretend we booked something
travelBooking.flight.reservationId = "A1B2C3";
await nextStepQueue.SendAsync(CreateForwardMessage(message, travelBooking, via));
// done with this job
await message.CompleteAsync();
}
}
}
else
{
await message.DeadLetterAsync(
new Dictionary<string, object>
{
{"DeadLetterReason", "BadMessage"},
{"DeadLetterErrorDescription", "Unrecognized input message"},
{"Via", via}
});
}
scope.Complete();
}
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
await message.AbandonAsync();
}
}
示例13: OnMessage
async Task OnMessage(BrokeredMessage message)
{
if (_shuttingDown)
{
await WaitAndAbandonMessage(message);
return;
}
var current = Interlocked.Increment(ref _currentPendingDeliveryCount);
while (current > _maxPendingDeliveryCount)
Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);
var deliveryCount = Interlocked.Increment(ref _deliveryCount);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receiving {0}:{1} - {2}", deliveryCount, message.MessageId, _receiveSettings.QueueDescription.Path);
var context = new ServiceBusReceiveContext(_inputAddress, message, _receiveObserver);
try
{
await _receiveObserver.PreReceive(context).ConfigureAwait(false);
await _receivePipe.Send(context).ConfigureAwait(false);
await context.CompleteTask.ConfigureAwait(false);
await message.CompleteAsync().ConfigureAwait(false);
await _receiveObserver.PostReceive(context).ConfigureAwait(false);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receive completed: {0}", message.MessageId);
}
catch (Exception ex)
{
if (_log.IsErrorEnabled)
_log.Error($"Received faulted: {message.MessageId}", ex);
await message.AbandonAsync().ConfigureAwait(false);
await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
}
finally
{
var pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
if (pendingCount == 0 && _shuttingDown)
{
if (_log.IsDebugEnabled)
_log.DebugFormat("Receiver shutdown completed: {0}", _inputAddress);
_participant.SetComplete();
}
}
}
示例14: OnMessage
async Task OnMessage(BrokeredMessage message)
{
int current = Interlocked.Increment(ref _currentPendingDeliveryCount);
while (current > _maxPendingDeliveryCount)
Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);
long deliveryCount = Interlocked.Increment(ref _deliveryCount);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receiving {0}:{1} - {2}", deliveryCount, message.MessageId, _receiveSettings.QueueDescription.Path);
var context = new ServiceBusReceiveContext(_inputAddress, message, _receiveObserver);
try
{
if (_shuttingDown)
{
await _completeTask.Task.ConfigureAwait(false);
throw new TransportException(_inputAddress, "Transport shutdown in progress, abandoning message");
}
await _receiveObserver.PreReceive(context).ConfigureAwait(false);
await _receivePipe.Send(context).ConfigureAwait(false);
await context.CompleteTask.ConfigureAwait(false);
await message.CompleteAsync().ConfigureAwait(false);
await _receiveObserver.PostReceive(context).ConfigureAwait(false);
if (_log.IsDebugEnabled)
_log.DebugFormat("Receive completed: {0}", message.MessageId);
}
catch (Exception ex)
{
if (_log.IsErrorEnabled)
_log.Error($"Received faulted: {message.MessageId}", ex);
await message.AbandonAsync().ConfigureAwait(false);
await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
}
finally
{
int pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
if (pendingCount == 0 && _shuttingDown)
_completeTask.TrySetResult(this);
}
}
示例15: CancelRentalCar
public static async Task CancelRentalCar(BrokeredMessage message, MessageSender nextStepQueue, MessageSender compensatorQueue)
{
try
{
var via = (message.Properties.ContainsKey("Via")
? ((string) message.Properties["Via"] + ",")
: string.Empty) +
"cancelcar";
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
if (message.Label != null &&
message.ContentType != null &&
message.Label.Equals(TravelBookingLabel, StringComparison.InvariantCultureIgnoreCase) &&
message.ContentType.Equals(ContentTypeApplicationJson, StringComparison.InvariantCultureIgnoreCase))
{
var body = message.GetBody<Stream>();
dynamic travelBooking = DeserializeTravelBooking(body);
// do we want to book a flight? No? Let's just forward the message to
// the next destination via transfer queue
if (travelBooking.car != null &&
travelBooking.car.reservationId != null)
{
lock (Console.Out)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cancelling Rental Car");
Console.ResetColor();
}
// undo the reservation (or pretend to fail)
if (DateTime.UtcNow.Second <= 3)
{
throw new Exception("O_o");
}
// reset the id
travelBooking.car.reservationId = null;
// forward
await nextStepQueue.SendAsync(CreateForwardMessage(message, travelBooking, via));
}
else
{
await nextStepQueue.SendAsync(CreateForwardMessage(message, travelBooking, via));
}
// done with this job
await message.CompleteAsync();
}
else
{
await message.DeadLetterAsync(
new Dictionary<string, object>
{
{"DeadLetterReason", "BadMessage"},
{"DeadLetterErrorDescription", "Unrecognized input message"},
{"Via", via}
});
}
scope.Complete();
}
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
await message.AbandonAsync();
}
}