Skip to content

Commit

Permalink
Fix #57 - Provide hook to add registrations when lifetime scope for s…
Browse files Browse the repository at this point in the history
…ervice is being created
  • Loading branch information
alexmg committed Oct 5, 2020
1 parent bc4f4e1 commit c15268f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ internal sealed class ActorFactoryRegistration : IActorFactoryRegistration
{
internal Action<Exception> ConstructorExceptionCallback { get; }

internal Action<ContainerBuilder> ConfigurationAction { get; }

// ReSharper disable once UnusedMember.Global
public ActorFactoryRegistration(Action<Exception> constructorExceptionCallback)
public ActorFactoryRegistration(
Action<Exception> constructorExceptionCallback,
Action<ContainerBuilder> configurationAction)
{
ConstructorExceptionCallback = constructorExceptionCallback;
ConfigurationAction = configurationAction;
}

public void RegisterActorFactory<TActor>(
Expand All @@ -66,7 +71,10 @@ ActorBase ActorFactory(ActorService actorService, ActorId actorId)
.As<ActorService>();
builder.RegisterInstance(actorId)
.As<ActorId>();

ConfigurationAction(builder);
});

try
{
var actor = lifetimeScope.Resolve<TActor>();
Expand Down
27 changes: 19 additions & 8 deletions src/Autofac.Integration.ServiceFabric/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ public static class RegistrationExtensions
/// Adds the core services required by the Service Fabric integration.
/// </summary>
/// <param name="builder">The container builder to register the services with.</param>
/// <param name="constructorExceptionCallback">Callback will be invoked if there are an exception thrown during resolving.</param>
public static void RegisterServiceFabricSupport(this ContainerBuilder builder, Action<Exception> constructorExceptionCallback = null)
/// <param name="constructorExceptionCallback">Callback will be invoked if an exception is thrown during resolving.</param>
/// <param name="configurationAction">Callback will be invoked while configuring the lifetime scope for a service.</param>
public static void RegisterServiceFabricSupport(
this ContainerBuilder builder,
Action<Exception> constructorExceptionCallback = null,
Action<ContainerBuilder> 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);
}
Expand Down Expand Up @@ -87,9 +91,13 @@ internal static IRegistrationBuilder<TService, ConcreteReflectionActivatorData,
});
}

private static void AddInternalRegistrations(this ContainerBuilder builder, Action<Exception> constructorExceptionCallback = null)
private static void AddInternalRegistrations(
this ContainerBuilder builder,
Action<Exception> constructorExceptionCallback = null,
Action<ContainerBuilder> configurationAction = null)
{
var callback = constructorExceptionCallback ?? (ex => { });
var exceptionCallback = constructorExceptionCallback ?? (ex => { });
var configurationCallback = configurationAction ?? (_ => { });

builder.RegisterType<ActorInterceptor>()
.InstancePerLifetimeScope();
Expand All @@ -99,17 +107,20 @@ private static void AddInternalRegistrations(this ContainerBuilder builder, Acti

builder.RegisterType<ActorFactoryRegistration>()
.As<IActorFactoryRegistration>()
.WithParameter(TypedParameter.From(callback))
.WithParameter(TypedParameter.From(exceptionCallback))
.WithParameter(TypedParameter.From(configurationCallback))
.SingleInstance();

builder.RegisterType<StatelessServiceFactoryRegistration>()
.As<IStatelessServiceFactoryRegistration>()
.WithParameter(TypedParameter.From(callback))
.WithParameter(TypedParameter.From(exceptionCallback))
.WithParameter(TypedParameter.From(configurationCallback))
.SingleInstance();

builder.RegisterType<StatefulServiceFactoryRegistration>()
.As<IStatefulServiceFactoryRegistration>()
.WithParameter(TypedParameter.From(callback))
.WithParameter(TypedParameter.From(exceptionCallback))
.WithParameter(TypedParameter.From(configurationCallback))
.SingleInstance();

builder.RegisterType<ActorService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ internal sealed class StatefulServiceFactoryRegistration : IStatefulServiceFacto
{
internal Action<Exception> ConstructorExceptionCallback { get; }

internal Action<ContainerBuilder> ConfigurationAction { get; }

// ReSharper disable once UnusedMember.Global
public StatefulServiceFactoryRegistration(Action<Exception> constructorExceptionCallback)
public StatefulServiceFactoryRegistration(
Action<Exception> constructorExceptionCallback,
Action<ContainerBuilder> configurationAction)
{
ConstructorExceptionCallback = constructorExceptionCallback;
ConfigurationAction = configurationAction;
}

public void RegisterStatefulServiceFactory<TService>(
Expand All @@ -54,7 +59,10 @@ public void RegisterStatefulServiceFactory<TService>(
builder.RegisterInstance(context)
.As<StatefulServiceContext>()
.As<ServiceContext>();

ConfigurationAction(builder);
});

try
{
var service = lifetimeScope.Resolve<TService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception> ConstructorExceptionCallback { get; }
internal Action<Exception> ConstructorExceptionCallback { get; }

internal Action<ContainerBuilder> ConfigurationAction { get; }

// ReSharper disable once UnusedMember.Global
public StatelessServiceFactoryRegistration(Action<Exception> constructorExceptionCallback)
public StatelessServiceFactoryRegistration(
Action<Exception> constructorExceptionCallback,
Action<ContainerBuilder> configurationAction)
{
ConstructorExceptionCallback = constructorExceptionCallback;
ConfigurationAction = configurationAction;
}

public void RegisterStatelessServiceFactory<TService>(
Expand All @@ -54,7 +59,10 @@ public void RegisterStatelessServiceFactory<TService>(
builder.RegisterInstance(context)
.As<StatelessServiceContext>()
.As<ServiceContext>();

ConfigurationAction(builder);
});

try
{
var service = lifetimeScope.Resolve<TService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActorFactoryRegistration>();
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<IActorFactoryRegistration>();
Assert.NotNull(factoryRegistration.ConfigurationAction);
}

[Fact]
public void StatefulServiceFactoryRegistrationReceivesProvidedConstructorExceptionCallbackParameter()
{
Expand Down Expand Up @@ -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<IStatefulServiceFactoryRegistration>();
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<IStatefulServiceFactoryRegistration>();
Assert.NotNull(factoryRegistration.ConfigurationAction);
}

[Fact]
public void StatelessServiceFactoryRegistrationReceivesProvidedConstructorExceptionCallbackParameter()
{
Expand All @@ -100,5 +150,30 @@ public void StatelessServiceFactoryRegistrationReceivesDefaultConstructorExcepti
var factoryRegistration = (StatelessServiceFactoryRegistration)container.Resolve<IStatelessServiceFactoryRegistration>();
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<IStatelessServiceFactoryRegistration>();
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<IStatelessServiceFactoryRegistration>();
Assert.NotNull(factoryRegistration.ConfigurationAction);
}
}
}

0 comments on commit c15268f

Please sign in to comment.