Beispiel #1
0
        public static async Task RunAsync()
        {
            //Log.Logger = new LoggerConfiguration()
            //    .WriteTo.LiterateConsole()
            //    .CreateLogger();

            _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = new ConfigurationBuilder()
                                      .SetBasePath(Directory.GetCurrentDirectory())
                                      .AddJsonFile("rawrabbit.json")
                                      .Build()
                                      .Get <RawRabbitConfiguration>()
            });

            //Type t = Type.GetType("AskEvent");
            //var tt = typeof(AskEvent);
            //
            //object o = new AskEvent { Question = "Ask Question" };
            //
            //await _client.PublishAsync(o);
            await _client.SubscribeAsync <TestMessage>(ServerValuesAsync);

            //await _client.RespondAsync<ValueRequest, ValueResponse>(request => SendValuesThoughRpcAsync(request));
        }
Beispiel #2
0
        public static IServiceCollection AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new RawRabbitConfigurationCustom();
            configuration.GetSection("RabbitMq").Bind(options);

            services.AddSingleton(context =>
            {
                var configuration = context.GetService<IConfiguration>();
                var options = configuration.GetSection("RabbitMq").Get<RawRabbitConfigurationCustom>();

                return options;
            });

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options,
                Plugins = p => p
                    .UseGlobalExecutionId()
                    .UseHttpContext()
                    .UseMessageContext(c => new MessageContext { GlobalRequestId = Guid.NewGuid() })
            });
            services.AddSingleton<IBusClient>(_ => client);

            services.AddScoped<IBusPublisher, BusPublisher>();

            return services;
        }
Beispiel #3
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();
        }
Beispiel #4
0
        private static BusClient Init()
        {
            var userName    = Environment.GetEnvironmentVariable("BUS_USERNAME");
            var password    = Environment.GetEnvironmentVariable("BUS_PASSWORD");
            var port        = int.Parse(Environment.GetEnvironmentVariable("BUS_PORT"));
            var virtualHost = Environment.GetEnvironmentVariable("BUS_VIRTUAL_HOST");
            var hostNames   = Environment.GetEnvironmentVariable("BUS_HOSTNAME")?.Split(',').ToList();

            var busConfig = new RawRabbitConfiguration
            {
                Username    = userName,
                Password    = password,
                Port        = port,
                VirtualHost = virtualHost,
                Hostnames   = hostNames
            };

            return(RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = busConfig,
                Plugins = p => p
                          .UseStateMachine()
                          .UseGlobalExecutionId()
                          .UseHttpContext()
                          .UseCustomQueueSuffix()
                          .UseMessageContext(ctx => new MessageContext
                {
                    GlobalRequestId = Guid.Parse(ctx.GetGlobalExecutionId())
                })
            }));
        }
Beispiel #5
0
 public RabbitMqFixtureBase()
 {
     _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
     {
         ClientConfiguration = new RawRabbitConfiguration
         {
             Hostnames = new List <string> {
                 Hostnames
             },                                          // localhost
             VirtualHost = VirtualHost,
             Port        = 5672,
             Username    = UserName,
             Password    = Password,
         },
         DependencyInjection = ioc =>
         {
             ioc.AddSingleton <INamingConventions>(new RabbitMqNamingConventions(NameSpace));
         },
         Plugins = p => p
                   .UseAttributeRouting()
                   .UseRetryLater()
                   .UseMessageContext <CorrelationContext>()
                   .UseContextForwarding()
     });
 }
Beispiel #6
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>();
        }
Beispiel #7
0
        public static IServiceCollection AddRabbitMq(
            this IServiceCollection services, IConfiguration configuration)
        {
            var section = configuration.GetSection("RabbitMq");

            if (!section.Exists())
            {
                throw new InvalidOperationException("Missing 'RabbitMq' section in config!");
            }
            var options = new RawRabbitConfiguration();

            section.Bind(options);

            IBusClient client = null;

            Policy
            .Handle <BrokerUnreachableException>().Or <SocketException>()
            .WaitAndRetry(3, _ => TimeSpan.FromSeconds(10))
            .Execute(() =>
            {
                client = RawRabbitFactory.CreateSingleton(
                    new RawRabbitOptions {
                    ClientConfiguration = options
                });
            });
            services.AddSingleton <IBusClient>(_ => client);
            services.AddSingleton <IServiceBus, RabbitMqBus>();
            services.AddSingleton <IBusBuilder, RabbitMqBusBuilder>();
            return(services);
        }
Beispiel #8
0
        public static void Main(string[] args)
        {
            IBusClient client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = ConnectionStringParser.Parse("guest:guest@localhost:5672/")
            });

//            var client = RabbitHutch.CreateBus("host=localhost;username=guest;password=guest");

            Console.Out.WriteLine($"Created client. Press enter to start.");
            Console.ReadLine();
            var memory = new List <long>();

            for (var i = 0; i < 10000; i++)
            {
                if ((i % 1000) == 0)
                {
                    Console.Out.WriteLine($"Paused at iteration: {i}. Press enter to GC.Collect() and continue.");
                    Console.ReadLine();
                    memory.Add(GC.GetTotalMemory(false));
                    GC.Collect();
                }

                var result = client.RequestAsync <ValueRequest, ValueResponse>(new ValueRequest {
                    Value = i
                }).Result;
            }

            Console.Out.WriteLine(string.Join(",", memory));
            Console.Out.WriteLine("Finished.");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            //Thread.Sleep(20000);


            _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = new ConfigurationBuilder()
                                      .SetBasePath(Directory.GetCurrentDirectory())
                                      .AddJsonFile("rawrabbit.json")
                                      .Build()
                                      .Get <RawRabbitConfiguration>(),
                Plugins = p => p
                          .UseGlobalExecutionId()
                          .UseAttributeRouting()
                          .UseRetryLater()
                          .UseMessageContext <RetryMessageContext>()
            });



            //            while (true)
            //            {
            //                Console.WriteLine("Send a new message ?");
            //                Console.ReadKey();
            Thread.Sleep(4000);
            Console.WriteLine("We send a new message !");


            SendMessage().Wait();
            //}
        }
Beispiel #10
0
        public RabbitMqFixture(string defaultNamespace)
        {
            _defaultNamespace = defaultNamespace;

            _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = new RawRabbitConfiguration
                {
                    Hostnames = new List <string> {
                        "localhost"
                    },                                            // localhost
                    VirtualHost = "/",
                    Port        = 5672,
                    Username    = "******",
                    Password    = "******",
                },
                DependencyInjection = ioc =>
                {
                    ioc.AddSingleton <INamingConventions>(new RabbitMqFixtureNamingConventions("availability"));
                },
                Plugins = p => p
                          .UseAttributeRouting()
                          .UseRetryLater()
                          .UseMessageContext <CorrelationContext>()
                          .UseContextForwarding()
            });
        }
Beispiel #11
0
        public static Task RunAsync()
        {
            using (var client = RawRabbitFactory.CreateSingleton())
            {
                client.SubscribeAsync <BackFillStageCompleted>(async command =>
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(command.Asset);

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write($"  {command.Current} / {command.TotalNumber}");
                    Console.ResetColor();
                    Console.WriteLine();
                });

                client.SubscribeAsync <PeriodClosed>(async command =>
                {
                    Thread.Sleep(100);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(command.DateTime);

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("   " + Math.Round(command.Price, 4));
                    Console.ResetColor();

                    Console.Write("    " + Math.Round(command.Quantity, 4));
                    Console.WriteLine("   ");
                });

                Console.ReadLine();

                return(Task.FromResult(0));
            }
        }
        public RabbitMqFixture()
        {
            var builder = new ConfigurationBuilder();

            config = builder.AddJsonFile("appsettings.json").Build();
            option = config.GetOptions <RabbitMqOptions>("rabbitMq");

            _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = new RawRabbitConfiguration
                {
                    Hostnames   = option.Hostnames, // localhost
                    VirtualHost = "/",
                    Port        = option.Port,
                    Username    = option.Username,
                    Password    = option.Password,
                },
                DependencyInjection = ioc =>
                {
                    ioc.AddSingleton <INamingConventions>(new RabbitMqNamingConventions(option.Namespace));
                },
                Plugins = p => p
                          .UseAttributeRouting()
                          .UseRetryLater()
                          .UseMessageContext <CorrelationContext>()
                          .UseContextForwarding()
            });
        }
        private static void Main()
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("Options/RawRabbit.json", false)
                                .AddJsonFile("Options/ConnectionStrings.json", false)
                                .AddJsonFile("Options/OneSignal.json", false)
                                .Build();

            Policy
            .Handle <Exception>()
            .WaitAndRetryAsync(new[] {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(4)
            });

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = configuration.Get <RawRabbitConfiguration>(),

                Plugins = p => p
                          .UseAttributeRouting()
                          //   .UsePolly(c => c
                          //       .UsePolicy(policy, PolicyKeys.QueueBind)
                          //       .UsePolicy(policy, PolicyKeys.QueueDeclare)
                          //       .UsePolicy(policy, PolicyKeys.ExchangeDeclare)
                          //)
            });

            //IoCContainerFactory.BuildConfigurationBuilder();
            var services = IoCContainerFactory.BuildServiceProvider(configuration);

            client.SubscribeAsync <ProfileMessage>(async msg =>
            {
                await Task.Run(() =>
                {
                    //Console.WriteLine($"Received: {context.GlobalRequestId}");
                    Console.WriteLine($"Received: {msg.Id}");

                    var service = services.GetService <NewNotificationWhenNewDealHandler>();

                    service.Handle(msg);
                });
            });

            //var message = new ProfileMessage()
            //{
            //    Id = Guid.NewGuid(),
            //    BusinessId = new Guid("3a9d67ca-e720-4c76-94cb-0ee64f1cb564"),
            //    StartDate = DateTime.Now.AddDays(1)

            //};
            //client.PublishAsync(message);
        }
Beispiel #14
0
 public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
 {
     var options = new RabbitMqOptions();
     var section = configuration.GetSection("rabbitmq");
     section.Bind(options);
     var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
     {
         ClientConfiguration = options
     });
     services.AddSingleton<IBusClient>(_ => client);
 }
        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 AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var connectionString = configuration["rabbitmq_connectionstring"];
            var config           = ConnectionStringParser.Parse(connectionString);
            var client           = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = config
            });

            services.AddSingleton <IBusClient>(_ => client);
        }
Beispiel #17
0
        public static void Main(string[] args)
        {
//            var client = RabbitHutch.CreateBus("host=localhost;username=guest;password=guest");
            IBusClient client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = ConnectionStringParser.Parse("guest:guest@localhost:5672/")
            });

            client.RespondAsync <ValueRequest, ValueResponse>(r => SendValuesThoughRpcAsync(r));

            QuitEvent.WaitOne();
        }
Beispiel #18
0
        public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            RabbitMqOptions       rabbitMqOptions      = new RabbitMqOptions();
            IConfigurationSection configurationSection = configuration.GetSection("rabbitmq");

            configurationSection.Bind(rabbitMqOptions);

            RawRabbit.Instantiation.Disposable.BusClient busClient = RawRabbitFactory.CreateSingleton(new RawRabbitOptions {
                ClientConfiguration = rabbitMqOptions
            });
            services.AddSingleton <IBusClient>(serviceProvider => busClient);
        }
Beispiel #19
0
        public static IServiceCollection AddRabbitMq(this IServiceCollection service, IConfiguration configuration)
        {
            var RabbitMq = new RabbitMqOptions();
            var section  = configuration.GetSection(nameof(RabbitMq));

            section.Bind(RabbitMq);
            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = RabbitMq
            });

            return(service.AddSingleton <IBusClient>(client));
        }
Beispiel #20
0
        /// <summary>
        /// Method to add a custom raw rabbit implementation with options from my appsettings.
        /// </summary>
        /// <param name="services"> Add as a singleton service.</param>
        /// <param name="configuration"> Get the RabbitMq section.</param>
        /// <returns> IServiceCollection to follow with the pipeline.</returns>
        public static IServiceCollection AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var rabbitSection = configuration.GetSection("RabbitMq");
            var rabbitOptions = new RawRabbitConfiguration();

            rabbitSection.Bind(rabbitOptions);
            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = rabbitOptions
            });

            return(services.AddSingleton <IBusClient>(client));
        }
Beispiel #21
0
        public static void AddRabbitMQ(this IServiceCollection servi, IConfiguration config)
        {
            var opt     = new RabbitMQOpt();
            var section = config.GetSection("rabbitmq");

            section.Bind(opt);

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = opt
            });

            servi.AddSingleton <IBusClient>(x => client);
        }
Beispiel #22
0
        public static void AddRawRabbit(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = new RabbitMQSettings();
            var section  = configuration.GetSection("rabbitmq");

            section.Bind(settings);
            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = settings,
                DependencyInjection = ioc => ioc.AddSingleton <ISerializer, CustomSerializer>()
            });

            services.AddSingleton <IBusClient>(client);
            services.AddScoped <IMessageBus, RawRabbitMessageBus>();
        }
        public static void AddBusManager(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new RawRabbitConfiguration();
            var section = configuration.GetSection("rabbitmq");

            section.Bind(options);
            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(x => client);
            services.AddSingleton <BusManager>(x => new BusManager(services, x.GetService <IBusClient>()));
            services.AddCommandHandler();
        }
        private static BusClient RabbitMqClientFactory(IConfiguration configuration)
        {
            var options = new RabbitMqOptions();
            var section = configuration.GetSection("rabbitmq");

            section.Bind(options);

            return(RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options,
                Plugins = p => p
                          .UseGlobalExecutionId()
                          .UseMessageContext <MessageContext>()
            }));
        }
Beispiel #25
0
        private void ConfigureRabbitMq(IServiceCollection services)
        {
            var options = new RabbitMqOptions();
            var section = Configuration.GetSection("rabbitmq");

            section.Bind(options);

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(_ => client);
            services.AddScoped <IEventHandler <ValueCalculated>, ValueCalculatedHandler>();
        }
Beispiel #26
0
        private void ConfigureRabbitMq(IServiceCollection services, IConfigurationSection section)
        {
            var options = new RawRabbitConfiguration();

            section.Bind(options);

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(_ => client);
            services.AddSingleton <IFactorialCalculator>(_ => new Factorial());
            services.AddTransient <ICommandHandler <CalculateFactorial>, CalculateFactorialHandler>();
        }
Beispiel #27
0
        public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new RabbitMqOptions();
            var section = configuration.GetSection("RabbitMq");

            section.Bind(options);

            Console.WriteLine($"Setup RabbitMq on {options.Hostnames[0]}:{options.Port}");

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions()
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(client);
        }
Beispiel #28
0
        private void ConfigureRabbitMq(IServiceCollection services)
        {
            var options = new RabbitMqOptions();
            var section = Configuration.GetSection("rabbitmq");

            section.Bind(options);

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(_ => client);
            services.AddSingleton <ICalculator>(_ => new Fast());
            services.AddTransient <ICommandHandler <CalculateValue>, CalculateValueHandler>();
        }
Beispiel #29
0
        public static IServiceCollection AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new RawRabbitConfiguration();

            configuration.GetSection("RabbitMq").Bind(options);

            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = options
            });

            services.AddSingleton <IBusClient>(_ => client);

            services.AddScoped <IBusPublisher, BusPublisher>();

            return(services);
        }
Beispiel #30
0
        private static async Task SendCommand(RawRabbitConfiguration busConfig)
        {
            var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
            {
                ClientConfiguration = busConfig
            });

            Console.WriteLine("Sending command and waiting for response...");

            var submitOrder = new SubmitOrder(Guid.NewGuid(), $"Order - {DateTime.Now}", 1230);

            var response = await client.RequestAsync <SubmitOrder, SubmitOrderResponse>(submitOrder);

            Console.WriteLine($"Command {submitOrder.Id} sent successfully.");

            Console.WriteLine($"Response: {response.Message}");
        }