예제 #1
0
        Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext =>
            {
                var context = new AzureServiceBusSendContext <T>(message, cancellationToken);

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

                    if (message is CancelScheduledMessage cancelScheduledMessage &&
                        (context.TryGetScheduledMessageId(out var sequenceNumber) ||
                         context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber)))
                    {
                        try
                        {
                            await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false);

                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath);
                            }
                        }
                        catch (MessageNotFoundException exception)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("The scheduled message was not found: {0}", exception.Message);
                            }
                        }

                        return;
                    }

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

                    var brokeredMessage = CreateBrokeredMessage(context);

                    if (context.ScheduledEnqueueTimeUtc.HasValue && context.ScheduledEnqueueTimeUtc.Value < DateTime.UtcNow)
                    {
                        var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value;

                        try
                        {
                            sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false);
                        }
                        catch (ArgumentOutOfRangeException exception)
                        {
                            brokeredMessage = CreateBrokeredMessage(context);

                            await clientContext.Send(brokeredMessage).ConfigureAwait(false);

                            sequenceNumber = 0;
                        }

                        context.SetScheduledMessageId(sequenceNumber);

                        context.LogScheduled(enqueueTimeUtc);
                    }
                    else
                    {
                        await clientContext.Send(brokeredMessage).ConfigureAwait(false);

                        context.LogSent();

                        await _observers.PostSend(context).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    await _observers.SendFault(context, ex).ConfigureAwait(false);

                    throw;
                }
            });

            return(_source.Send(clientPipe, cancellationToken));
        }
        Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext =>
            {
                var context = new AzureServiceBusSendContext <T>(message, cancellationToken);

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

                    if (message is CancelScheduledMessage cancelScheduledMessage &&
                        (context.TryGetScheduledMessageId(out var sequenceNumber) || context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber)))
                    {
                        try
                        {
                            await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false);

                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath);
                            }
                        }
                        catch (MessageNotFoundException exception)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("The scheduled message was not found: {0}", exception.Message);
                            }
                        }

                        return;
                    }

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

                    var brokeredMessage = new Message(context.Body)
                    {
                        ContentType = context.ContentType.MediaType
                    };

                    brokeredMessage.UserProperties.SetTextHeaders(context.Headers, (_, text) => text);

                    if (context.TimeToLive.HasValue)
                    {
                        brokeredMessage.TimeToLive = context.TimeToLive.Value;
                    }
                    if (context.MessageId.HasValue)
                    {
                        brokeredMessage.MessageId = context.MessageId.Value.ToString("N");
                    }
                    if (context.CorrelationId.HasValue)
                    {
                        brokeredMessage.CorrelationId = context.CorrelationId.Value.ToString("N");
                    }
                    CopyIncomingIdentifiersIfPresent(context);
                    if (context.PartitionKey != null)
                    {
                        brokeredMessage.PartitionKey = context.PartitionKey;
                    }
                    var sessionId = string.IsNullOrWhiteSpace(context.SessionId) ? context.ConversationId?.ToString("N") : context.SessionId;
                    if (!string.IsNullOrWhiteSpace(sessionId))
                    {
                        brokeredMessage.SessionId = sessionId;

                        if (context.ReplyToSessionId == null)
                        {
                            brokeredMessage.ReplyToSessionId = sessionId;
                        }
                    }
                    if (context.ReplyToSessionId != null)
                    {
                        brokeredMessage.ReplyToSessionId = context.ReplyToSessionId;
                    }
                    if (context.ScheduledEnqueueTimeUtc.HasValue)
                    {
                        var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value;

                        sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false);

                        context.SetScheduledMessageId(sequenceNumber);

                        context.LogScheduled(enqueueTimeUtc);
                    }
                    else
                    {
                        await clientContext.Send(brokeredMessage).ConfigureAwait(false);

                        context.LogSent();

                        await _observers.PostSend(context).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    await _observers.SendFault(context, ex).ConfigureAwait(false);

                    throw;
                }
            });

            return(_source.Send(clientPipe, cancellationToken));
        }
예제 #3
0
        Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext =>
            {
                var context = new AzureServiceBusSendContext <T>(message, cancellationToken);

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

                    if (message is CancelScheduledMessage cancelScheduledMessage &&
                        (context.TryGetScheduledMessageId(out var sequenceNumber) || context.TryGetSequencyNumber(cancelScheduledMessage.TokenId, out sequenceNumber)))
                    {
                        try
                        {
                            await clientContext.CancelScheduledSend(sequenceNumber).ConfigureAwait(false);

                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("Canceled Scheduled: {0} {1}", sequenceNumber, clientContext.EntityPath);
                            }
                        }
                        catch (MessageNotFoundException exception)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("The scheduled message was not found: {0}", exception.Detail.Message);
                            }
                        }

                        return;
                    }

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

                    using (var messageBodyStream = context.GetBodyStream())
                        using (var brokeredMessage = new BrokeredMessage(messageBodyStream))
                        {
                            brokeredMessage.ContentType      = context.ContentType.MediaType;
                            brokeredMessage.ForcePersistence = context.Durable;

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

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

                                brokeredMessage.Properties.Add(header.Key, header.Value);
                            }

                            if (context.TimeToLive.HasValue)
                            {
                                brokeredMessage.TimeToLive = context.TimeToLive.Value;
                            }

                            if (context.MessageId.HasValue)
                            {
                                brokeredMessage.MessageId = context.MessageId.Value.ToString("N");
                            }

                            if (context.CorrelationId.HasValue)
                            {
                                brokeredMessage.CorrelationId = context.CorrelationId.Value.ToString("N");
                            }

                            CopyIncomingIdentifiersIfPresent(context);

                            if (context.PartitionKey != null)
                            {
                                brokeredMessage.PartitionKey = context.PartitionKey;
                            }

                            var sessionId = string.IsNullOrWhiteSpace(context.SessionId) ? context.ConversationId?.ToString("N") : context.SessionId;
                            if (!string.IsNullOrWhiteSpace(sessionId))
                            {
                                brokeredMessage.SessionId = sessionId;

                                if (context.ReplyToSessionId == null)
                                {
                                    brokeredMessage.ReplyToSessionId = sessionId;
                                }
                            }

                            if (context.ReplyToSessionId != null)
                            {
                                brokeredMessage.ReplyToSessionId = context.ReplyToSessionId;
                            }

                            if (context.ScheduledEnqueueTimeUtc.HasValue)
                            {
                                var enqueueTimeUtc = context.ScheduledEnqueueTimeUtc.Value;

                                sequenceNumber = await clientContext.ScheduleSend(brokeredMessage, enqueueTimeUtc).ConfigureAwait(false);

                                context.SetScheduledMessageId(sequenceNumber);

                                context.LogScheduled(enqueueTimeUtc);
                            }
                            else
                            {
                                await clientContext.Send(brokeredMessage).ConfigureAwait(false);

                                context.LogSent();

                                await _observers.PostSend(context).ConfigureAwait(false);
                            }
                        }
                }
                catch (Exception ex)
                {
                    await _observers.SendFault(context, ex).ConfigureAwait(false);

                    throw;
                }
            });

            return(_source.Send(clientPipe, cancellationToken));
        }