Пример #1
0
        public virtual Task <TResponse> Request <TRequest, TResponse>(TRequest request)
            where TRequest : class
            where TResponse : class
        {
            Preconditions.CheckNotNull(request, "request");

            var correlationId = Guid.NewGuid();

            var tcs   = new TaskCompletionSource <TResponse>();
            var timer = new Timer(state =>
            {
                ((Timer)state).Dispose();
                tcs.TrySetException(new TimeoutException(
                                        string.Format("Request timed out. CorrelationId: {0}", correlationId.ToString())));
            });


            var requestType = typeof(TRequest);

            timer.Change(TimeSpan.FromSeconds(timeoutStrategy.GetTimeoutSeconds(requestType)), disablePeriodicSignaling);
            RegisterErrorHandling(correlationId, timer, tcs);

            var queueName  = SubscribeToResponse <TRequest, TResponse>();
            var routingKey = conventions.RpcRoutingKeyNamingConvention(requestType);

            RequestPublish(request, routingKey, queueName, correlationId);

            return(tcs.Task);
        }
Пример #2
0
        public virtual Task <TResponse> Request <TRequest, TResponse>(TRequest request, Action <IRequestConfiguration> configure)
            where TRequest : class
            where TResponse : class
        {
            Preconditions.CheckNotNull(request, "request");

            var correlationId = Guid.NewGuid();
            var requestType   = typeof(TRequest);
            var configuration = new RequestConfiguration();

            configure(configuration);

            var tcs = new TaskCompletionSource <TResponse>();

            var   timeout = timeoutStrategy.GetTimeoutSeconds(requestType);
            Timer timer   = null;

            if (timeout > 0)
            {
                timer = new Timer(state =>
                {
                    tcs.TrySetException(new TimeoutException(string.Format("Request timed out. CorrelationId: {0}", correlationId.ToString())));
                    responseActions.TryRemove(correlationId.ToString(), out ResponseAction responseAction);
                }, null, TimeSpan.FromSeconds(timeout), disablePeriodicSignaling);
            }

            RegisterErrorHandling(correlationId, timer, tcs);

            var queueName  = SubscribeToResponse <TRequest, TResponse>();
            var routingKey = configuration.QueueName ?? conventions.RpcRoutingKeyNamingConvention(requestType);

            RequestPublish(request, routingKey, queueName, correlationId);

            return(tcs.Task);
        }