Esempio n. 1
0
        public async Task <TResponse> RequestAsync <TResponse>(object message, ITraceScope traceScope)
        {
            string directory    = message.GetType().GetCustomAttribute <BusNamespace>().Directory;
            string subdirectory = message.GetType().GetCustomAttribute <BusNamespace>().Subdirectory;

            using (ITraceScope traceSubScope = traceScope.CreateSubScope("Request-" + directory + "." + subdirectory + "." + message.GetType().Name))
                return(await RequestAsync <TResponse>(message, traceSubScope.SpanId, traceSubScope.TraceId));
        }
        /// <inheritdoc />
        public string FormatDisposedMessage(ITraceScope scope)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            return($"{scope.Name} Disposed");
        }
Esempio n. 3
0
        /// <inheritdoc />
        protected override void OnScopeDisposed(ITraceScope scope)
        {
            base.OnScopeDisposed(scope);

            if (_scopeFormatter == null)
            {
                return;
            }

            var formattedMessage = _scopeFormatter.FormatDisposedMessage(scope);

            WriteMessage("SCOPE", formattedMessage);
        }
Esempio n. 4
0
        public IIncludeForRetry Subscribe <TSubscriber, TRequest>(AbstractValidator <TRequest> validator) where TSubscriber : IResponder <TRequest> where TRequest : class
        {
            //subscriber registration in a container
            Injection.AddTransient(typeof(TSubscriber));

            //retry handler
            IRetry retryHandler = new RetryHandler(MIN_RETRY_DELAY, MAX_RETRY_DELAY, RETRY_LIMIT, true);

            //creates queue and exchange
            string directory    = typeof(TRequest).GetTypeInfo().GetCustomAttribute <BusNamespace>().Directory;
            string subdirectory = typeof(TRequest).GetTypeInfo().GetCustomAttribute <BusNamespace>().Subdirectory;
            string exchange     = "request_" + directory.ToLower() + "_" + subdirectory.ToLower();
            string routingKey   = typeof(TRequest).Name.ToLower();
            string queue        = _appId.ToLower() + "-request-" + directory.ToLower() + "-" + subdirectory.ToLower() + "-" + typeof(TRequest).Name.ToLower();

            _responderChannel.ExchangeDeclare(exchange, ExchangeType.Direct, true, false);
            _responderChannel.QueueDeclare(queue, true, false, false, new Dictionary <string, object> {
                { "x-message-ttl", (int)REQUEST_TIMEOUT }, { "x-queue-mode", "default" }
            });
            _responderChannel.QueueBind(queue, exchange, routingKey);

            //request listener
            EventingBasicConsumer consumer = new EventingBasicConsumer(_responderChannel);

            consumer.Received += (obj, args) =>
            {
                Task.Factory.StartNew(async() =>
                {
                    //request message
                    string messageBody = Encoding.UTF8.GetString(args.Body.ToArray());

                    //tracing data
                    string traceSpanId      = Encoding.UTF8.GetString((byte[])args.BasicProperties.Headers["TraceSpanId"]);
                    string traceId          = Encoding.UTF8.GetString((byte[])args.BasicProperties.Headers["TraceId"]);
                    string traceDisplayName = "Respond-" + directory + "." + subdirectory + "." + typeof(TRequest).Name;

                    //response action
                    ResponseWrapper <object> responseWrapper = null;

                    await retryHandler.ExecuteAsync(async() =>
                    {
                        TRequest messageRequest = _jsonConverter.Deserialize <TRequest>(messageBody);
                        if (validator != null)
                        {
                            await validator.ValidateAndThrowAsync(messageRequest, (directory + "." + subdirectory + "." + messageRequest.GetType().Name + " is not valid"));
                        }

                        using (IServiceScope serviceScope = _serviceProvider.CreateScope())
                            using (ITraceScope traceScope = (traceSpanId != "" && traceId != "") ? new TraceScope(traceSpanId, traceId, traceDisplayName, _tracer) : new TraceScope(traceDisplayName, _tracer))
                            {
                                TSubscriber subscriber = serviceScope.ServiceProvider.GetService <TSubscriber>();
                                subscriber.Bus         = this;
                                subscriber.TraceScope  = traceScope;
                                traceScope.Attributes.Add("AppId", _appId);
                                traceScope.Attributes.Add("MessageId", args.BasicProperties.MessageId);
                                responseWrapper = new ResponseWrapper <object>(await subscriber.RespondAsync(messageRequest));
                            }

                        _logger.Send(new MessageDetail
                        {
                            Id            = args.BasicProperties.MessageId,
                            CorrelationId = args.BasicProperties.CorrelationId,
                            Type          = MessageType.Request,
                            Directory     = directory,
                            Subdirectory  = subdirectory,
                            Name          = typeof(TRequest).Name,
                            Body          = messageBody,
                            AppId         = args.BasicProperties.AppId,
                            Exception     = null,
                            ToRetry       = false
                        });
                    },
                                                    async(exception, retryIndex, retryLimit) =>
                    {
                        responseWrapper = new ResponseWrapper <object>(exception);

                        _logger.Send(new MessageDetail
                        {
                            Id            = args.BasicProperties.MessageId,
                            CorrelationId = args.BasicProperties.CorrelationId,
                            Type          = MessageType.Request,
                            Directory     = directory,
                            Subdirectory  = subdirectory,
                            Name          = typeof(TRequest).Name,
                            Body          = messageBody,
                            AppId         = args.BasicProperties.AppId,
                            Exception     = exception,
                            ToRetry       = retryIndex != retryLimit
                        });

                        await Task.CompletedTask;
                    });

                    //response message
                    if (typeof(TSubscriber).GetMethod("RespondAsync").GetCustomAttribute <FakeResponse>() == null)
                    {
                        messageBody = _jsonConverter.Serialize(responseWrapper);
                        IBasicProperties properties = _responderChannel.CreateBasicProperties();
                        properties.MessageId        = Guid.NewGuid().ToString();
                        properties.Persistent       = false;
                        properties.CorrelationId    = args.BasicProperties.CorrelationId;
                        _responderChannel.BasicPublish("", args.BasicProperties.ReplyTo, properties, Encoding.UTF8.GetBytes(messageBody));

                        _logger.Send(new MessageDetail
                        {
                            Id            = properties.MessageId,
                            CorrelationId = properties.CorrelationId,
                            Type          = MessageType.Response,
                            Directory     = directory,
                            Subdirectory  = subdirectory,
                            Name          = typeof(TRequest).Name,
                            Body          = messageBody,
                            AppId         = args.BasicProperties.AppId,
                            Exception     = null,
                            ToRetry       = false
                        });
                    }

                    //acknowledgment
                    _responderChannel.BasicAck(args.DeliveryTag, false);
                },
                                      _cancellationTokenSource.Token,
                                      TaskCreationOptions.DenyChildAttach,
                                      TaskScheduler.Default);
            };

            _toActivateConsumers.Add(new Tuple <IModel, string, EventingBasicConsumer>(_responderChannel, queue, consumer));

            return(retryHandler);
        }
Esempio n. 5
0
 public async Task RequestAsync(object message, ITraceScope traceScope)
 {
     await RequestAsync <object>(message, traceScope);
 }
Esempio n. 6
0
 /// <summary>
 /// Called when a child scope is disposed.
 /// </summary>
 /// <param name="scope">The disposed scope.</param>
 protected virtual void OnScopeDisposed(ITraceScope scope)
 {
 }
Esempio n. 7
0
 /// <summary>
 /// Called when a child scope is created.
 /// </summary>
 /// <param name="scope">The created scope.</param>
 protected virtual void OnScopeCreated(ITraceScope scope)
 {
 }