Task <TProperty> IPropertyConverter <TProperty, TInput> .Convert <TMessage>(InitializeContext <TMessage> context, TInput input)
        {
            if (input == null)
            {
                return(TaskUtil.Default <TProperty>());
            }

            InitializeContext <TProperty> messageContext = MessageFactoryCache <TProperty> .Factory.Create(context);

            IMessageInitializer <TProperty> initializer = typeof(TInput) == typeof(object)
                ? MessageInitializerCache <TProperty> .GetInitializer(input.GetType())
                : MessageInitializerCache <TProperty> .GetInitializer(typeof(TInput));

            Task <InitializeContext <TProperty> > initTask = initializer.Initialize(messageContext, input);

            if (initTask.IsCompleted)
            {
                return(Task.FromResult(initTask.Result.Message));
            }

            async Task <TProperty> ConvertAsync()
            {
                InitializeContext <TProperty> result = await initTask.ConfigureAwait(false);

                return(result.Message);
            }

            return(ConvertAsync());
        }
Esempio n. 2
0
        /// <summary>
        /// Send a message
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="provider"></param>
        /// <param name="values"></param>
        /// <param name="pipe"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Send is acknowledged by the broker</returns>
        public static async Task Send <T>(this ISendEndpointProvider provider, object values, IPipe <SendContext> pipe,
                                          CancellationToken cancellationToken = default)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (!EndpointConvention.TryGetDestinationAddress <T>(out var destinationAddress))
            {
                throw new ArgumentException($"A convention for the message type {TypeMetadataCache<T>.ShortName} was not found");
            }

            var endpoint = await provider.GetSendEndpoint(destinationAddress).ConfigureAwait(false);

            var initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            if (provider is ConsumeContext context)
            {
                await initializer.Send(endpoint, initializer.Create(context), values, pipe).ConfigureAwait(false);
            }
            else
            {
                await initializer.Send(endpoint, values, pipe, cancellationToken).ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        public Task <InitializeContext <TMessage> > CreateMessage(object values, CancellationToken cancellationToken)
        {
            var initializer = MessageInitializerCache <TMessage> .GetInitializer(values.GetType());

            return(_consumeContext != null
                ? initializer.Initialize(initializer.Create(_consumeContext), values)
                : initializer.Initialize(values, cancellationToken));
        }
        public async Task <T> CreateMessage(object values, CancellationToken cancellationToken)
        {
            var initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            if (_sendEndpointProvider is ConsumeContext context)
            {
                return((await initializer.Initialize(initializer.Create(context), values).ConfigureAwait(false)).Message);
            }

            return((await initializer.Initialize(values, cancellationToken).ConfigureAwait(false)).Message);
        }
        public Task Send <T>(object values, CancellationToken cancellationToken)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            return(initializer.Send(this, values, cancellationToken));
        }
        public Task <TRequest> Send(Guid requestId, object values, IPipe <SendContext <TRequest> > pipe, CancellationToken cancellationToken)
        {
            IMessageInitializer <TRequest> initializer = MessageInitializerCache <TRequest> .GetInitializer(values.GetType());

            if (_consumeContext != null)
            {
                InitializeContext <TRequest> initializeContext = initializer.Create(_consumeContext);

                return(initializer.Send(_endpoint, initializeContext, values, new ConsumeSendPipeAdapter <TRequest>(_consumeContext, pipe, requestId)));
            }

            return(initializer.Send(_endpoint, values, pipe, cancellationToken));
        }
        public async Task <TRequest> Send(Guid requestId, object values, IPipe <SendContext <TRequest> > pipe, CancellationToken cancellationToken)
        {
            var endpoint = await GetPublishSendEndpoint().ConfigureAwait(false);

            IMessageInitializer <TRequest> initializer = MessageInitializerCache <TRequest> .GetInitializer(values.GetType());

            if (_consumeContext != null)
            {
                InitializeContext <TRequest> initializeContext = initializer.Create(_consumeContext);

                return(await initializer.Send(endpoint, initializeContext, values, new ConsumeSendPipeAdapter <TRequest>(_consumeContext, pipe, requestId))
                       .ConfigureAwait(false));
            }

            return(await initializer.Send(endpoint, values, pipe, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 8
0
        Task IPublishEndpoint.Publish <T>(object values, IPipe <PublishContext> publishPipe, CancellationToken cancellationToken)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            if (_consumeContext != null)
            {
                return(initializer.Publish(this, initializer.Create(_consumeContext), values, publishPipe));
            }

            return(initializer.Publish(this, values, publishPipe, cancellationToken));
        }
        async Task <TProperty> IPropertyConverter <TProperty, TInput> .Convert <TMessage>(InitializeContext <TMessage> context, TInput input)
        {
            if (input == null)
            {
                return(null);
            }

            InitializeContext <TProperty> messageContext = MessageFactoryCache <TProperty> .Factory.Create(context);

            IMessageInitializer <TProperty> initializer = typeof(TInput) == typeof(object)
                ? MessageInitializerCache <TProperty> .GetInitializer(input.GetType())
                : MessageInitializerCache <TProperty> .GetInitializer(typeof(TInput));

            InitializeContext <TProperty> result = await initializer.Initialize(messageContext, input).ConfigureAwait(false);

            return(result.Message);
        }
Esempio n. 10
0
        public Task Send <T>(object values, IPipe <SendContext> pipe, CancellationToken cancellationToken)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (pipe == null)
            {
                throw new ArgumentNullException(nameof(pipe));
            }

            var initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            return(initializer.Send(this, initializer.Create(_context), values, pipe));
        }
Esempio n. 11
0
        public Task Send <T>(object values, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (pipe == null)
            {
                throw new ArgumentNullException(nameof(pipe));
            }

            IMessageInitializer <T> initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            InitializeContext <T> context = GetInitializeContext(initializer, cancellationToken);

            return(initializer.Send(this, context, values, pipe));
        }
Esempio n. 12
0
        public async Task SendResponse(FutureConsumeContext <TResponse> context, params FutureSubscription[] subscriptions)
        {
            context.SetCompleted(context.Instance.CorrelationId);

            InitializeContext <TResult> initializeContext = await MessageInitializerCache <TResult> .Initialize(new
            {
                context.Instance.Completed,
                context.Instance.Created,
                context.Instance.Faulted,
                context.Instance.Location,
            }, context.CancellationToken);

            var request = context.Instance.GetCommand <TCommand>();

            if (request != null)
            {
                initializeContext = await MessageInitializerCache <TResult> .Initialize(initializeContext, request);
            }

            if (context.Message != null)
            {
                initializeContext = await MessageInitializerCache <TResult> .Initialize(initializeContext, context.Message);
            }

            // this is due to the way headers are propagated via the initializer
            var values = _provider(context);

            IMessageInitializer <TResult> initializer = MessageInitializerCache <TResult> .GetInitializer(values.GetType());

            var result = await context.SendMessageToSubscriptions(subscriptions, initializer, initializeContext, values).ConfigureAwait(false);

            if (result == null)
            {
                // initialize the message and save it as the response
                InitializeContext <TResult> messageContext = await initializer.Initialize(initializeContext, values).ConfigureAwait(false);

                result = messageContext.Message;
            }

            context.SetResult(context.Instance.CorrelationId, result);
        }
        public async Task SendFault(FutureConsumeContext <TInput> context, params FutureSubscription[] subscriptions)
        {
            InitializeContext <TFault> initializeContext;

            if (context.Message is Fault fault)
            {
                var request = context.Instance.GetCommand <TCommand>();

                context.SetFaulted(context.Instance.CorrelationId, fault.Timestamp);

                initializeContext = await MessageInitializerCache <TFault> .Initialize(new
                {
                    fault.FaultId,
                    fault.FaultedMessageId,
                    fault.Timestamp,
                    fault.Exceptions,
                    fault.Host,
                    fault.FaultMessageTypes,
                    Message = request
                }, context.CancellationToken);

                initializeContext = await MessageInitializerCache <TFault> .Initialize(initializeContext, context.Message);
            }
            else
            {
                context.SetFaulted(context.Instance.CorrelationId);

                initializeContext = await MessageInitializerCache <TFault> .Initialize(context.Message, context.CancellationToken);
            }

            var values = _provider(context);

            IMessageInitializer <TFault> initializer = MessageInitializerCache <TFault> .GetInitializer(values.GetType());

            InitializeContext <TFault> messageContext = await initializer.Initialize(initializeContext, values).ConfigureAwait(false);

            context.SetFault(context.Instance.CorrelationId, messageContext.Message);

            await context.SendMessageToSubscriptions(subscriptions, initializer, initializeContext, values).ConfigureAwait(false);
        }
Esempio n. 14
0
        async Task RespondAsyncInternal <T>(object values, IPipe <SendContext <T> > responsePipe)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var context = await MessageInitializerCache <T> .Initialize(Message, _context.CancellationToken).ConfigureAwait(false);

            IMessageInitializer <T> initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            if (_context.ResponseAddress != null)
            {
                var endpoint = await _context.GetSendEndpoint(_context.ResponseAddress).ConfigureAwait(false);

                await ConsumeTask(initializer.Send(endpoint, context, values, responsePipe)).ConfigureAwait(false);
            }
            else
            {
                await ConsumeTask(initializer.Publish(_context, context, values, responsePipe)).ConfigureAwait(false);
            }
        }
Esempio n. 15
0
        async Task RespondAsyncInternal <T>(object values, IPipe <SendContext <T> > responsePipe = default)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var context = await MessageInitializerCache <T> .Initialize(Message, _context.CancellationToken).ConfigureAwait(false);

            IMessageInitializer <T> initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            var responseEndpoint = await GetResponseEndpoint <T>().ConfigureAwait(false);

            if (responsePipe.IsNotEmpty())
            {
                await ConsumeTask(initializer.Send(responseEndpoint, context, values, responsePipe)).ConfigureAwait(false);
            }
            else
            {
                await ConsumeTask(initializer.Send(responseEndpoint, context, values)).ConfigureAwait(false);
            }
        }
Esempio n. 16
0
        async Task RespondAsyncInternal <T>(object values, IPipe <SendContext <T> > responsePipe)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            IMessageInitializer <T> initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            var context = initializer.Create(this);

            if (ResponseAddress != null)
            {
                var endpoint = await GetSendEndpoint(ResponseAddress).ConfigureAwait(false);

                await ConsumeTask(initializer.Send(endpoint, context, values, responsePipe)).ConfigureAwait(false);
            }
            else
            {
                await ConsumeTask(initializer.Publish(_publishEndpoint.Value, context, values, responsePipe)).ConfigureAwait(false);
            }
        }
Esempio n. 17
0
 public InitializePropertyConverter()
 {
     _initializer = MessageInitializerCache <TProperty> .GetInitializer(typeof(TInput));
 }