本文整理汇总了C#中ITransactionContext.GetOrAdd方法的典型用法代码示例。如果您正苦于以下问题:C# ITransactionContext.GetOrAdd方法的具体用法?C# ITransactionContext.GetOrAdd怎么用?C# ITransactionContext.GetOrAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITransactionContext
的用法示例。
在下文中一共展示了ITransactionContext.GetOrAdd方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Send
public async Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
{
var outgoingMessages = context.GetOrAdd(OutgoingMessagesItemsKey, () =>
{
context.OnCommitted(async () => SendOutgoingMessages(context));
return new ConcurrentQueue<OutgoingMessage>();
});
outgoingMessages.Enqueue(new OutgoingMessage(destinationAddress, message));
}
示例2: GetConnection
Task<IDbConnection> GetConnection(ITransactionContext context)
{
return context
.GetOrAdd(CurrentConnectionKey,
async () =>
{
var dbConnection = await _connectionProvider.GetConnection();
context.OnCommitted(async () => await dbConnection.Complete());
context.OnDisposed(() =>
{
dbConnection.Dispose();
});
return dbConnection;
});
}
示例3: GetClientFromTransactionContext
private AmazonSQSClient GetClientFromTransactionContext(ITransactionContext context)
{
return context.GetOrAdd(ClientContextKey, () =>
{
var amazonSqsClient = new AmazonSQSClient(_accessKeyId, _secretAccessKey, new AmazonSQSConfig()
{
RegionEndpoint = _regionEndpoint
});
context.OnDisposed(amazonSqsClient.Dispose);
return amazonSqsClient;
});
}
示例4: Send
public Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
{
if (destinationAddress == null) throw new ArgumentNullException("destinationAddress");
if (message == null) throw new ArgumentNullException("message");
if (context == null) throw new ArgumentNullException("context");
var outputQueue = context.GetOrAdd(OutgoingQueueContextKey, () => new InMemOutputQueue());
var contextActionsSet = context.GetOrAdd(OutgoingQueueContextActionIsSetKey, () => false);
if (!contextActionsSet)
{
context.OnCommitted(async () =>
{
var client = GetClientFromTransactionContext(context);
var messageSendRequests = outputQueue.GetMessages();
var tasks = messageSendRequests.Select(r => client.SendMessageBatchAsync(new SendMessageBatchRequest(r.DestinationAddressUrl, r.Messages.ToList())));
var response = await Task.WhenAll(tasks);
if (response.Any(r => r.Failed.Any()))
{
GenerateErrorsAndThrow(response);
}
});
context.OnAborted(outputQueue.Clear);
context.Items[OutgoingQueueContextActionIsSetKey] = true;
}
var sendMessageRequest = new SendMessageBatchRequestEntry()
{
MessageAttributes = CreateAttributesFromHeaders(message.Headers),
MessageBody = GetBody(message.Body),
Id = message.Headers.GetValueOrNull(Headers.MessageId) ?? Guid.NewGuid().ToString(),
};
outputQueue.AddMessage(GetDestinationQueueUrlByName(destinationAddress, context), sendMessageRequest);
return _emptyTask;
}
示例5: Send
/// <summary>
/// Sends the given transport message to the specified destination address using MSMQ. Will use the existing <see cref="MessageQueueTransaction"/> stashed
/// under the <see cref="CurrentTransactionKey"/> key in the given <paramref name="context"/>, or else it will create one and add it.
/// </summary>
public async Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
{
if (destinationAddress == null) throw new ArgumentNullException("destinationAddress");
if (message == null) throw new ArgumentNullException("message");
if (context == null) throw new ArgumentNullException("context");
var logicalMessage = CreateMsmqMessage(message);
var messageQueueTransaction = context.GetOrAdd(CurrentTransactionKey, () =>
{
var messageQueueTransaction1 = new MessageQueueTransaction();
messageQueueTransaction1.Begin();
context.OnCommitted(async () => messageQueueTransaction1.Commit());
return messageQueueTransaction1;
});
var sendQueues = context.GetOrAdd(CurrentOutgoingQueuesKey, () =>
{
var messageQueues = new ConcurrentDictionary<string, MessageQueue>(StringComparer.InvariantCultureIgnoreCase);
context.OnDisposed(() =>
{
foreach (var messageQueue in messageQueues.Values)
{
messageQueue.Dispose();
}
});
return messageQueues;
});
var path = MsmqUtil.GetFullPath(destinationAddress);
var sendQueue = sendQueues.GetOrAdd(path, _ =>
{
var messageQueue = new MessageQueue(path, QueueAccessMode.Send);
return messageQueue;
});
sendQueue.Send(logicalMessage, messageQueueTransaction);
}
示例6: GetOutgoingMessages
ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>> GetOutgoingMessages(ITransactionContext context)
{
return context.GetOrAdd(OutgoingMessagesKey, () =>
{
var destinations = new ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>>();
context.OnCommitted(async () =>
{
// send outgoing messages
foreach (var destinationAndMessages in destinations)
{
var destinationAddress = destinationAndMessages.Key;
var messages = destinationAndMessages.Value;
_log.Debug("Sending {0} messages to {1}", messages.Count, destinationAddress);
var sendTasks = messages
.Select(async message =>
{
await GetRetrier().Execute(async () =>
{
using (var brokeredMessageToSend = CreateBrokeredMessage(message))
{
await GetQueueClient(destinationAddress).SendAsync(brokeredMessageToSend);
}
});
})
.ToArray();
await Task.WhenAll(sendTasks);
}
});
return destinations;
});
}
示例7: GetOutgoingMessages
ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>> GetOutgoingMessages(ITransactionContext context)
{
return context.GetOrAdd(OutgoingMessagesKey, () =>
{
var destinations = new ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>>();
context.OnCommitted(async () =>
{
// send outgoing messages
foreach (var destinationAndMessages in destinations)
{
var destinationAddress = destinationAndMessages.Key;
var messages = destinationAndMessages.Value;
var sendTasks = messages
.Select(async message =>
{
await GetRetrier().Execute(async () =>
{
using (var brokeredMessageToSend = CreateBrokeredMessage(message))
{
try
{
await Send(destinationAddress, brokeredMessageToSend);
}
catch (MessagingEntityNotFoundException exception)
{
throw new MessagingEntityNotFoundException($"Could not send to '{destinationAddress}'!", exception);
}
}
});
})
.ToArray();
await Task.WhenAll(sendTasks);
}
});
return destinations;
});
}
示例8: Send
public async Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
{
if (destinationAddress == null) throw new ArgumentNullException(nameof(destinationAddress));
if (message == null) throw new ArgumentNullException(nameof(message));
if (context == null) throw new ArgumentNullException(nameof(context));
var outgoingMessages = context.GetOrAdd(OutgoingMessagesItemsKey, () =>
{
var sendMessageBatchRequestEntries = new ConcurrentQueue<OutgoingMessage>();
context.OnCommitted(async () =>
{
await SendOutgoingMessages(sendMessageBatchRequestEntries, context);
});
return sendMessageBatchRequestEntries;
});
outgoingMessages.Enqueue(new OutgoingMessage(destinationAddress, message));
}
示例9: GetModel
IModel GetModel(ITransactionContext context)
{
var model = context.GetOrAdd(CurrentModelItemsKey, () =>
{
var connection = _connectionManager.GetConnection();
var newModel = connection.CreateModel();
context.OnDisposed(() => newModel.Dispose());
return newModel;
});
return model;
}
示例10: GetOutgoingMessages
ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>> GetOutgoingMessages(ITransactionContext context)
{
return context.GetOrAdd(OutgoingMessagesKey, () =>
{
var destinations = new ConcurrentDictionary<string, ConcurrentQueue<TransportMessage>>();
context.OnCommitted(async () =>
{
// send outgoing messages
foreach (var destinationAndMessages in destinations)
{
var destinationAddress = destinationAndMessages.Key;
var messages = destinationAndMessages.Value;
var sendTasks = messages
.Select(async message =>
{
await GetRetrier().Execute(async () =>
{
using (var brokeredMessageToSend = MsgHelpers.CreateBrokeredMessage(message))
{
try
{
await GetQueueClient(destinationAddress).SendAsync(brokeredMessageToSend);
}
catch (MessagingEntityNotFoundException exception)
{
// do NOT rethrow as MessagingEntityNotFoundException because it has its own ToString that swallows most of the info!!
throw new MessagingException($"Could not send to '{destinationAddress}'!", false, exception);
}
}
});
})
.ToArray();
await Task.WhenAll(sendTasks);
}
});
return destinations;
});
}