/// <summary>
        /// Load the consumer configuration from the specified Autofac LifetimeScope
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="scope">The LifetimeScope of the container</param>
        /// <param name="name">The name to use for the scope created for each message</param>
        public static void LoadFrom(this IReceiveEndpointConfigurator configurator, ILifetimeScope scope, string name = "message")
        {
            var lifetimeScopeProvider = new SingleLifetimeScopeProvider(scope);

            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(lifetimeScopeProvider, name);

            IList <Type> concreteTypes = FindTypes(scope, r => !r.HasInterface <ISaga>(), typeof(IConsumer));

            if (concreteTypes.Count > 0)
            {
                foreach (var concreteType in concreteTypes)
                {
                    ConsumerConfiguratorCache.Configure(concreteType, configurator, scopeProvider);
                }
            }

            var sagaRepositoryFactory = new AutofacSagaRepositoryFactory(lifetimeScopeProvider, name);

            IList <Type> sagaTypes = FindTypes(scope, x => true, typeof(ISaga));

            if (sagaTypes.Count > 0)
            {
                foreach (var sagaType in sagaTypes)
                {
                    SagaConfiguratorCache.Configure(sagaType, configurator, sagaRepositoryFactory);
                }
            }
        }
        /// <summary>
        /// Creates a lifetime scope which is shared by any downstream filters (rather than creating a new one).
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="lifetimeScope"></param>
        /// <param name="name">The name of the lifetime scope</param>
        public static void UseLifetimeScope(this IPipeConfigurator <ConsumeContext> configurator, ILifetimeScope lifetimeScope, string name = "message")
        {
            var scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(lifetimeScope), name);
            var specification = new FilterPipeSpecification <ConsumeContext>(new ScopeFilter(scopeProvider));

            configurator.AddPipeSpecification(specification);
        }
        /// <summary>
        /// Registers a consumer given the lifetime scope specified
        /// </summary>
        /// <typeparam name="T">The consumer type</typeparam>
        /// <param name="configurator">The service bus configurator</param>
        /// <param name="scope">The LifetimeScope of the container</param>
        /// <param name="configure"></param>
        /// <param name="name">The name of the scope created per-message</param>
        /// <returns></returns>
        public static void Consumer <T>(this IReceiveEndpointConfigurator configurator, ILifetimeScope scope, Action <IConsumerConfigurator <T> > configure, string name = "message")
            where T : class, IConsumer
        {
            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(scope), name);

            var consumerFactory = new ScopeConsumerFactory <T>(scopeProvider);

            configurator.Consumer(consumerFactory, configure);
        }
        /// <summary>
        /// Registers a consumer given the lifetime scope specified
        /// </summary>
        /// <typeparam name="T">The consumer type</typeparam>
        /// <param name="configurator">The service bus configurator</param>
        /// <param name="scope">The LifetimeScope of the container</param>
        /// <param name="name">The name of the scope created per-message</param>
        /// <param name="configureScope">Configuration for scope container</param>
        /// <returns></returns>
        public static void Consumer <T>(this IReceiveEndpointConfigurator configurator, ILifetimeScope scope, string name = "message",
                                        Action <ContainerBuilder, ConsumeContext> configureScope = null)
            where T : class, IConsumer
        {
            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(scope), name, configureScope);

            var consumerFactory = new ScopeConsumerFactory <T>(scopeProvider);

            configurator.Consumer(consumerFactory);
        }
        /// <summary>
        /// Registers a consumer given the lifetime scope specified
        /// </summary>
        /// <typeparam name="T">The consumer type</typeparam>
        /// <param name="configurator">The service bus configurator</param>
        /// <param name="context">The LifetimeScope of the container</param>
        /// <param name="name">The name of the scope created per-message</param>
        /// <returns></returns>
        public static void Consumer <T>(this IReceiveEndpointConfigurator configurator, IComponentContext context, string name = "message")
            where T : class, IConsumer
        {
            var lifetimeScope = context.Resolve <ILifetimeScope>();

            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(lifetimeScope), name);

            var consumerFactory = new ScopeConsumerFactory <T>(scopeProvider);

            configurator.Consumer(consumerFactory);
        }
        /// <summary>
        /// Registers a consumer given the lifetime scope specified
        /// </summary>
        /// <typeparam name="T">The consumer type</typeparam>
        /// <typeparam name="TId"></typeparam>
        /// <param name="configurator">The service bus configurator</param>
        /// <param name="context">The component context containing the registry</param>
        /// <param name="name">The name of the scope created per-message</param>
        /// <returns></returns>
        public static void ConsumerInScope <T, TId>(this IReceiveEndpointConfigurator configurator, IComponentContext context, string name = "message")
            where T : class, IConsumer
        {
            ILifetimeScopeRegistry <TId> registry = context.Resolve <ILifetimeScopeRegistry <TId> >();

            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(new RegistryLifetimeScopeProvider <TId>(registry), name);

            var consumerFactory = new ScopeConsumerFactory <T>(scopeProvider);

            configurator.Consumer(consumerFactory);
        }
        /// <summary>
        /// Connect a consumer with a consumer factory method
        /// </summary>
        /// <typeparam name="TConsumer"></typeparam>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="configurator"></param>
        /// <param name="scope"></param>
        /// <param name="configure"></param>
        /// <param name="name">The name of the scope created per-message</param>
        /// <param name="configureScope">Configuration for scope container</param>
        /// <returns></returns>
        public static void Consumer <TConsumer, TMessage>(this IBatchConfigurator <TMessage> configurator, ILifetimeScope scope,
                                                          Action <IConsumerMessageConfigurator <TConsumer, Batch <TMessage> > > configure = null, string name = "message",
                                                          Action <ContainerBuilder, ConsumeContext> configureScope = null)
            where TConsumer : class, IConsumer <Batch <TMessage> >
            where TMessage : class
        {
            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(scope), name, configureScope);

            IConsumerFactory <TConsumer> consumerFactory = new ScopeConsumerFactory <TConsumer>(scopeProvider);

            configurator.Consumer(consumerFactory, configure);
        }
 private static void LoadConsumersFrom <TBusType, TRoutingType>(this IReceiveEndpointConfigurator configurator, ILifetimeScope rootScope)
     where TBusType : class, IConsumerBusType
     where TRoutingType : class, IConsumerRountingType
 {
     try
     {
         foreach (var consumerType in rootScope.FindConsumerTypes <TBusType, TRoutingType>())
         {
             var scopeProvider = new AutofacConsumerScopeProvider(new SingleLifetimeScopeProvider(rootScope), AutofacExtensions.ServiceBusMessageScopeTag);
             ConsumerConfiguratorCache.Configure(consumerType, configurator, scopeProvider);
         }
     }
     catch (Exception e)
     {
         Log.Error(e, $"Failed to register receive endpoint: {configurator.InputAddress}");
     }
 }
Beispiel #9
0
        public static void LoadFrom(this IReceiveEndpointConfigurator configurator, ILifetimeScope scope, string name = "message",
                                    Action <ContainerBuilder, ConsumeContext> configureScope = null)
        {
            var registration = scope.ResolveOptional <IRegistration>();

            if (registration != null)
            {
                registration.ConfigureConsumers(configurator);
                registration.ConfigureSagas(configurator);

                return;
            }

            var lifetimeScopeProvider = new SingleLifetimeScopeProvider(scope);

            IConsumerScopeProvider scopeProvider = new AutofacConsumerScopeProvider(lifetimeScopeProvider, name, configureScope);

            IList <Type> concreteTypes = FindTypes(scope, r => !r.HasInterface <ISaga>(), typeof(IConsumer));

            if (concreteTypes.Count > 0)
            {
                foreach (var concreteType in concreteTypes)
                {
                    ConsumerConfiguratorCache.Configure(concreteType, configurator, scopeProvider);
                }
            }

            var sagaRepositoryFactory = new AutofacSagaRepositoryFactory(lifetimeScopeProvider, name, configureScope);

            IList <Type> sagaTypes = FindTypes(scope, x => true, typeof(ISaga));

            if (sagaTypes.Count > 0)
            {
                foreach (var sagaType in sagaTypes)
                {
                    SagaConfiguratorCache.Configure(sagaType, configurator, sagaRepositoryFactory);
                }
            }
        }