diff --git a/samples/apis/Backet.Api/Controllers/BacketController.cs b/samples/apis/Backet.Api/Controllers/BacketController.cs index 405abc0..2b63024 100644 --- a/samples/apis/Backet.Api/Controllers/BacketController.cs +++ b/samples/apis/Backet.Api/Controllers/BacketController.cs @@ -44,8 +44,7 @@ namespace Backet.Api.Controllers public Task RemoveFirstItem() { var id = "da8a489fa7b4409294ee1b358fbbfba5"; - var grain = clusterClient.GetGrain(id); - clusterClient. + var grain = clusterClient.GetGrain(id); return grain.RemoveFirstItem(); } } diff --git a/samples/apis/Backet.Api/Startup.cs b/samples/apis/Backet.Api/Startup.cs index 9436c30..8ef5c57 100644 --- a/samples/apis/Backet.Api/Startup.cs +++ b/samples/apis/Backet.Api/Startup.cs @@ -27,6 +27,15 @@ namespace Backet.Api services.AddDbContextPool(options => options.UseNpgsql(Configuration["postgres:write"])); services.AddControllers(); + services.AddPole(config => { + config.AddRabbitMQ(option => + { + option.Hosts = new string[1] { Configuration["RabbitmqConfig:HostAddress"] }; + option.Password = Configuration["RabbitmqConfig:HostPassword"]; + option.UserName = Configuration["RabbitmqConfig:HostUserName"]; + }); + }); + services.ConfigureGrainStorageOptions( options => { @@ -44,6 +53,7 @@ namespace Backet.Api app.UseDeveloperExceptionPage(); } + app.UsePole(); app.UseRouting(); app.UseEndpoints(endpoints => diff --git a/src/Pole.Core/EventBus/ObserverUnitContainer.cs b/src/Pole.Core/EventBus/ObserverUnitContainer.cs index 3b5c0f7..e832ec4 100644 --- a/src/Pole.Core/EventBus/ObserverUnitContainer.cs +++ b/src/Pole.Core/EventBus/ObserverUnitContainer.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Logging; -using Pole.Core.Observer; using Pole.Core.Utils; using System; using System.Collections.Concurrent; diff --git a/src/Pole.Core/Extensions/IServiceProviderExtensions.cs b/src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs similarity index 84% rename from src/Pole.Core/Extensions/IServiceProviderExtensions.cs rename to src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs index 608a6ab..45e5ec4 100644 --- a/src/Pole.Core/Extensions/IServiceProviderExtensions.cs +++ b/src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs @@ -1,11 +1,12 @@ using Microsoft.AspNetCore.Builder; +using Pole.Core; using System; using System.Collections.Generic; using System.Text; -namespace Pole.Core.Extensions +namespace Microsoft.AspNetCore.Builder { - public static class IApplicationBuilderExtensions + public static class PoleApplicationBuilderExtensions { public static IApplicationBuilder UsePole(this IApplicationBuilder applicationBuilder) { diff --git a/src/Pole.Core/Extensions/IServiceCollectionExtensions.cs b/src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs similarity index 83% rename from src/Pole.Core/Extensions/IServiceCollectionExtensions.cs rename to src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs index 8464d7b..0ebe8ef 100644 --- a/src/Pole.Core/Extensions/IServiceCollectionExtensions.cs +++ b/src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Pole.Core; using Pole.Core.Abstraction; using Pole.Core.Channels; using Pole.Core.EventBus; @@ -12,12 +13,17 @@ using System; using System.Collections.Generic; using System.Text; -namespace Pole.Core.Extensions +namespace Microsoft.Extensions.DependencyInjection { - public static class IServiceCollectionExtensions + public static class PoleServiceCollectionExtensions { - public static IServiceCollection AddPole(this IServiceCollection services,Action config) + public static IServiceCollection AddPole(this IServiceCollection services,Action config) { + StartupConfig startupOption = new StartupConfig(services); + if (startupOption.PoleOptionsConfig == null) + { + services.Configure(option => { }); + } services.AddSingleton(); services.AddTransient(typeof(IMpscChannel<>), typeof(MpscChannel<>)); services.AddScoped(); @@ -26,10 +32,11 @@ namespace Pole.Core.Extensions services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); + + config(startupOption); return services; } } diff --git a/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs b/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs index fe8a0b8..fcba568 100644 --- a/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs +++ b/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Pole.Core.EventBus.EventStorage; using System; using System.Collections.Generic; using System.Linq; @@ -21,6 +22,8 @@ namespace Pole.Core.Processor.Server } public async Task Start(CancellationToken stoppingToken) { + var eventStorageInitializer = _serviceProvider.GetService(); + await eventStorageInitializer.InitializeAsync(stoppingToken); ProcessingContext processingContext = new ProcessingContext(stoppingToken); List loopProcessors = new List(); diff --git a/src/Pole.Core/Startup.cs b/src/Pole.Core/Startup.cs index 8f98d93..e77a66a 100644 --- a/src/Pole.Core/Startup.cs +++ b/src/Pole.Core/Startup.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -28,4 +29,13 @@ namespace Pole.Core public Func Func { get; set; } } } + public class StartupConfig + { + public StartupConfig(IServiceCollection services) + { + Services = services; + } + public IServiceCollection Services { get; } + public Action PoleOptionsConfig { get; set; } + } } diff --git a/src/Pole.EventBus.Rabbitmq/Extensions.cs b/src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs similarity index 66% rename from src/Pole.EventBus.Rabbitmq/Extensions.cs rename to src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs index 91bed51..837b8a7 100644 --- a/src/Pole.EventBus.Rabbitmq/Extensions.cs +++ b/src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs @@ -3,21 +3,22 @@ using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Pole.Core; using Pole.Core.EventBus; +using Pole.EventBus.RabbitMQ; -namespace Pole.EventBus.RabbitMQ +namespace Microsoft.Extensions.DependencyInjection { - public static class Extensions + public static class PoleRabbitmqStartupConfigExtensions { public static void AddRabbitMQ( - this IServiceCollection serviceCollection, + this StartupConfig startupOption, Action rabbitConfigAction, Func eventBusConfig = default) { - serviceCollection.Configure(config => rabbitConfigAction(config)); - serviceCollection.AddSingleton(); - serviceCollection.AddHostedService(); - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(serviceProvider => serviceProvider.GetService() as IProducerContainer); + startupOption.Services.Configure(config => rabbitConfigAction(config)); + startupOption.Services.AddSingleton(); + startupOption.Services.AddHostedService(); + startupOption.Services.AddSingleton(); + startupOption.Services.AddSingleton(serviceProvider => serviceProvider.GetService() as IProducerContainer); Startup.Register(async serviceProvider => { var container = serviceProvider.GetService(); diff --git a/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs b/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs index b23cbd1..b061622 100644 --- a/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs +++ b/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs @@ -9,36 +9,6 @@ namespace Pole.EventStorage.PostgreSql { public static class CapOptionsExtensions { - public static PoleOptions UseEntityFrameworkEventStorage(this PoleOptions options) - where TContext : DbContext - { - return options.UseEntityFrameworkEventStorage(opt => { }); - } - public static PoleOptions UseEntityFrameworkEventStorage(this PoleOptions options, Action configure) - where TContext : DbContext - { - if (configure == null) throw new ArgumentNullException(nameof(configure)); - EFOptions eFOptions = new EFOptions(); - configure(eFOptions); - Action postgreSqlOptionsConfig = postgreSqlOptions => - { - postgreSqlOptions.DbContextType = typeof(TContext); - postgreSqlOptions.Schema = eFOptions.Schema; - using var scope = options.Services.BuildServiceProvider().CreateScope(); - var provider = scope.ServiceProvider; - using var dbContext = (DbContext)provider.GetRequiredService(typeof(TContext)); - postgreSqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; - }; - options.Services.Configure(postgreSqlOptionsConfig); - - return options; - } - public static PoleOptions UsePostgreSqlEventStorage(this PoleOptions options, Action configure) - where TContext : DbContext - { - if (configure == null) throw new ArgumentNullException(nameof(configure)); - options.Services.Configure(configure); - return options; - } + } } diff --git a/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs b/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs new file mode 100644 index 0000000..0b2e8ba --- /dev/null +++ b/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using Pole.Core; +using Pole.Core.EventBus.EventStorage; +using Pole.Core.EventBus.Transaction; +using Pole.EventStorage.PostgreSql; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class PolePostgreSqlStartupConfigExtensions + { + public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config) + where TContext : DbContext + { + return config.AddEntityFrameworkEventStorage(opt => { }); + } + public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config, Action configure) + where TContext : DbContext + { + if (configure == null) throw new ArgumentNullException(nameof(configure)); + EFOptions eFOptions = new EFOptions(); + configure(eFOptions); + Action postgreSqlOptionsConfig = postgreSqlOptions => + { + postgreSqlOptions.DbContextType = typeof(TContext); + postgreSqlOptions.Schema = eFOptions.Schema; + using var scope = config.Services.BuildServiceProvider().CreateScope(); + var provider = scope.ServiceProvider; + using var dbContext = (DbContext)provider.GetRequiredService(typeof(TContext)); + postgreSqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; + }; + config.Services.Configure(postgreSqlOptionsConfig); + + config.Services.AddScoped(); + config.Services.AddSingleton(); + config.Services.AddSingleton(); + return config; + } + } +}