Example #1
0
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend)
        {
            var context = new InMemorySendContext <T>(message, cancelSend);

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

                var messageId = context.MessageId ?? NewId.NextGuid();

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

                var transportMessage = new InMemoryTransportMessage(messageId, context.Body, context.ContentType.MediaType, TypeMetadataCache <T> .ShortName);

                Interlocked.Increment(ref _queueDepth);

#pragma warning disable 4014
                Task.Factory.StartNew(() => DispatchMessage(transportMessage), _supervisor.StoppedToken, TaskCreationOptions.None, _scheduler);
#pragma warning restore 4014

                context.LogSent();

                await _sendObservable.PostSend(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                context.LogFaulted(ex);

                await _sendObservable.SendFault(context, ex).ConfigureAwait(false);

                throw;
            }
        }
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend)
        {
            var context = new InMemorySendContext <T>(message, cancelSend);

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

                var messageId = context.MessageId ?? NewId.NextGuid();

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

                var transportMessage = new InMemoryTransportMessage(messageId, context.Body, context.ContentType.MediaType, TypeMetadataCache <T> .ShortName);

                await _exchange.Send(transportMessage).ConfigureAwait(false);

                context.LogSent();

                await _sendObservable.PostSend(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                context.LogFaulted(ex);

                await _sendObservable.SendFault(context, ex).ConfigureAwait(false);

                throw;
            }
        }
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            LogContext.SetCurrentIfNull(_context.LogContext);

            var context = new InMemorySendContext <T>(message, cancellationToken);

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

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

                activity.AddSendContextHeaders(context);

                var messageId = context.MessageId ?? NewId.NextGuid();

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

                var transportMessage = new InMemoryTransportMessage(messageId, context.Body, context.ContentType.MediaType, TypeMetadataCache <T> .ShortName);

                await _context.Exchange.Send(transportMessage).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();
            }
        }