Esempio n. 1
0
        static void Main(string[] args)
        {
            var rawRabbitOptions = new RawRabbitOptions
            {
                ClientConfiguration = GetRawRabbitConfiguration()
            };
            var busClient = RawRabbitFactory.CreateSingleton();


            try
            {
                var qq = busClient.SubscribeAsync <CreateInvoiceCommand>(async(msg) =>
                {
                    await Handle(msg);
                },
                                                                         CFG => CFG.UseSubscribeConfiguration(F =>
                                                                                                              F.Consume(c => c
                                                                                                                        .WithRoutingKey("createinvoicecommand1"))
                                                                                                              .OnDeclaredExchange(E => E
                                                                                                                                  .WithName("amq.direct")
                                                                                                                                  .WithType(ExchangeType.Direct))
                                                                                                              .FromDeclaredQueue(q => q
                                                                                                                                 .WithName("createinvoicecommand1"))
                                                                                                              ));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            /* var builder = new ConfigurationBuilder().AddConfiguration(Configuration);
             * services.AddRawRabbit(cfg =>cfg.AddJsonFile("rawrabbit.json")); */


            /* services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
             * var options = new RawRabbitOptions();
             * var section = Configuration.GetSection("rabbitmq");
             * section.Bind(options);
             *
             * services.AddRawRabbit(options);
             * var service = new ServiceCollection()
             * .AddRawRabbit<CustomContext>()
             * .BuildServiceProvider();
             * var client = service.GetService<IBusClient<CustomContext>>(); */


            var options = new RawRabbitOptions();
            var section = Configuration.GetSection("rabbitmq");

            section.Bind(options);
            var client = RawRabbitFactory.CreateSingleton(options);

            services.AddSingleton <IBusClient>(_ => client);
            services.AddSingleton <IRepository, InMemoryRepository>();
            services.AddTransient <IEventHandler <ValueCalculatedEvent>, ValueCalculatedHandler>();
        }
 public static IKernel RegisterRawRabbit(this IKernel config, RawRabbitOptions options = null)
 {
     if (options != null)
     {
         config.Bind <RawRabbitOptions>().ToConstant(options);
     }
     config.Load <RawRabbitModule>();
     return(config);
 }
        public static void ConfigureRawRabbit(IServiceCollection services, RawRabbitConfiguration configuration)
        {
            var options = new RawRabbitOptions {
                ClientConfiguration = configuration,
                Plugins             = p => p.UseAttributeRouting()
            };

            services.AddRawRabbit(options);
        }
        public static void AddRabbitMq(this IServiceCollection services, IConfigurationSection configuration)
        {
            var config = configuration.Get <RawRabbitConfiguration>();
            RawRabbitOptions options = new RawRabbitOptions()
            {
                ClientConfiguration = config
            };
            var client = RawRabbitFactory.CreateSingleton(options);

            services.AddSingleton <IBusClient>(_ => client);
        }
        public static void AddRabbitMqHealthCheck(this IServiceCollection services, IConfigurationSection configuration)
        {
            var config = configuration.Get <RawRabbitConfiguration>();
            RawRabbitOptions options = new RawRabbitOptions()
            {
                ClientConfiguration = config
            };

            services.AddHealthChecks().AddCheck("RabbitMq check", new RabbitMqHealthCheck(config), tags: new List <string> {
                "order queue", "payment queue"
            });
        }
Esempio n. 7
0
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                       .ConfigureHostConfiguration(configurationBuilder =>
            {
                configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                configurationBuilder.AddEnvironmentVariables();
            })
                       .ConfigureLogging((hostContext, loggerBuilder) =>
            {
                loggerBuilder.AddConsole();
            })
                       .ConfigureServices((hostContext, services) =>
            {
                services.AddFighting(fightBuilder =>
                {
                    fightBuilder.ConfigureCacheing(cacheBuilder =>
                    {
                        cacheBuilder.UseRedisCache(options =>
                        {
                            options.ConnectionString = hostContext.Configuration.GetConnectionString("Fighting.Redis");
                        });
                    });

                    fightBuilder.ConfigureMessageServices(messageServiceBuilder =>
                    {
                        messageServiceBuilder.UseLotteryDispatchingMessagePublisher();
                        messageServiceBuilder.UseLotteryNoticingMessagePublisher();
                    });


                    fightBuilder.ConfigureLotteryDispatcher(dispatchBuilder =>
                    {
                        var dispatcherOptions = hostContext.Configuration.GetSection("DispatcherConfiguration").Get <DispatcherConfiguration>();
                        dispatchBuilder.UseSuicaiExecuteDispatcher(dispatcherOptions);
                    });

                    RawRabbitOptions Options = new RawRabbitOptions {
                        ClientConfiguration = hostContext.Configuration.GetSection("RawRabbitConfiguration").Get <RawRabbitConfiguration>()
                    };

                    services.AddRawRabbit(Options);
                });
            });

            await host.RunConsoleAsync();
        }
        public static IBusClient CreateClient(RawRabbitOptions options = null)
        {
            options = options ?? new RawRabbitOptions();
            options.DependencyInjection  = options.DependencyInjection ?? (register => { });
            options.DependencyInjection += register => register.AddSingleton <IConfigurationEvaluator, ConfigurationEvaluator>();
            options.ClientConfiguration  = options?.ClientConfiguration ?? RawRabbitConfiguration.Local;
            options.Plugins              = options.Plugins ?? (builder => { });
            options.Plugins             += builder => builder
                                           .UseMessageContext(context => new MessageContext {
                GlobalRequestId = Guid.NewGuid()
            })
                                           .UseContextForwarding();
            var simpleIoc = new SimpleDependencyInjection();
            var client    = Instantiation.RawRabbitFactory.CreateSingleton(options, simpleIoc, ioc => simpleIoc);

            return(new BusClient(client, simpleIoc.GetService <IConfigurationEvaluator>()));
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            var rawRabbitOptions = new RawRabbitOptions
            {
                ClientConfiguration = GetRawRabbitConfiguration()
            };
            var busClient = RawRabbitFactory.CreateSingleton();

            var qq = busClient.SubscribeAsync <CreateInvoiceCommand>(async(msg) =>
            {
                await Handle(msg);
            },
                                                                     CFG => CFG.UseSubscribeConfiguration(F => F.OnDeclaredExchange(E => E.WithName(""))
                                                                                                          .FromDeclaredQueue(q => q.WithName("createinvoicecommand")))
                                                                     );

            Console.ReadKey();
        }
Esempio n. 10
0
        private static void ConfigureBus(IServiceCollection services, IConfiguration configuration)
        {
            var rawRabbitOptions       = configuration.GetOptions <RabbitMqOptions>(SectionName);
            var rawRabbitConfiguration = configuration.GetOptions <RawRabbitConfiguration>(SectionName);
            var namingConventions      = new CustomNamingConventions(rawRabbitOptions.Namespace);
            var options = new RawRabbitOptions
            {
                DependencyInjection = ioc =>
                {
                    ioc.AddSingleton(rawRabbitOptions);
                    ioc.AddSingleton(rawRabbitConfiguration);
                    ioc.AddSingleton <INamingConventions>(namingConventions);
                },
                Plugins = p => p.UseAttributeRouting().UseMessageContext <CorrelationContext>().UseContextForwarding()
            };

            services.AddRawRabbit(options);
        }
Esempio n. 11
0
        public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var options           = new RabbitMqOptions();
            var section           = configuration.GetSection("rabbitmq");
            var namingConventions = new CustomNamingConventions(options.Namespace);

            section.Bind(options);
            var rawRabbitOptions = new RawRabbitOptions
            {
                ClientConfiguration = options,
                DependencyInjection = ioc =>
                {
                    ioc.AddSingleton <INamingConventions>(namingConventions);
                }
            };
            var client = RawRabbitFactory.CreateSingleton(rawRabbitOptions);

            services.AddSingleton <IBusClient>(_ => client);
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            var rawRabbitOptions = new RawRabbitOptions
            {
                ClientConfiguration = GetRawRabbitConfiguration()
            };
            var busClient = RawRabbitFactory.CreateSingleton();
            var random    = new Random();

            while (true)
            {
                var message = random.Next(1, 1000);
                var cmd     = new CreateInvoiceCommand()
                {
                    Name = message.ToString()
                };
                busClient.PublishAsync(cmd, CFG => CFG.UsePublishConfiguration(C => C.OnExchange("amq.direct")
                                                                               .WithRoutingKey("createinvoicecommand1")
                                                                               ));
                Console.WriteLine($"{DateTime.Now} Sended: {cmd.Name}");
                Thread.Sleep(2000);
            }
        }
Esempio n. 13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSingleton <ICalculate, Calculate>();
            services.AddTransient <ICommandHandler <CalculateValueCommand>, CalculateValueHandler>();

            /* var options = new RawRabbitOptions();
             * var section = Configuration.GetSection("rabbitmq");
             * section.Bind(options);
             *
             * services.AddRawRabbit(options);  */



            var options = new RawRabbitOptions();
            var section = Configuration.GetSection("rabbitmq");

            section.Bind(options);


            var client = RawRabbitFactory.CreateSingleton(options);

            services.AddSingleton <IBusClient>(_ => client);
        }
Esempio n. 14
0
        public static ContainerBuilder RegisterRawRabbit(this ContainerBuilder builder, RawRabbitOptions options = null)
        {
            builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(type => type.Namespace.StartsWith(RawRabbit)));
            var adapter = new ContainerBuilderAdapter(builder);

            adapter.AddRawRabbit(options);
            return(builder);
        }
        public static IDependencyRegister AddRawRabbit(this IDependencyRegister register, RawRabbitOptions options = null)
        {
            register
            .AddSingleton(options?.ClientConfiguration ?? RawRabbitConfiguration.Local)
            .AddSingleton <IConnectionFactory, ConnectionFactory>(provider =>
            {
                var cfg = provider.GetService <RawRabbitConfiguration>();
                return(new ConnectionFactory
                {
                    VirtualHost = cfg.VirtualHost,
                    UserName = cfg.Username,
                    Password = cfg.Password,
                    Port = cfg.Port,
                    HostName = cfg.Hostnames.FirstOrDefault() ?? string.Empty,
                    AutomaticRecoveryEnabled = cfg.AutomaticRecovery,
                    TopologyRecoveryEnabled = cfg.TopologyRecovery,
                    NetworkRecoveryInterval = cfg.RecoveryInterval,
                    ClientProperties = provider.GetService <IClientPropertyProvider>().GetClientProperties(cfg),
                    Ssl = cfg.Ssl
                });
            })
            .AddSingleton <IChannelPoolFactory, AutoScalingChannelPoolFactory>()
            .AddSingleton(resolver => AutoScalingOptions.Default)
            .AddSingleton <IClientPropertyProvider, ClientPropertyProvider>()
            .AddSingleton <ISerializer>(resolver => new Serialization.JsonSerializer(new Newtonsoft.Json.JsonSerializer
            {
                TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
                Formatting             = Formatting.None,
                CheckAdditionalContent = true,
                ContractResolver       = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                },
                ObjectCreationHandling     = ObjectCreationHandling.Auto,
                DefaultValueHandling       = DefaultValueHandling.Ignore,
                TypeNameHandling           = TypeNameHandling.Auto,
                ReferenceLoopHandling      = ReferenceLoopHandling.Serialize,
                MissingMemberHandling      = MissingMemberHandling.Ignore,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                NullValueHandling          = NullValueHandling.Ignore
            }))
            .AddSingleton <IConsumerFactory, ConsumerFactory>()
            .AddSingleton <IChannelFactory>(resolver =>
            {
                var channelFactory = new ChannelFactory(resolver.GetService <IConnectionFactory>(), resolver.GetService <RawRabbitConfiguration>());
                channelFactory
                .ConnectAsync()
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();
                return(channelFactory);
            })
            .AddSingleton <ISubscriptionRepository, SubscriptionRepository>()
            .AddSingleton <ITopologyProvider, TopologyProvider>()
            .AddTransient <IPublisherConfigurationFactory, PublisherConfigurationFactory>()
            .AddTransient <IBasicPublishConfigurationFactory, BasicPublishConfigurationFactory>()
            .AddTransient <IConsumerConfigurationFactory, ConsumerConfigurationFactory>()
            .AddTransient <IConsumeConfigurationFactory, ConsumeConfigurationFactory>()
            .AddTransient <IExchangeDeclarationFactory, ExchangeDeclarationFactory>()
            .AddTransient <IQueueConfigurationFactory, QueueDeclarationFactory>()
            .AddSingleton <INamingConventions, NamingConventions>()
            .AddSingleton <IExclusiveLock, ExclusiveLock>()
            .AddSingleton <IBusClient, BusClient>()
            .AddSingleton <IResourceDisposer, ResourceDisposer>()
            .AddTransient <IInstanceFactory>(resolver => new InstanceFactory(resolver))
            .AddSingleton <IPipeContextFactory, PipeContextFactory>()
            .AddTransient <IExtendedPipeBuilder, PipeBuilder>(resolver => new PipeBuilder(resolver))
            .AddSingleton <IPipeBuilderFactory>(provider => new PipeBuilderFactory(provider));

            var clientBuilder = new ClientBuilder();

            options?.Plugins?.Invoke(clientBuilder);
            clientBuilder.DependencyInjection?.Invoke(register);
            register.AddSingleton(clientBuilder.PipeBuilderAction);

            options?.DependencyInjection?.Invoke(register);
            return(register);
        }
Esempio n. 16
0
        public static IServiceCollection AddRawRabbit(this IServiceCollection collection, RawRabbitOptions options = null)
        {
            var adapter = new ServiceCollectionAdapter(collection);

            adapter.AddRawRabbit(options);
            options?.DependencyInjection?.Invoke(adapter);
            return(collection);
        }