本文整理汇总了C#中ITransactionContext.OnCompleted方法的典型用法代码示例。如果您正苦于以下问题:C# ITransactionContext.OnCompleted方法的具体用法?C# ITransactionContext.OnCompleted怎么用?C# ITransactionContext.OnCompleted使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITransactionContext
的用法示例。
在下文中一共展示了ITransactionContext.OnCompleted方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Receive
public async Task<TransportMessage> Receive(ITransactionContext context)
{
if (context == null) throw new ArgumentNullException("context");
if (_inputQueueAddress == null)
{
throw new InvalidOperationException("This Amazon SQS transport does not have an input queue, hence it is not possible to reveive anything");
}
var client = GetClientFromTransactionContext(context);
var response = await client.ReceiveMessageAsync(new ReceiveMessageRequest(_queueUrl)
{
MaxNumberOfMessages = 1,
WaitTimeSeconds = 1,
AttributeNames = new List<string>(new[] { "All" }),
MessageAttributeNames = new List<string>(new[] { "All" })
});
if (response.Messages.Any())
{
var message = response.Messages.First();
var renewalTask = CreateRenewalTaskForMessage(message, client);
context.OnCompleted(async () =>
{
renewalTask.Dispose();
var result = await client.DeleteMessageBatchAsync(new DeleteMessageBatchRequest(_queueUrl,
response.Messages
.Select(m => new DeleteMessageBatchRequestEntry(m.MessageId, m.ReceiptHandle))
.ToList()));
if (result.Failed.Any())
{
GenerateErrorsAndLog(result);
}
});
context.OnAborted(() =>
{
renewalTask.Dispose();
var result = client.ChangeMessageVisibilityBatch(new ChangeMessageVisibilityBatchRequest(_queueUrl,
response.Messages
.Select(m => new ChangeMessageVisibilityBatchRequestEntry(m.MessageId, m.ReceiptHandle)
{
VisibilityTimeout = 0
}).ToList()));
if (result.Failed.Any())
{
GenerateErrorsAndLog(result);
}
});
if (MessageIsExpired(message))
{
await client.DeleteMessageAsync(new DeleteMessageRequest(_queueUrl, message.ReceiptHandle));
return null;
}
renewalTask.Start();
var transportMessage = GetTransportMessage(message);
return transportMessage;
}
return null;
}
示例2: Receive
/// <summary>
/// Receives the next message (if any) from the transport's input queue <see cref="ITransport.Address"/>
/// </summary>
public async Task<TransportMessage> Receive(ITransactionContext context)
{
var inputQueue = GetQueue(_inputQueueName);
var cloudQueueMessage = await inputQueue.GetMessageAsync(_initialVisibilityDelay, new QueueRequestOptions(), new OperationContext());
if (cloudQueueMessage == null) return null;
context.OnCompleted(async () =>
{
await inputQueue.DeleteMessageAsync(cloudQueueMessage);
});
context.OnAborted(() =>
{
inputQueue.UpdateMessage(cloudQueueMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Visibility);
});
return Deserialize(cloudQueueMessage);
}
示例3: Receive
public async Task<TransportMessage> Receive(ITransactionContext context)
{
if (_inputQueueAddress == null)
{
throw new InvalidOperationException("This Azure Service Bus transport does not have an input queue, hence it is not possible to reveive anything");
}
using (await _bottleneck.Enter())
{
var brokeredMessage = await ReceiveBrokeredMessage();
if (brokeredMessage == null) return null;
var headers = brokeredMessage.Properties
.Where(kvp => kvp.Value is string)
.ToDictionary(kvp => kvp.Key, kvp => (string)kvp.Value);
var messageId = headers.GetValueOrNull(Headers.MessageId);
var leaseDuration = (brokeredMessage.LockedUntilUtc - DateTime.UtcNow);
var lockRenewalInterval = TimeSpan.FromMinutes(0.8 * leaseDuration.TotalMinutes);
var renewalTask = GetRenewalTaskOrFakeDisposable(messageId, brokeredMessage, lockRenewalInterval);
context.OnAborted(() =>
{
renewalTask.Dispose();
try
{
brokeredMessage.Abandon();
}
catch (Exception exception)
{
// if it fails, it'll be back on the queue anyway....
_log.Warn("Could not abandon message: {0}", exception);
}
});
context.OnCommitted(async () => renewalTask.Dispose());
context.OnCompleted(async () =>
{
await brokeredMessage.CompleteAsync();
});
context.OnDisposed(() =>
{
renewalTask.Dispose();
brokeredMessage.Dispose();
});
using (var memoryStream = new MemoryStream())
{
await brokeredMessage.GetBody<Stream>().CopyToAsync(memoryStream);
return new TransportMessage(headers, memoryStream.ToArray());
}
}
}
示例4: Receive
public async Task<TransportMessage> Receive(ITransactionContext context)
{
using (await _bottleneck.Enter())
{
var brokeredMessage = await ReceiveBrokeredMessage();
if (brokeredMessage == null) return null;
var headers = brokeredMessage.Properties
.Where(kvp => kvp.Value is string)
.ToDictionary(kvp => kvp.Key, kvp => (string)kvp.Value);
var messageId = headers.GetValueOrNull(Headers.MessageId);
_log.Debug("Received brokered message with ID {0}", messageId);
var leaseDuration = (brokeredMessage.LockedUntilUtc - DateTime.UtcNow);
var lockRenewalInterval = TimeSpan.FromMinutes(0.8 * leaseDuration.TotalMinutes);
var renewalTask = GetRenewalTaskOrFakeDisposable(messageId, brokeredMessage, lockRenewalInterval);
context.OnAborted(() =>
{
renewalTask.Dispose();
_log.Debug("Abandoning message with ID {0}", messageId);
try
{
brokeredMessage.Abandon();
}
catch (Exception exception)
{
// if it fails, it'll be back on the queue anyway....
_log.Warn("Could not abandon message: {0}", exception);
}
});
context.OnCommitted(async () =>
{
renewalTask.Dispose();
});
context.OnCompleted(async () =>
{
_log.Debug("Completing message with ID {0}", messageId);
await GetRetrier().Execute(() => brokeredMessage.CompleteAsync());
});
context.OnDisposed(() =>
{
renewalTask.Dispose();
_log.Debug("Disposing message with ID {0}", messageId);
brokeredMessage.Dispose();
});
return new TransportMessage(headers, brokeredMessage.GetBody<byte[]>());
}
}
示例5: Receive
/// <summary>
/// Receives the next message (if any) from the transport's input queue <see cref="ITransport.Address"/>
/// </summary>
public async Task<TransportMessage> Receive(ITransactionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
if (_inputQueueName == null)
{
throw new InvalidOperationException("This Azure Storage Queues transport does not have an input queue, hence it is not possible to reveive anything");
}
var inputQueue = GetQueue(_inputQueueName);
var cloudQueueMessage = await inputQueue.GetMessageAsync(_initialVisibilityDelay, new QueueRequestOptions(), new OperationContext(), cancellationToken);
if (cloudQueueMessage == null) return null;
context.OnCompleted(async () =>
{
// if we get this far, don't pass on the cancellation token
await inputQueue.DeleteMessageAsync(cloudQueueMessage);
});
context.OnAborted(() =>
{
inputQueue.UpdateMessage(cloudQueueMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Visibility);
});
return Deserialize(cloudQueueMessage);
}
示例6: Receive
public async Task<TransportMessage> Receive(ITransactionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (Address == null)
{
throw new InvalidOperationException("This Amazon SQS transport does not have an input queue, hence it is not possible to reveive anything");
}
var client = GetClientFromTransactionContext(context);
var request = new ReceiveMessageRequest(_queueUrl)
{
MaxNumberOfMessages = 1,
WaitTimeSeconds = 1,
AttributeNames = new List<string>(new[] { "All" }),
MessageAttributeNames = new List<string>(new[] { "All" })
};
var response = await client.ReceiveMessageAsync(request, cancellationToken);
if (!response.Messages.Any()) return null;
var message = response.Messages.First();
var renewalTask = CreateRenewalTaskForMessage(message, client);
context.OnCompleted(async () =>
{
renewalTask.Dispose();
// if we get this far, we don't want to pass on the cancellation token
await client.DeleteMessageAsync(new DeleteMessageRequest(_queueUrl, message.ReceiptHandle));
});
context.OnAborted(() =>
{
renewalTask.Dispose();
client.ChangeMessageVisibility(_queueUrl, message.ReceiptHandle, 0);
});
if (MessageIsExpired(message))
{
// if the message is expired , we don't want to pass on the cancellation token
await client.DeleteMessageAsync(new DeleteMessageRequest(_queueUrl, message.ReceiptHandle));
return null;
}
renewalTask.Start();
var transportMessage = GetTransportMessage(message);
return transportMessage;
}
示例7: Receive
/// <summary>
/// Receives the next message from the logical input queue by loading the next file from the corresponding directory,
/// deserializing it, deleting it when the transaction is committed.
/// </summary>
public async Task<TransportMessage> Receive(ITransactionContext context, CancellationToken cancellationToken)
{
string fullPath = null;
try
{
var fileNames = Directory.GetFiles(GetDirectoryForQueueNamed(_inputQueue), "*.rebusmessage.json")
.OrderBy(f => f)
.ToList();
var index = 0;
while (index < fileNames.Count)
{
fullPath = fileNames[index++];
// attempt to capture a "lock" on the file
if (_messagesBeingHandled.TryAdd(fullPath, new object()))
break;
fullPath = null;
}
if (fullPath == null) return null;
var jsonText = await ReadAllText(fullPath);
var receivedTransportMessage = Deserialize(jsonText);
string timeToBeReceived;
if (receivedTransportMessage.Headers.TryGetValue(Headers.TimeToBeReceived, out timeToBeReceived))
{
var maxAge = TimeSpan.Parse(timeToBeReceived);
var creationTimeUtc = File.GetCreationTimeUtc(fullPath);
var nowUtc = RebusTime.Now.UtcDateTime;
var messageAge = nowUtc - creationTimeUtc;
if (messageAge > maxAge)
{
try
{
File.Delete(fullPath);
return null;
}
finally
{
object _;
_messagesBeingHandled.TryRemove(fullPath, out _);
}
}
}
context.OnCompleted(async () => File.Delete(fullPath));
context.OnDisposed(() =>
{
object _;
_messagesBeingHandled.TryRemove(fullPath, out _);
});
return receivedTransportMessage;
}
catch (IOException)
{
if (fullPath != null)
{
object _;
_messagesBeingHandled.TryRemove(fullPath, out _);
}
return null;
}
}
示例8: Receive
public async Task<TransportMessage> Receive(ITransactionContext context)
{
if (Address == null)
{
throw new InvalidOperationException("This RabbitMQ transport does not have an input queue, hence it is not possible to reveive anything");
}
var model = GetModel(context);
var result = model.BasicGet(Address, false);
if (result == null) return null;
var deliveryTag = result.DeliveryTag;
context.OnCompleted(async () =>
{
model.BasicAck(deliveryTag, false);
});
context.OnAborted(() =>
{
model.BasicNack(deliveryTag, false, true);
});
var headers = result.BasicProperties.Headers
.ToDictionary(kvp => kvp.Key, kvp =>
{
var headerValue = kvp.Value;
if (headerValue is byte[])
{
var stringHeaderValue = HeaderValueEncoding.GetString((byte[])headerValue);
return stringHeaderValue;
}
return headerValue.ToString();
});
return new TransportMessage(headers, result.Body);
}
示例9: Receive
public async Task<TransportMessage> Receive(ITransactionContext context)
{
var model = GetModel(context);
var result = model.BasicGet(Address, false);
if (result == null) return null;
var deliveryTag = result.DeliveryTag;
context.OnCompleted(async () =>
{
model.BasicAck(deliveryTag, false);
});
context.OnAborted(() =>
{
model.BasicNack(deliveryTag, false, true);
});
var headers = result.BasicProperties.Headers
.ToDictionary(kvp => kvp.Key, kvp =>
{
var headerValue = kvp.Value;
if (headerValue is byte[])
{
var stringHeaderValue = HeaderValueEncoding.GetString((byte[])headerValue);
return stringHeaderValue;
}
return headerValue.ToString();
});
return new TransportMessage(headers, result.Body);
}