diff --git a/src/Autofac.Integration.ServiceFabric/ActorFactoryRegistration.cs b/src/Autofac.Integration.ServiceFabric/ActorFactoryRegistration.cs index d68bf93..06e9104 100644 --- a/src/Autofac.Integration.ServiceFabric/ActorFactoryRegistration.cs +++ b/src/Autofac.Integration.ServiceFabric/ActorFactoryRegistration.cs @@ -37,10 +37,15 @@ internal sealed class ActorFactoryRegistration : IActorFactoryRegistration { internal Action ConstructorExceptionCallback { get; } + internal Action ConfigurationAction { get; } + // ReSharper disable once UnusedMember.Global - public ActorFactoryRegistration(Action constructorExceptionCallback) + public ActorFactoryRegistration( + Action constructorExceptionCallback, + Action configurationAction) { ConstructorExceptionCallback = constructorExceptionCallback; + ConfigurationAction = configurationAction; } public void RegisterActorFactory( @@ -66,7 +71,10 @@ ActorBase ActorFactory(ActorService actorService, ActorId actorId) .As(); builder.RegisterInstance(actorId) .As(); + + ConfigurationAction(builder); }); + try { var actor = lifetimeScope.Resolve(); diff --git a/src/Autofac.Integration.ServiceFabric/RegistrationExtensions.cs b/src/Autofac.Integration.ServiceFabric/RegistrationExtensions.cs index 09cbd2a..8ce4915 100644 --- a/src/Autofac.Integration.ServiceFabric/RegistrationExtensions.cs +++ b/src/Autofac.Integration.ServiceFabric/RegistrationExtensions.cs @@ -44,14 +44,18 @@ public static class RegistrationExtensions /// Adds the core services required by the Service Fabric integration. /// /// The container builder to register the services with. - /// Callback will be invoked if there are an exception thrown during resolving. - public static void RegisterServiceFabricSupport(this ContainerBuilder builder, Action constructorExceptionCallback = null) + /// Callback will be invoked if an exception is thrown during resolving. + /// Callback will be invoked while configuring the lifetime scope for a service. + public static void RegisterServiceFabricSupport( + this ContainerBuilder builder, + Action constructorExceptionCallback = null, + Action configurationAction = null) { if (builder == null) throw new ArgumentNullException(nameof(builder)); if (builder.Properties.ContainsKey(MetadataKey)) return; - builder.AddInternalRegistrations(constructorExceptionCallback); + builder.AddInternalRegistrations(constructorExceptionCallback, configurationAction); builder.Properties.Add(MetadataKey, true); } @@ -87,9 +91,13 @@ internal static IRegistrationBuilder constructorExceptionCallback = null) + private static void AddInternalRegistrations( + this ContainerBuilder builder, + Action constructorExceptionCallback = null, + Action configurationAction = null) { - var callback = constructorExceptionCallback ?? (ex => { }); + var exceptionCallback = constructorExceptionCallback ?? (ex => { }); + var configurationCallback = configurationAction ?? (_ => { }); builder.RegisterType() .InstancePerLifetimeScope(); @@ -99,17 +107,20 @@ private static void AddInternalRegistrations(this ContainerBuilder builder, Acti builder.RegisterType() .As() - .WithParameter(TypedParameter.From(callback)) + .WithParameter(TypedParameter.From(exceptionCallback)) + .WithParameter(TypedParameter.From(configurationCallback)) .SingleInstance(); builder.RegisterType() .As() - .WithParameter(TypedParameter.From(callback)) + .WithParameter(TypedParameter.From(exceptionCallback)) + .WithParameter(TypedParameter.From(configurationCallback)) .SingleInstance(); builder.RegisterType() .As() - .WithParameter(TypedParameter.From(callback)) + .WithParameter(TypedParameter.From(exceptionCallback)) + .WithParameter(TypedParameter.From(configurationCallback)) .SingleInstance(); builder.RegisterType() diff --git a/src/Autofac.Integration.ServiceFabric/StatefulServiceFactoryRegistration.cs b/src/Autofac.Integration.ServiceFabric/StatefulServiceFactoryRegistration.cs index 6ee4dfc..85fdd3d 100644 --- a/src/Autofac.Integration.ServiceFabric/StatefulServiceFactoryRegistration.cs +++ b/src/Autofac.Integration.ServiceFabric/StatefulServiceFactoryRegistration.cs @@ -36,10 +36,15 @@ internal sealed class StatefulServiceFactoryRegistration : IStatefulServiceFacto { internal Action ConstructorExceptionCallback { get; } + internal Action ConfigurationAction { get; } + // ReSharper disable once UnusedMember.Global - public StatefulServiceFactoryRegistration(Action constructorExceptionCallback) + public StatefulServiceFactoryRegistration( + Action constructorExceptionCallback, + Action configurationAction) { ConstructorExceptionCallback = constructorExceptionCallback; + ConfigurationAction = configurationAction; } public void RegisterStatefulServiceFactory( @@ -54,7 +59,10 @@ public void RegisterStatefulServiceFactory( builder.RegisterInstance(context) .As() .As(); + + ConfigurationAction(builder); }); + try { var service = lifetimeScope.Resolve(); diff --git a/src/Autofac.Integration.ServiceFabric/StatelessServiceFactoryRegistration.cs b/src/Autofac.Integration.ServiceFabric/StatelessServiceFactoryRegistration.cs index 3e94e47..f245b59 100644 --- a/src/Autofac.Integration.ServiceFabric/StatelessServiceFactoryRegistration.cs +++ b/src/Autofac.Integration.ServiceFabric/StatelessServiceFactoryRegistration.cs @@ -34,12 +34,17 @@ namespace Autofac.Integration.ServiceFabric [SuppressMessage("Microsoft.Performance", "CA1812", Justification = "Instantiated at runtime via dependency injection")] internal sealed class StatelessServiceFactoryRegistration : IStatelessServiceFactoryRegistration { - public Action ConstructorExceptionCallback { get; } + internal Action ConstructorExceptionCallback { get; } + + internal Action ConfigurationAction { get; } // ReSharper disable once UnusedMember.Global - public StatelessServiceFactoryRegistration(Action constructorExceptionCallback) + public StatelessServiceFactoryRegistration( + Action constructorExceptionCallback, + Action configurationAction) { ConstructorExceptionCallback = constructorExceptionCallback; + ConfigurationAction = configurationAction; } public void RegisterStatelessServiceFactory( @@ -54,7 +59,10 @@ public void RegisterStatelessServiceFactory( builder.RegisterInstance(context) .As() .As(); + + ConfigurationAction(builder); }); + try { var service = lifetimeScope.Resolve(); diff --git a/test/Autofac.Integration.ServiceFabric.Test/ServiceFabricModuleTests.cs b/test/Autofac.Integration.ServiceFabric.Test/ServiceFabricModuleTests.cs index 9d29e4d..88dbd70 100644 --- a/test/Autofac.Integration.ServiceFabric.Test/ServiceFabricModuleTests.cs +++ b/test/Autofac.Integration.ServiceFabric.Test/ServiceFabricModuleTests.cs @@ -47,6 +47,31 @@ public void ActorFactoryRegistrationReceivesDefaultConstructorExceptionCallbackP Assert.NotNull(factoryRegistration.ConstructorExceptionCallback); } + [Fact] + public void ActorFactoryRegistrationReceivesProvidedConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + var invoked = false; + builder.RegisterServiceFabricSupport(configurationAction: _ => invoked = true); + var container = builder.Build(); + + var factoryRegistration = (ActorFactoryRegistration)container.Resolve(); + factoryRegistration.ConfigurationAction(new ContainerBuilder()); + + Assert.True(invoked); + } + + [Fact] + public void ActorFactoryRegistrationReceivesDefaultConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + builder.RegisterServiceFabricSupport(); + var container = builder.Build(); + + var factoryRegistration = (ActorFactoryRegistration)container.Resolve(); + Assert.NotNull(factoryRegistration.ConfigurationAction); + } + [Fact] public void StatefulServiceFactoryRegistrationReceivesProvidedConstructorExceptionCallbackParameter() { @@ -74,6 +99,31 @@ public void StatefulServiceFactoryRegistrationReceivesDefaultConstructorExceptio Assert.NotNull(factoryRegistration.ConstructorExceptionCallback); } + [Fact] + public void StatefulServiceFactoryRegistrationReceivesProvidedConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + var invoked = false; + builder.RegisterServiceFabricSupport(configurationAction: _ => invoked = true); + var container = builder.Build(); + + var factoryRegistration = (StatefulServiceFactoryRegistration)container.Resolve(); + factoryRegistration.ConfigurationAction(new ContainerBuilder()); + + Assert.True(invoked); + } + + [Fact] + public void StatefulServiceFactoryRegistrationReceivesDefaultConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + builder.RegisterServiceFabricSupport(); + var container = builder.Build(); + + var factoryRegistration = (StatefulServiceFactoryRegistration)container.Resolve(); + Assert.NotNull(factoryRegistration.ConfigurationAction); + } + [Fact] public void StatelessServiceFactoryRegistrationReceivesProvidedConstructorExceptionCallbackParameter() { @@ -100,5 +150,30 @@ public void StatelessServiceFactoryRegistrationReceivesDefaultConstructorExcepti var factoryRegistration = (StatelessServiceFactoryRegistration)container.Resolve(); Assert.NotNull(factoryRegistration.ConstructorExceptionCallback); } + + [Fact] + public void StatelessServiceFactoryRegistrationReceivesProvidedConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + var invoked = false; + builder.RegisterServiceFabricSupport(configurationAction: _ => invoked = true); + var container = builder.Build(); + + var factoryRegistration = (StatelessServiceFactoryRegistration)container.Resolve(); + factoryRegistration.ConfigurationAction(new ContainerBuilder()); + + Assert.True(invoked); + } + + [Fact] + public void StatelessServiceFactoryRegistrationReceivesDefaultConfigurationActionParameter() + { + var builder = new ContainerBuilder(); + builder.RegisterServiceFabricSupport(); + var container = builder.Build(); + + var factoryRegistration = (StatelessServiceFactoryRegistration)container.Resolve(); + Assert.NotNull(factoryRegistration.ConfigurationAction); + } } }