public BrokerFactoryBase(ServiceBusConfiguration configuration, IAttributeProvider <A> attributeProvider = null)
        {
            _configuration     = configuration ?? throw new ArgumentNullException(nameof(configuration));
            _attributeProvider = attributeProvider ?? new SimpleAttributeProvider <A>();

            _lazyManagementClient = new Lazy <ManagementClient>(new ManagementClient(configuration.ConnectionString));

            _senderClientsDic = new ConcurrentDictionary <string, ISenderClient>();
            _semaphoresDic    = new ConcurrentDictionary <string, SemaphoreSlim>();
            _attributesDic    = new ConcurrentDictionary <Type, A>();
        }
Esempio n. 2
0
        private static void configureServices(HostBuilderContext hostContext, IServiceCollection services)
        {
            IConfiguration configuration = hostContext.Configuration;

            // --> Add: BrokerFactory depending on the environment.
            if (hostContext.HostingEnvironment.IsProduction())
            {
                var brokerFactoryConfiguration = new BrokerFactoryConfiguration
                {
                    Url                       = configuration.GetConnectionString("RabbitMQ"),
                    SkipManagement            = false,
                    DefaultDeadLetterExchange = "message.morgue",
                    DefaultDeadLetterQueue    = "message.morgue.sink"
                };

                services
                .AddSingleton(brokerFactoryConfiguration)
                .AddSingleton <IBrokerFactory, BrokerFactory>();
            }
            else
            {
                // In-memory queuing.
                services.AddSingleton <IBrokerFactory, Queue.InMemory.BrokerFactory>();

                // File system queuing.
                //services.AddSingleton<IBrokerFactory>(new Queue.FileSystem.BrokerFactory(@"d:\Downloads\Messages"));

                // Redis pub/sub messaging.
                //services.AddSingleton<IBrokerFactory>(new Queue.Redis.BrokerFactory());

                var sbConfiguration = new Queue.Azure.ServiceBus.ServiceBusConfiguration
                {
                    ConnectionString = configuration.GetConnectionString("ServiceBus"),
                    SkipManagement   = false
                };

                // Azure queue.
                //services.AddSingleton<IBrokerFactory>(x => new Queue.Azure.ServiceBus.Queue.BrokerFactory(sbConfiguration));

                // Azure topic.
                //services.AddSingleton<IBrokerFactory>(x => new Queue.Azure.ServiceBus.Topic.BrokerFactory(sbConfiguration));
            }

            // --> Add: DelaySettings.
            services.AddSingleton(configuration.BindTo <DelaySettings>());

            // --> Add: Message handlers with Scrutor.
            services.Scan(scan => scan
                          .FromEntryAssembly()
                          .AddClasses(classes => classes.AssignableTo(typeof(IMessageHandler <>)))
                          //.UsingRegistrationStrategy(RegistrationStrategy.Append) // Default is Append.
                          .AsImplementedInterfaces()
                          .WithSingletonLifetime());
            // Note: If the handler has scope dependencies, it should be present as a scope handler.

            // These handlers will be added by Scrutor.
            //services.AddSingleton<IMessageHandler<LoginMessage>, LoginMessageHandler>();
            //services.AddSingleton<IMessageHandler<PurchaseMessage>, PurchaseMessageHandler>();

            // --> Add: Background services.
            // Message consumers in BackgroundService.
            services.AddHostedService <ConsumerBackgroundService <LoginMessage> >();
            services.AddHostedService <ConsumerBackgroundService <PurchaseMessage> >();

            // Demo purpose.
            services.AddHostedService <ProducerBackgroundService>();
        }