Ejemplo n.º 1
0
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            if (IsStopped)
            {
                throw new TransportUnavailableException($"The send transport is stopped: {_entityName}");
            }

            IPipe <ClientContext> modelPipe = Pipe.New <ClientContext>(p =>
            {
                p.UseFilter(_filter);

                p.UseExecuteAsync(async clientContext =>
                {
                    var sendContext = new TransportAmazonSqsSendContext <T>(message, cancellationToken);
                    try
                    {
                        await pipe.Send(sendContext).ConfigureAwait(false);

                        var transportMessage = clientContext.CreateSendRequest(_entityName, sendContext.Body);

                        transportMessage.MessageAttributes.Set(sendContext.Headers);

                        if (!string.IsNullOrEmpty(sendContext.DeduplicationId))
                        {
                            transportMessage.MessageDeduplicationId = sendContext.DeduplicationId;
                        }

                        if (!string.IsNullOrEmpty(sendContext.GroupId))
                        {
                            transportMessage.MessageGroupId = sendContext.GroupId;
                        }

                        transportMessage.MessageAttributes.Set("Content-Type", sendContext.ContentType.MediaType);
                        transportMessage.MessageAttributes.Set(nameof(sendContext.MessageId), sendContext.MessageId);
                        transportMessage.MessageAttributes.Set(nameof(sendContext.CorrelationId), sendContext.CorrelationId);
                        transportMessage.MessageAttributes.Set(nameof(sendContext.TimeToLive), sendContext.TimeToLive);

                        await _observers.PreSend(sendContext).ConfigureAwait(false);

                        await clientContext.SendMessage(transportMessage, sendContext.CancellationToken).ConfigureAwait(false);

                        sendContext.LogSent();

                        await _observers.PostSend(sendContext).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        sendContext.LogFaulted(ex);

                        await _observers.SendFault(sendContext, ex).ConfigureAwait(false);

                        throw;
                    }
                });
            });

            await _clientSource.Send(modelPipe, cancellationToken).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
            public async Task Send(ClientContext clientContext)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                await _context.ConfigureTopologyPipe.Send(clientContext).ConfigureAwait(false);

                var context = new TransportAmazonSqsSendContext <T>(_message, _cancellationToken);

                var activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartActivity(new { _context.EntityName });

                try
                {
                    await _pipe.Send(context).ConfigureAwait(false);

                    activity.AddSendContextHeaders(context);

                    var request = clientContext.CreatePublishRequest(_context.EntityName, context.Body);

                    _context.SnsSetHeaderAdapter.Set(request.MessageAttributes, context.Headers);

                    _context.SnsSetHeaderAdapter.Set(request.MessageAttributes, "Content-Type", context.ContentType.MediaType);
                    _context.SnsSetHeaderAdapter.Set(request.MessageAttributes, nameof(context.CorrelationId), context.CorrelationId);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(context).ConfigureAwait(false);
                    }

                    await clientContext.Publish(request, context.CancellationToken).ConfigureAwait(false);

                    context.LogSent();

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(context).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    context.LogFaulted(ex);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(context, ex).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Ejemplo n.º 3
0
            public async Task Send(ClientContext context)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                await _context.ConfigureTopologyPipe.Send(context).ConfigureAwait(false);

                var sendContext = new TransportAmazonSqsSendContext <T>(_message, _cancellationToken);

                await _pipe.Send(sendContext).ConfigureAwait(false);

                StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(sendContext);

                try
                {
                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(sendContext).ConfigureAwait(false);
                    }

                    var message = new SendMessageBatchRequestEntry("", Encoding.UTF8.GetString(sendContext.Body));

                    _context.SqsSetHeaderAdapter.Set(message.MessageAttributes, sendContext.Headers);

                    _context.SqsSetHeaderAdapter.Set(message.MessageAttributes, "Content-Type", sendContext.ContentType.MediaType);
                    _context.SqsSetHeaderAdapter.Set(message.MessageAttributes, nameof(sendContext.CorrelationId), sendContext.CorrelationId);

                    if (!string.IsNullOrEmpty(sendContext.DeduplicationId))
                    {
                        message.MessageDeduplicationId = sendContext.DeduplicationId;
                    }

                    if (!string.IsNullOrEmpty(sendContext.GroupId))
                    {
                        message.MessageGroupId = sendContext.GroupId;
                    }

                    if (sendContext.DelaySeconds.HasValue)
                    {
                        message.DelaySeconds = sendContext.DelaySeconds.Value;
                    }

                    await context.SendMessage(_context.EntityName, message, sendContext.CancellationToken).ConfigureAwait(false);

                    sendContext.LogSent();
                    activity.AddSendContextHeadersPostSend(sendContext);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(sendContext).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    sendContext.LogFaulted(ex);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(sendContext, ex).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Ejemplo n.º 4
0
            public async Task Send(ClientContext clientContext)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                await _context.ConfigureTopologyPipe.Send(clientContext).ConfigureAwait(false);

                var context = new TransportAmazonSqsSendContext <T>(_message, _cancellationToken);

                await _pipe.Send(context).ConfigureAwait(false);

                var activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(context);

                try
                {
                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(context).ConfigureAwait(false);
                    }

                    var request = await clientContext.CreateSendRequest(_context.EntityName, context.Body).ConfigureAwait(false);

                    _context.SqsSetHeaderAdapter.Set(request.MessageAttributes, context.Headers);

                    _context.SqsSetHeaderAdapter.Set(request.MessageAttributes, "Content-Type", context.ContentType.MediaType);
                    _context.SqsSetHeaderAdapter.Set(request.MessageAttributes, nameof(context.CorrelationId), context.CorrelationId);

                    if (!string.IsNullOrEmpty(context.DeduplicationId))
                    {
                        request.MessageDeduplicationId = context.DeduplicationId;
                    }

                    if (!string.IsNullOrEmpty(context.GroupId))
                    {
                        request.MessageGroupId = context.GroupId;
                    }

                    if (context.DelaySeconds.HasValue)
                    {
                        request.DelaySeconds = context.DelaySeconds.Value;
                    }

                    await clientContext.SendMessage(request, context.CancellationToken).ConfigureAwait(false);

                    context.LogSent();

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(context).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    context.LogFaulted(ex);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(context, ex).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Ejemplo n.º 5
0
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            if (IsStopped)
            {
                throw new TransportUnavailableException($"The send transport is stopped: {_entityName}");
            }

            IPipe <ModelContext> modelPipe = Pipe.New <ModelContext>(p =>
            {
                p.UseFilter(_filter);

                p.UseExecuteAsync(async modelContext =>
                {
                    var topicArn = await modelContext.GetTopic(_entityName).ConfigureAwait(false);

                    var sendContext = new TransportAmazonSqsSendContext <T>(message, cancellationToken);
                    try
                    {
                        await pipe.Send(sendContext).ConfigureAwait(false);

                        var transportMessage = modelContext.CreateTransportMessage(topicArn, sendContext.Body);

                        KeyValuePair <string, object>[] headers = sendContext.Headers.GetAll()
                                                                  .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().GetTypeInfo().IsValueType))
                                                                  .ToArray();

                        foreach (KeyValuePair <string, object> header in headers)
                        {
                            if (transportMessage.MessageAttributes.ContainsKey(header.Key))
                            {
                                continue;
                            }

                            transportMessage.MessageAttributes[header.Key].StringValue = header.Value.ToString();
                        }

                        transportMessage.MessageAttributes.Add("Content-Type", new MessageAttributeValue
                        {
                            DataType    = "String",
                            StringValue = sendContext.ContentType.MediaType
                        });

                        if (sendContext.MessageId.HasValue)
                        {
                            transportMessage.MessageAttributes.Add("MessageId", new MessageAttributeValue
                            {
                                DataType    = "String",
                                StringValue = sendContext.MessageId.ToString()
                            });
                        }

                        if (sendContext.CorrelationId.HasValue)
                        {
                            transportMessage.MessageAttributes.Add("CorrelationId", new MessageAttributeValue
                            {
                                DataType    = "String",
                                StringValue = sendContext.CorrelationId.ToString()
                            });
                        }

                        if (sendContext.TimeToLive.HasValue)
                        {
                            transportMessage.MessageAttributes.Add("TimeToLive", new MessageAttributeValue
                            {
                                DataType    = "Number",
                                StringValue = sendContext.TimeToLive.Value.Milliseconds.ToString()
                            });
                        }

                        await _observers.PreSend(sendContext).ConfigureAwait(false);

                        var publishTask = Task.Run(() => modelContext.Publish(transportMessage, sendContext.CancellationToken), sendContext.CancellationToken);

                        await publishTask.UntilCompletedOrCanceled(sendContext.CancellationToken).ConfigureAwait(false);

                        sendContext.LogSent();

                        await _observers.PostSend(sendContext).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        sendContext.LogFaulted(ex);

                        await _observers.SendFault(sendContext, ex).ConfigureAwait(false);

                        throw;
                    }
                });
            });

            await _modelAgent.Send(modelPipe, cancellationToken).ConfigureAwait(false);
        }