diff --git a/Source/Orleankka.Runtime/Cluster/ClusterActorSystem.cs b/Source/Orleankka.Runtime/Cluster/ClusterActorSystem.cs index f6139c68..fe3cf93b 100644 --- a/Source/Orleankka.Runtime/Cluster/ClusterActorSystem.cs +++ b/Source/Orleankka.Runtime/Cluster/ClusterActorSystem.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Microsoft.Extensions.DependencyInjection; namespace Orleankka.Cluster { @@ -14,14 +15,10 @@ class ClusterActorSystem : ActorSystem readonly IActorMiddleware actorMiddleware; - internal ClusterActorSystem( - Assembly[] assemblies, - IServiceProvider serviceProvider, - IActorRefMiddleware actorRefMiddleware = null, - IActorMiddleware actorMiddleware = null) - : base(assemblies, serviceProvider, actorRefMiddleware) + internal ClusterActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider) + : base(assemblies, serviceProvider) { - this.actorMiddleware = actorMiddleware ?? DefaultActorMiddleware.Instance; + this.actorMiddleware = serviceProvider.GetService() ?? DefaultActorMiddleware.Instance; Register(assemblies); } diff --git a/Source/Orleankka.Runtime/Cluster/ClusterOptions.cs b/Source/Orleankka.Runtime/Cluster/ClusterOptions.cs index 1f30f9e7..bf684be7 100644 --- a/Source/Orleankka.Runtime/Cluster/ClusterOptions.cs +++ b/Source/Orleankka.Runtime/Cluster/ClusterOptions.cs @@ -6,56 +6,40 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; + using Orleans; using Orleans.ApplicationParts; using Orleans.CodeGeneration; -using Orleans.Configuration.Overrides; using Orleans.Hosting; using Orleans.Runtime; namespace Orleankka.Cluster { using Client; - using Utility; - public sealed class OrleankkaClusterOptions + public static class SiloHostBuilderExtension { - IActorRefMiddleware actorRefMiddleware; - IActorMiddleware actorMiddleware; - - /// - /// Registers global actor middleware (interceptor). - /// - /// The middleware. - public OrleankkaClusterOptions ActorMiddleware(IActorMiddleware middleware) - { - actorMiddleware = middleware; - return this; - } - - /// - /// Registers global cluster-wide middleware (interceptor) - /// - /// The middleware. - public OrleankkaClusterOptions ActorRefMiddleware(IActorRefMiddleware middleware) - { - Requires.NotNull(middleware, nameof(middleware)); + public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder) => + builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services)); - if (actorRefMiddleware != null) - throw new InvalidOperationException("ActorRef middleware for cluster has been already registered"); + public static ISiloBuilder UseOrleankka(this ISiloBuilder builder) => + builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services)); - actorRefMiddleware = middleware; - return this; + static void UseOrleankka(IApplicationPartManager apm, IServiceCollection services) + { + Configure(apm, services); + apm.AddApplicationPart(typeof(IClientEndpoint).Assembly) + .WithCodeGeneration(); } - - internal void Configure(IApplicationPartManager apm, IServiceCollection services) + + static void Configure(IApplicationPartManager apm, IServiceCollection services) { var assemblies = apm.ApplicationParts .OfType().Select(x => x.Assembly) .ToArray(); - services.AddSingleton(sp => new ClusterActorSystem(assemblies, sp, actorRefMiddleware, actorMiddleware)); - services.AddSingleton(sp => new ClientActorSystem(assemblies, sp, actorRefMiddleware)); + services.AddSingleton(sp => new ClusterActorSystem(assemblies, sp)); + services.AddSingleton(sp => new ClientActorSystem(assemblies, sp)); services.AddSingleton(sp => sp.GetService()); services.AddSingleton(sp => sp.GetService()); @@ -70,7 +54,7 @@ internal void Configure(IApplicationPartManager apm, IServiceCollection services services.AddOptions(); } - DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerable assemblies) + static DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerable assemblies) { var dispatchActors = assemblies.SelectMany(x => x.GetTypes()) .Where(x => typeof(DispatchActorGrain).IsAssignableFrom(x) && !x.IsAbstract); @@ -83,33 +67,6 @@ DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerabl return dispatcherRegistry; } - } - - public static class SiloHostBuilderExtension - { - public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder) => - UseOrleankka(builder, new OrleankkaClusterOptions()); - - public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder, Func configure) => - UseOrleankka(builder, configure(new OrleankkaClusterOptions())); - - public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder, OrleankkaClusterOptions cfg) => - builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services, cfg)); - - public static ISiloBuilder UseOrleankka(this ISiloBuilder builder) => - UseOrleankka(builder, new OrleankkaClusterOptions()); - - public static ISiloBuilder UseOrleankka(this ISiloBuilder builder, Func configure) => - UseOrleankka(builder, configure(new OrleankkaClusterOptions())); - - public static ISiloBuilder UseOrleankka(this ISiloBuilder builder, OrleankkaClusterOptions cfg) => - builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services, cfg)); - - static void UseOrleankka(IApplicationPartManager apm, IServiceCollection services, OrleankkaClusterOptions cfg) - { - cfg.Configure(apm, services); - apm.AddApplicationPart(typeof(IClientEndpoint).Assembly).WithCodeGeneration(); - } public static IClientActorSystem ActorSystem(this ISiloHost host) => host.Services.GetRequiredService(); } diff --git a/Source/Orleankka/ActorSystem.cs b/Source/Orleankka/ActorSystem.cs index ec5051a1..d3a44392 100644 --- a/Source/Orleankka/ActorSystem.cs +++ b/Source/Orleankka/ActorSystem.cs @@ -52,14 +52,11 @@ public abstract class ActorSystem : IActorSystem readonly IGrainFactory grainFactory; readonly IActorRefMiddleware actorRefMiddleware; - protected ActorSystem( - Assembly[] assemblies, - IServiceProvider serviceProvider, - IActorRefMiddleware actorRefMiddleware = null) + protected ActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider) { this.serviceProvider = serviceProvider; this.grainFactory = serviceProvider.GetService(); - this.actorRefMiddleware = actorRefMiddleware ?? DefaultActorRefMiddleware.Instance; + this.actorRefMiddleware = serviceProvider.GetService() ?? DefaultActorRefMiddleware.Instance; Register(assemblies); } diff --git a/Source/Orleankka/Client/ClientActorSystem.cs b/Source/Orleankka/Client/ClientActorSystem.cs index dc371cfb..76c374a8 100644 --- a/Source/Orleankka/Client/ClientActorSystem.cs +++ b/Source/Orleankka/Client/ClientActorSystem.cs @@ -27,11 +27,8 @@ public sealed class ClientActorSystem : ActorSystem, IClientActorSystem { readonly IGrainFactory grainFactory; - internal ClientActorSystem( - Assembly[] assemblies, - IServiceProvider serviceProvider, - IActorRefMiddleware actorRefMiddleware = null) - : base(assemblies, serviceProvider, actorRefMiddleware) + internal ClientActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider) + : base(assemblies, serviceProvider) { grainFactory = serviceProvider.GetService(); } diff --git a/Source/Orleankka/Client/ClientOptions.cs b/Source/Orleankka/Client/ClientOptions.cs index f25883be..7ad41434 100644 --- a/Source/Orleankka/Client/ClientOptions.cs +++ b/Source/Orleankka/Client/ClientOptions.cs @@ -9,54 +9,26 @@ namespace Orleankka.Client { - using Utility; - - public sealed class OrleankkaClientOptions + public static class ClientBuilderExtension { - IActorRefMiddleware middleware; - - /// - /// Registers global middleware (interceptor) - /// - /// The middleware. - public OrleankkaClientOptions ActorRefMiddleware(IActorRefMiddleware middleware) - { - Requires.NotNull(middleware, nameof(middleware)); - - if (this.middleware != null) - throw new InvalidOperationException("ActorRef middleware has been already registered"); - - this.middleware = middleware; - return this; - } + public static IClientBuilder UseOrleankka(this IClientBuilder builder) => + builder + .ConfigureServices(services => Configure(builder, services)) + .ConfigureApplicationParts(apm => apm + .AddApplicationPart(typeof(IClientEndpoint).Assembly) + .WithCodeGeneration()); - internal void Configure(IClientBuilder builder, IServiceCollection services) + static void Configure(IClientBuilder builder, IServiceCollection services) { var assemblies = builder.GetApplicationPartManager().ApplicationParts - .OfType().Select(x => x.Assembly) - .ToArray(); + .OfType().Select(x => x.Assembly) + .ToArray(); services.AddSingleton(sp => sp.GetService()); services.AddSingleton(sp => sp.GetService()); - services.AddSingleton(sp => new ClientActorSystem(assemblies, sp, middleware)); + services.AddSingleton(sp => new ClientActorSystem(assemblies, sp)); } - } - - public static class ClientBuilderExtension - { - public static IClientBuilder UseOrleankka(this IClientBuilder builder) => - UseOrleankka(builder, new OrleankkaClientOptions()); - - public static IClientBuilder UseOrleankka(this IClientBuilder builder, Func configure) => - UseOrleankka(builder, configure(new OrleankkaClientOptions())); - - public static IClientBuilder UseOrleankka(this IClientBuilder builder, OrleankkaClientOptions cfg) => - builder - .ConfigureServices(services => cfg.Configure(builder, services)) - .ConfigureApplicationParts(apm => apm - .AddApplicationPart(typeof(IClientEndpoint).Assembly) - .WithCodeGeneration()); public static IClientActorSystem ActorSystem(this IClusterClient client) => client.ServiceProvider.GetRequiredService(); } diff --git a/Tests/Orleankka.Tests/Testing/TestActions.cs b/Tests/Orleankka.Tests/Testing/TestActions.cs index 5a813ab3..11a5e90a 100644 --- a/Tests/Orleankka.Tests/Testing/TestActions.cs +++ b/Tests/Orleankka.Tests/Testing/TestActions.cs @@ -83,14 +83,15 @@ public override void BeforeTest(ITest test) var customConventions = Dispatcher.DefaultHandlerNamingConventions.Concat(new[]{"OnFoo"}); o.HandlerNamingConventions = customConventions.ToArray(); }); + + services.AddSingleton(s => new TestActorRefMiddleware()); + services.AddSingleton(s => new TestActorMiddleware()); }) .ConfigureApplicationParts(x => x .AddApplicationPart(GetType().Assembly) .AddApplicationPart(typeof(MemoryGrainStorage).Assembly) .WithCodeGeneration()) - .UseOrleankka(x => x - .ActorMiddleware(new TestActorMiddleware()) - .ActorRefMiddleware(new TestActorRefMiddleware())) + .UseOrleankka() .UseOrleankkaLegacyFeatures(x => x .AddSimpleMessageStreamProvider("sms") .RegisterPersistentStreamProviders("aqp"));