Beispiel #1
0
        private static void ConfigureBus(ContainerBuilder builder)
        {
            builder.Register <IInstanceFactory>(context =>
            {
                var options           = context.Resolve <RabbitMqOptions>();
                var configuration     = context.Resolve <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                    },
                    Plugins = p => p
                              .UseAttributeRouting()
                              .UseRetryLater()
                              .UpdateRetryInfo()
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                }));
            }).SingleInstance();

            // In case of 'BrokerUnreachableException' - this indicates that a RabbitMQ instance
            // isn't running. Please run 'docker-compose -f ./compose/infrastructure.yml up' to resolve this.
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
Beispiel #2
0
        private static void ConfigureBus(ContainerBuilder builder)
        {
            builder.Register <IInstanceFactory>(context =>
            {
                var options           = context.Resolve <RabbitMqOptions>();
                var configuration     = context.Resolve <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                    },
                    Plugins = p => p
                              .UseAttributeRouting()
                              .UseRetryLater()
                              .UpdateRetryInfo()
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                }));
            }).SingleInstance();
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
Beispiel #3
0
        public static void Register(ContainerBuilder builder, RawRabbitConfiguration configuration, int retryAttempts = 5)
        {
            var policy = Policy
                         .Handle <ConnectFailureException>()
                         .Or <BrokerUnreachableException>()
                         .Or <IOException>()
                         .WaitAndRetry(retryAttempts, retryAttempt =>
                                       TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                       (exception, timeSpan, retryCount, context) =>
            {
                Logger.Error(exception, "Can not connect to RabbitMQ. " +
                             $"Retries: {retryCount}, duration: {timeSpan}");
            }
                                       );

            builder.RegisterInstance(configuration).SingleInstance();
            policy.Execute(() =>
            {
                builder.Register(context => RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc => ioc.AddSingleton(configuration)
                }))
                .As <IInstanceFactory>()
                .SingleInstance();
                builder.Register(context => context.Resolve <IInstanceFactory>().Create());
            });
        }
Beispiel #4
0
        public async Task InitAsync()
        {
            var extension         = _configuration.Extensions.Single(e => e.Value.Use == Name).Value;
            var configurationPath = extension.Configuration;

            if (!File.Exists(configurationPath))
            {
                configurationPath = $"{_configuration.SettingsPath}/{configurationPath}";
                if (!File.Exists(configurationPath))
                {
                    throw new Exception($"Configuration for an extension: '{Name}'," +
                                        $"was not found under: '{configurationPath}'.");
                }
            }

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile(configurationPath);

            var options = new RabbitMqOptions();

            builder.Build().Bind(options);

            _busClient = RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
            {
                DependencyInjection = ioc => { ioc.AddSingleton(options); },
                Plugins             = p => p
                                      .UseAttributeRouting()
                                      .UseRetryLater()
                                      .UseMessageContext <CorrelationContext>()
                                      .UseContextForwarding()
            }).Create();

            await Task.CompletedTask;
        }
Beispiel #5
0
 private static void ConfigureRabbitMq(ContainerBuilder builder)
 {
     builder.Register <IInstanceFactory>(context =>
                                         RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
     {
         DependencyInjection = ioc => ioc.AddSingleton(context.Resolve <RawRabbitConfiguration>())
     })).SingleInstance();
     builder.Register(context => context.Resolve <IInstanceFactory>().Create());
 }
Beispiel #6
0
        public static void AddRabbitMQServices(this ContainerBuilder builder)
        {
            builder.Register(context =>
            {
                var configuration = context.Resolve <IConfiguration>();
                var options       = configuration.GetOptions <RabbitMqOptions>("rabbitMq");

                return(options);
            }).SingleInstance();

            builder.Register(context =>
            {
                var configuration = context.Resolve <IConfiguration>();
                var options       = configuration.GetOptions <RawRabbitConfiguration>("rabbitMq");

                return(options);
            }).SingleInstance();

            var assembly = Assembly.GetCallingAssembly();

            builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(IEventHandler <>))
            .InstancePerDependency();
            builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(ICommandHandler <>))
            .InstancePerDependency();
            builder.RegisterType <BusPublisher>().As <IBusPublisher>()
            .InstancePerDependency();

            builder.Register <IInstanceFactory>(context =>
            {
                var options           = context.Resolve <RabbitMqOptions>();
                var configuration     = context.Resolve <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);
                // var tracer = context.Resolve<ITracer> ();

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                        // ioc.AddSingleton (tracer);
                    },
                    Plugins = p => p
                              .UseAttributeRouting()
                              .UseRetryLater()
                              .UpdateRetryInfo()
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                              // .UseJaeger (tracer)
                }));
            }).SingleInstance();
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
Beispiel #7
0
        public static void AddRabbitMq(this ContainerBuilder builder)
        {
            //Register the options from appconfig into the DI container
            builder.Register(context => {
                var config = context.Resolve <IConfiguration>();

                var rabbitMqOptions = new RabbitMqOptions();
                config.GetSection("rabbitMq").Bind(rabbitMqOptions);

                return(rabbitMqOptions);
            }).SingleInstance();

            builder.Register(context => {
                var config           = context.Resolve <IConfiguration>();
                var rawRabbitOptions = new RawRabbitConfiguration();
                config.GetSection("rabbitMq").Bind(rawRabbitOptions);

                return(rawRabbitOptions);
            }).SingleInstance();

            //Note:Obtain the service assembly
            var assembly = Assembly.GetCallingAssembly();

            builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(ICommandHandler <>)).InstancePerDependency();
            //builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(IEventHandler<>)).InstancePerDependency();
            // builder.RegisterType<MessageHandler>().As<IMessageHandler>().InstancePerDependency();
            builder.RegisterType <BusPublisher>().As <IBusPublisher>().InstancePerDependency();;
            // builder.RegisterInstance(ParkAndRideTracer).As<ITracer>().SingleInstance().PreserveExistingDefaults();


            builder.Register <IIntanceFactory>(context =>
            {
                var options       = context.Resolve <RabbitMqOptions>();
                var configuration = context.Resolve <RawRabbitConfiguration>();
                //  var tracer = context.Resolve<ITracer>();
                return(RawRabbitFactory <InstanceFactory> .CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        // ioc.AddSingleton(tracer);
                    },
                    Plugins = p => p
                              .UseAttributeRouting()
                              .UseRetryLater()
                              .UpdateRetryInfo()
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                              //.UseJaeger(tracer)
                }));
            }).SingleInstance();
            //Create the instance factory and instantiate it right away???
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
        private static void ConfigureBus(ContainerBuilder builder)
        {
            builder.Register <IInstanceFactory>(context =>
                                                RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
            {
                DependencyInjection = ioc => ioc.AddSingleton(context.Resolve <RawRabbitConfiguration>()),
                Plugins             = p => p
                                      .UseMessageContext <CorrelationContext>()
                                      .UseContextForwarding()
            })).SingleInstance();

            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
        public override void Load()
        {
            Kernel
            .Bind <IDependencyResolver>()
            .ToMethod(context => new NinjectAdapter(context));

            Kernel
            .Bind <IInstanceFactory>()
            .ToMethod(context => RawRabbitFactory.CreateInstanceFactory(context.Kernel.Get <RawRabbitOptions>()))
            .InSingletonScope();

            Kernel
            .Bind <IBusClient>()
            .ToMethod(context => context.Kernel.Get <IInstanceFactory>().Create());
        }
Beispiel #10
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register <IConfiguration>((a, s) =>
            {
                var configurationBuilder = new ConfigurationBuilder()
                                           .SetBasePath(Directory.GetCurrentDirectory())
                                           .AddJsonFile("appsettings.json");

                return(configurationBuilder.Build());
            }).SingleInstance();

            builder
            .Register <IInstanceFactory>(c => RawRabbitFactory.CreateInstanceFactory())
            .SingleInstance();
            builder
            .Register <IBusClient>(c => c.Resolve <IInstanceFactory>().Create());
        }
        public static void AddRabbitMq(this ContainerBuilder builder)
        {
            builder.Register(context =>
            {
                var configuration = context.Resolve <IConfiguration>();
                var options       = new RabbitMqOptions();
                configuration.GetSection("rabbitmq").Bind(options);
                return(options);
            }).SingleInstance();

            builder.Register(context =>
            {
                var configuration = context.Resolve <IConfiguration>();
                var options       = new RawRabbitConfiguration();
                configuration.GetSection("rabbitmq").Bind(options);
                return(options);
            }).SingleInstance();

            builder.Register <IInstanceFactory>(context =>
            {
                var options           = context.Resolve <RabbitMqOptions>();
                var configuration     = context.Resolve <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);

                var tracer = context.Resolve <ITracer>();

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                        ioc.AddSingleton(tracer);
                    },
                    ClientConfiguration = options,
                    Plugins = p => p
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                              .UseJaeger(tracer)
                }));
            }).SingleInstance();
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
        private static void ConfigureBus <TContext>(IConveyBuilder builder,
                                                    Func <IRabbitMqPluginRegister, IRabbitMqPluginRegister> plugins = null)
            where TContext : ICorrelationContext, new()
        {
            builder.Services.AddSingleton <IInstanceFactory>(serviceProvider =>
            {
                var register          = plugins?.Invoke(new RabbitMqPluginRegister(serviceProvider));
                var options           = serviceProvider.GetService <RabbitMqOptions>();
                var configuration     = serviceProvider.GetService <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        register?.Register(ioc);
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton(serviceProvider);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                        ioc.AddSingleton <ISerializer, Serializer <TContext> >(ctx =>
                                                                               new Serializer <TContext>(ctx.GetService <JsonSerializer>()));
                    },
                    Plugins = p =>
                    {
                        register?.Register(p);
                        p.UseAttributeRouting()
                        .UseRetryLater()
                        .UpdateRetryInfo()
                        .UseMessageContext <TContext>()
                        .UseContextForwarding();

                        if (options.MessageProcessor?.Enabled == true)
                        {
                            p.ProcessUniqueMessages();
                        }
                    }
                }));
            });

            builder.Services.AddTransient(serviceProvider => serviceProvider.GetService <IInstanceFactory>().Create());
        }
Beispiel #13
0
        private static void ConfigureBus(ContainerBuilder builder)
        {
            builder.Register <IInstanceFactory>(context =>
            {
                var options           = context.Resolve <RabbitMqOptions>();
                var configuration     = context.Resolve <RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                    }
                }));
            }).SingleInstance();
            builder.Register(context => context.Resolve <IInstanceFactory>().Create());
        }
Beispiel #14
0
        private static void ConfigureBus(IServiceCollection serviceCollection, Func <IRabbitMqPluginRegister,
                                                                                     IRabbitMqPluginRegister> plugins = null)
        {
            serviceCollection.AddSingleton <IInstanceFactory>(serviceProvider =>
            {
                var options           = serviceProvider.GetService <RabbitMqOptions>();
                var configuration     = serviceProvider.GetService <RawRabbitConfiguration>();
                var namingConventions = serviceProvider.GetService <INamingConventions>();
                var register          = plugins?.Invoke(new RabbitMqPluginRegister(serviceProvider));

                var factory = RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        register?.Register(ioc);
                        ioc.AddSingleton(options);
                        ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                    },
                    Plugins = clientBuilder =>
                    {
                        register?.Register(clientBuilder);
                        clientBuilder.UseAttributeRouting()
                        .UseRetryLater()
                        .UpdateRetryInfo()
                        //.UseMessageContext(ctx => ctx.GetDeliveryEventArgs()) //we will use context in scope of subscribe method
                        .UseContextForwarding();

                        if (options.MessageProcessor?.Enabled == true)
                        {
                            clientBuilder.ProcessUniqueMessages();
                        }
                    }
                });
                return(factory);
            });

            serviceCollection.AddTransient(context => context.GetService <IInstanceFactory>().Create());
        }
Beispiel #15
0
        public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            //services.AddTransient(typeof(ICommandHandler<>));
            services.AddTransient <IHandler, Handler>();
            services.AddTransient <IBusPublisher, BusPublisher>();

            services.AddSingleton <IInstanceFactory>(context =>
            {
                IConfiguration configuration;
                using (var serviceProvider = services.BuildServiceProvider())
                {
                    configuration = serviceProvider.GetService <IConfiguration>();
                }
                var options = configuration.GetSection("rabbitMq").Get <RabbitMqOptions>();
                //var rawRabbitConfiguration = context.GetService<RawRabbitConfiguration>();
                var namingConventions = new CustomNamingConventions(options.Namespace);
                var tracer            = context.GetRequiredService <ITracer>();

                return(RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions
                {
                    DependencyInjection = ioc =>
                    {
                        ioc.AddSingleton(options);
                        //ioc.AddSingleton(configuration);
                        ioc.AddSingleton <INamingConventions>(namingConventions);
                        ioc.AddSingleton(tracer);
                    },
                    Plugins = p => p
                              .UseAttributeRouting()
                              .UseMessageContext <CorrelationContext>()
                              .UseContextForwarding()
                              .UseJaeger(tracer)
                }));
            });

            services.AddTransient <IBusClient>(context => context.GetService <IInstanceFactory>().Create());
        }