public static BusBuilder AddRabbitMQ(this IServiceCollection services, IConfiguration configuration, Action <RabbitMQOption> optionAction) { RabbitMQOption option = new RabbitMQOption(); optionAction(option); var factory = new ConnectionFactory(); var section = configuration.GetSection(option.ConnectionFactory); section.Bind(factory); var connection = factory.CreateConnection(); var channel = connection.CreateModel(); var queues = new Queues(channel); services.AddSingleton(channel); services.AddScoped <IRabbitMQEventBus, RabbitMQEventBus>(); services.AddScoped <PublishMessage>(); services.AddSingleton(queues); services.AddSingleton((RabbitMQTrackException)option); var serverProvaider = services.BuildServiceProvider(); IModel bus = serverProvaider.GetRequiredService <IModel>(); return(new BusBuilder(bus, services)); }
public static IServiceProvider AddEventBusAsAutofacService(this IServiceCollection services, RabbitMQOption rabbitMqOption, Action <ICollection <Type> > eventHandlerOption) { var container = new ContainerBuilder(); container.Populate(AddEventBus(services, rabbitMqOption, eventHandlerOption)); return(new AutofacServiceProvider(container.Build())); }
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy(ALLOWED_ORIGIN_POLICY, builder => { builder.AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin(); }); }); services.AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include; options.SerializerSettings.StringEscapeHandling = StringEscapeHandling.Default; options.SerializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full; options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; options.SerializerSettings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor; }) .AddApplicationPart(typeof(HomeController).Assembly); #region Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo()); }); #endregion #region Db DbOption dbOption = AppConfigs.SelectedDbOption(); switch (dbOption.DbType) { case DbTypes.SqlServer: services.AddDbContext <DataContext>(builder => builder.UseSqlServer(dbOption.ConnectionStr)); break; default: throw new ArgumentOutOfRangeException(); } #endregion #region RabbitMQ RabbitMQConfigModel rabbitMqConfigModel = AppConfigs.GetRabbitMQConfigModel(); RabbitMQOption rabbitMqOption = rabbitMqConfigModel.SelectedRabbitMQOption(); services.AddSingleton(rabbitMqConfigModel); Type interfaceType = typeof(ICapSubscribe); IEnumerable <Type> types = typeof(AccountCreated_VerificationMailSender) .Assembly .GetTypes() .Where(t => interfaceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract); foreach (Type t in types) { services.Add(new ServiceDescriptor(typeof(ICapSubscribe), t, ServiceLifetime.Transient)); } services.AddCap(configurator => { configurator.UseEntityFramework <DataContext>(); configurator.UseRabbitMQ(options => { options.UserName = rabbitMqOption.UserName; options.Password = rabbitMqOption.Password; options.HostName = rabbitMqOption.HostName; options.VirtualHost = rabbitMqOption.VirtualHost; } ); } ); #endregion #region IntegrationEventPublisher services.AddSingleton <ICapIntegrationEventPublisher, CapIntegrationEventPublisher>(); #endregion #region HealthCheck IHealthChecksBuilder healthChecksBuilder = services.AddHealthChecks(); healthChecksBuilder.AddUrlGroup(new Uri($"{AppConfigs.AppUrls().First()}/health-check"), HttpMethod.Get, name: "HealthCheck Endpoint"); switch (dbOption.DbType) { case DbTypes.SqlServer: healthChecksBuilder.AddSqlServer(dbOption.ConnectionStr, name: "Sql Server"); break; default: throw new ArgumentOutOfRangeException(); } switch (rabbitMqOption.BrokerType) { case MessageBrokerTypes.RabbitMq: string rabbitConnStr = $"amqp://{rabbitMqOption.UserName}:{rabbitMqOption.Password}@{rabbitMqOption.HostName}:5672{rabbitMqOption.VirtualHost}"; healthChecksBuilder.AddRabbitMQ(rabbitConnStr, sslOption: null, name: "RabbitMq", HealthStatus.Unhealthy, new[] { "rabbitmq" }); break; default: throw new ArgumentOutOfRangeException(); } services .AddHealthChecksUI(setup => { setup.MaximumHistoryEntriesPerEndpoint(50); setup.AddHealthCheckEndpoint("StockManagement Project", $"{AppConfigs.AppUrls().First()}/healthz"); }) .AddInMemoryStorage(); #endregion services.AddScoped <IAccountService, AccountService>(); }
public static IServiceCollection AddEventBus(this IServiceCollection services, RabbitMQOption rabbitMqOption, Action <ICollection <Type> > eventHandlerOption) { int port = 5672; string hostName = rabbitMqOption.EventBusConnection; if (rabbitMqOption.EventBusConnection.Contains(":")) { string[] hostPort = rabbitMqOption.EventBusConnection.Split(':'); hostName = hostPort[0]; port = Convert.ToInt32(hostPort[1]); } //添加RabbitMQ持久化连接单例 services.AddSingleton <IRabbitMQPersistentConnection, DefaultRabbitMQPersistentConnection>(sp => new DefaultRabbitMQPersistentConnection(new ConnectionFactory() { HostName = hostName, Port = port, UserName = rabbitMqOption.EventBusUserName, Password = rabbitMqOption.EventBusPassword }, sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(), rabbitMqOption.EventBusRetryCount)); var subscriptionClientName = rabbitMqOption.SubscriptionClientName; services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp => { var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>(); var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>(); var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var retryCount = 5; if (rabbitMqOption.EventBusRetryCount > 0) { retryCount = rabbitMqOption.EventBusRetryCount; } return(new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, rabbitMqOption.EventBusBrokeName, subscriptionClientName, retryCount)); }); services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); ICollection <Type> eventHandlers = new List <Type>(); eventHandlerOption?.Invoke(eventHandlers); foreach (var handler in eventHandlers) { services.AddTransient(handler); } return(services); }
public ConsumerClient(ClientOption clientOption, IChannelPool channelPool, RabbitMQOption options) { _clientOption = clientOption; _channelPool = channelPool; _rabbitMQOptions = options; }