|
| 1 | +using Autofac.Extensions.DependencyInjection; |
| 2 | +using Autofac; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using System; |
| 6 | +using Ordering.BackgroundTasksHost.Configuration; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; |
| 10 | +using Microsoft.Azure.ServiceBus; |
| 11 | +using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; |
| 12 | +using RabbitMQ.Client; |
| 13 | +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
| 14 | +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
| 15 | +using Ordering.BackgroundTasksHost.Tasks; |
| 16 | + |
| 17 | +namespace Ordering.BackgroundTasksHost |
| 18 | +{ |
| 19 | + class Program |
| 20 | + { |
| 21 | + static void Main(string[] args) |
| 22 | + { |
| 23 | + using (var host = CreateHost(args)) |
| 24 | + { |
| 25 | + host.Start(); |
| 26 | + |
| 27 | + host.WaitForShutdown(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + static IHost CreateHost(string[] args) |
| 32 | + { |
| 33 | + var host = new HostBuilder() |
| 34 | + .ConfigureAppConfiguration((hostContext, configApp) => |
| 35 | + { |
| 36 | + configApp.AddEnvironmentVariables(); |
| 37 | + configApp.AddJsonFile("appsettings.json", optional: true); |
| 38 | + configApp.AddJsonFile( |
| 39 | + $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", |
| 40 | + optional: true); |
| 41 | + configApp.AddCommandLine(args); |
| 42 | + |
| 43 | + }) |
| 44 | + .ConfigureServices(services => |
| 45 | + { |
| 46 | + var configuration = services.BuildServiceProvider() |
| 47 | + .GetRequiredService<IConfiguration>(); |
| 48 | + |
| 49 | + services.AddOptions() |
| 50 | + .Configure<BackgroundTaskSettings>(configuration) |
| 51 | + .RegisterBus(configuration) |
| 52 | + .RegisterHostedServices(); |
| 53 | + |
| 54 | + }) |
| 55 | + .UseServiceProviderFactory(new AutofacServiceProviderFactory()) |
| 56 | + .Build(); |
| 57 | + |
| 58 | + return host; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + class AutofacServiceProviderFactory |
| 65 | + : IServiceProviderFactory<ContainerBuilder> |
| 66 | + { |
| 67 | + public ContainerBuilder CreateBuilder(IServiceCollection services) |
| 68 | + { |
| 69 | + var containerBuilder = new ContainerBuilder(); |
| 70 | + containerBuilder.Populate(services); |
| 71 | + |
| 72 | + return containerBuilder; |
| 73 | + } |
| 74 | + |
| 75 | + public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder) |
| 76 | + { |
| 77 | + return new AutofacServiceProvider(containerBuilder.Build()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static class ServiceCollectionExtensions |
| 82 | + { |
| 83 | + public static IServiceCollection RegisterBus(this IServiceCollection services, IConfiguration configuration) |
| 84 | + { |
| 85 | + if (configuration.GetValue<bool>("AzureServiceBusEnabled")) |
| 86 | + { |
| 87 | + services.AddSingleton<IServiceBusPersisterConnection>(sp => |
| 88 | + { |
| 89 | + var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
| 90 | + |
| 91 | + var serviceBusConnectionString = configuration["EventBusConnection"]; |
| 92 | + var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); |
| 93 | + |
| 94 | + return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); |
| 95 | + }); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 100 | + { |
| 101 | + var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
| 102 | + |
| 103 | + |
| 104 | + var factory = new ConnectionFactory() |
| 105 | + { |
| 106 | + HostName = configuration["EventBusConnection"] |
| 107 | + }; |
| 108 | + |
| 109 | + if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) |
| 110 | + { |
| 111 | + factory.UserName = configuration["EventBusUserName"]; |
| 112 | + } |
| 113 | + |
| 114 | + if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) |
| 115 | + { |
| 116 | + factory.Password = configuration["EventBusPassword"]; |
| 117 | + } |
| 118 | + |
| 119 | + var retryCount = 5; |
| 120 | + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) |
| 121 | + { |
| 122 | + retryCount = int.Parse(configuration["EventBusRetryCount"]); |
| 123 | + } |
| 124 | + |
| 125 | + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + var subscriptionClientName = configuration["SubscriptionClientName"]; |
| 131 | + |
| 132 | + if (configuration.GetValue<bool>("AzureServiceBusEnabled")) |
| 133 | + { |
| 134 | + services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
| 135 | + { |
| 136 | + var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
| 137 | + var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
| 138 | + var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
| 139 | + var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
| 140 | + |
| 141 | + return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
| 142 | + eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); |
| 143 | + }); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => |
| 148 | + { |
| 149 | + var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); |
| 150 | + var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
| 151 | + var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); |
| 152 | + var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
| 153 | + |
| 154 | + var retryCount = 5; |
| 155 | + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) |
| 156 | + { |
| 157 | + retryCount = int.Parse(configuration["EventBusRetryCount"]); |
| 158 | + } |
| 159 | + |
| 160 | + return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
| 165 | + |
| 166 | + return services; |
| 167 | + } |
| 168 | + |
| 169 | + public static IServiceCollection RegisterHostedServices(this IServiceCollection services) |
| 170 | + { |
| 171 | + services.AddSingleton<IHostedService, GracePeriodManagerService>(); |
| 172 | + |
| 173 | + return services; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments