Beispiel #1
0
        public ISubscription SubscribeAsync <T>(Func <T, TMessageContext, Task> subscribeMethod, Action <ISubscriptionConfigurationBuilder> configuration = null)
        {
            var config      = _configEval.GetConfiguration <T>(configuration);
            var exchangeCfg = config.Exchange.AssumeInitialized
                                ? null
                                : new ExchangeDeclaration
            {
                AutoDelete   = config.Exchange.AutoDelete,
                Durable      = config.Exchange.Durable,
                ExchangeType = config.Exchange.ExchangeType,
                Name         = config.Exchange.ExchangeName,
                Arguments    = config.Exchange.Arguments
            };
            var queueCfg = config.Queue.AssumeInitialized
                                ? null
                                : new QueueDeclaration
            {
                Arguments  = config.Queue.Arguments,
                Durable    = config.Queue.Durable,
                AutoDelete = config.Queue.AutoDelete,
                Exclusive  = config.Queue.Exclusive,
                Name       = config.Queue.FullQueueName
            };

            var consumerCfg = new ConsumerConfiguration
            {
                Exchange = exchangeCfg,
                Queue    = queueCfg,
                Consume  = new ConsumeConfiguration
                {
                    ConsumerTag   = Guid.NewGuid().ToString(),
                    ExchangeName  = config.Exchange.ExchangeName,
                    QueueName     = config.Queue.FullQueueName,
                    AutoAck       = config.AutoAck,
                    RoutingKey    = config.RoutingKey,
                    PrefetchCount = config.PrefetchCount
                }
            };
            Action <IPipeContext> ctxAction = context =>
            {
                context.Properties.TryAdd(PipeKey.ConsumerConfiguration, consumerCfg);
                context.Properties.TryAdd(PipeKey.ConsumeConfiguration, consumerCfg.Consume);
                context.Properties.TryAdd(PipeKey.QueueDeclaration, consumerCfg.Queue);
                context.Properties.TryAdd(PipeKey.ExchangeDeclaration, consumerCfg.Exchange);
            };

            return(_client
                   .SubscribeAsync(subscribeMethod, ctxAction)
                   .ContinueWith(tContext => tContext.Result.GetSubscription())
                   .GetAwaiter()
                   .GetResult());
        }
Beispiel #2
0
 public ISubscription SubscribeAsync <T>(Func <T, TMessageContext, Task> subscribeMethod, Action <ISubscriptionConfigurationBuilder> configuration = null)
 {
     return(_client.SubscribeAsync(subscribeMethod, configuration));
 }