Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            _appSettings = Config.GetSection("AppSettings").Get <AppSettings>();
            services.AddSingleton(a => _appSettings);

            var rabbitConfig = new RabbitConfig();

            Config.GetSection("RabbitSettings").Bind(rabbitConfig);
            RabbitConfig.UserName = DecryptCypherText(RabbitConfig.UserName);             // always decrypt credentials at runtime
            RabbitConfig.Password = DecryptCypherText(RabbitConfig.Password);             // always decrypt credentials at runtime

            var rabbitConnection = ConnectionProvider.CreateConnection();

            services.AddSingleton(rabbitConnection);

            //Shared
            services.AddSingleton <ChannelProvider>();
            services.AddSingleton <ConnectionProvider>();

            //Publish
            services.AddSingleton <Marshaller>();
            services.AddSingleton <MessageSender>();
            services.AddSingleton <Sender>();
            var senderProvider = new SenderProvider();

            services.AddSingleton(senderProvider);
            services.AddSingleton <SenderWrapper>();

            //Subscribe
            services.AddTransient <MessageProcessor>();
            services.AddSingleton <MessageDispatcher>();

            if (Debugger.IsAttached)
            {
                var temp = services.BuildServiceProvider();
                _dispatcher = temp.GetService <MessageDispatcher>();
                _dispatcher.StartDispatchers(RabbitConfig.DispatcherCount);
            }

            //Emulate pub/sub eventing
            var processor = new PublishGenerator(senderProvider);

            processor.StartPubSubLoop();
        }         // end ConfigureServices