Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change the way we resolve consumers when using Autofac. We now use a …

…lifetime scope and resolve the consumers within a new child scope, this ensures that any consumers resolved are then released properly once this child scope is disposed.
  • Loading branch information...
commit 5dea5ebe24fa1d7e403423d5f5d6b3b892e3736d 1 parent 95622ab
@stefansedich stefansedich authored phatboyg committed
View
22 src/Containers/MassTransit.AutoFacIntegration/AutofacConsumerFactory.cs
@@ -22,23 +22,29 @@ public class AutofacConsumerFactory<T> :
IConsumerFactory<T>
where T : class
{
- readonly IComponentContext _container;
+ readonly ILifetimeScope _scope;
- public AutofacConsumerFactory(IComponentContext container)
+ public AutofacConsumerFactory(ILifetimeScope scope)
{
- _container = container;
+ _scope = scope;
}
public IEnumerable<Action<IConsumeContext<TMessage>>> GetConsumer<TMessage>(
IConsumeContext<TMessage> context, InstanceHandlerSelector<T, TMessage> selector)
where TMessage : class
{
- var consumer = _container.Resolve<T>();
- if (consumer == null)
- throw new ConfigurationException(string.Format("Unable to resolve type '{0}' from container: ",
- typeof (T)));
+ using (var innerScope = _scope.BeginLifetimeScope())
+ {
+ var consumer = innerScope.Resolve<T>();
+ if (consumer == null)
+ throw new ConfigurationException(string.Format("Unable to resolve type '{0}' from container: ",
+ typeof (T)));
- return selector(consumer, context);
+ foreach (var handler in selector(consumer, context))
+ {
+ yield return handler;
+ }
+ }
}
}
}
View
8 src/Containers/MassTransit.AutoFacIntegration/AutofacConsumerFactoryConfigurator.cs
@@ -21,11 +21,11 @@ namespace MassTransit.AutofacIntegration
public class AutofacConsumerFactoryConfigurator
{
readonly SubscriptionBusServiceConfigurator _configurator;
- readonly IComponentContext _container;
+ readonly ILifetimeScope _scope;
- public AutofacConsumerFactoryConfigurator(SubscriptionBusServiceConfigurator configurator, IComponentContext container)
+ public AutofacConsumerFactoryConfigurator(SubscriptionBusServiceConfigurator configurator, ILifetimeScope scope)
{
- _container = container;
+ _scope = scope;
_configurator = configurator;
}
@@ -38,7 +38,7 @@ public void ConfigureConsumer(Type messageType)
public void Configure<T>()
where T : class
{
- _configurator.Consumer(new AutofacConsumerFactory<T>(_container));
+ _configurator.Consumer(new AutofacConsumerFactory<T>(_scope));
}
}
}
View
22 src/Containers/MassTransit.AutoFacIntegration/AutofacExtensions.cs
@@ -25,12 +25,12 @@ namespace MassTransit
public static class AutofacExtensions
{
- public static void LoadFrom(this SubscriptionBusServiceConfigurator configurator, IComponentContext container)
+ public static void LoadFrom(this SubscriptionBusServiceConfigurator configurator, ILifetimeScope scope)
{
- IList<Type> concreteTypes = FindTypes<IConsumer>(container, r => !r.Implements<ISaga>());
+ IList<Type> concreteTypes = FindTypes<IConsumer>(scope, r => !r.Implements<ISaga>());
if (concreteTypes.Count != 0)
{
- var consumerConfigurator = new AutofacConsumerFactoryConfigurator(configurator, container);
+ var consumerConfigurator = new AutofacConsumerFactoryConfigurator(configurator, scope);
foreach (Type concreteType in concreteTypes)
{
@@ -38,10 +38,10 @@ public static void LoadFrom(this SubscriptionBusServiceConfigurator configurator
}
}
- IList<Type> sagaTypes = FindTypes<ISaga>(container, x => true);
+ IList<Type> sagaTypes = FindTypes<ISaga>(scope, x => true);
if (sagaTypes.Count > 0)
{
- var sagaConfigurator = new AutofacSagaRepositoryFactoryConfigurator(configurator, container);
+ var sagaConfigurator = new AutofacSagaRepositoryFactoryConfigurator(configurator, scope);
foreach (Type type in sagaTypes)
{
@@ -51,24 +51,24 @@ public static void LoadFrom(this SubscriptionBusServiceConfigurator configurator
}
public static ConsumerSubscriptionConfigurator<TConsumer> Consumer<TConsumer>(
- this SubscriptionBusServiceConfigurator configurator, IComponentContext kernel)
+ this SubscriptionBusServiceConfigurator configurator, ILifetimeScope scope)
where TConsumer : class
{
- var consumerFactory = new AutofacConsumerFactory<TConsumer>(kernel);
+ var consumerFactory = new AutofacConsumerFactory<TConsumer>(scope);
return configurator.Consumer(consumerFactory);
}
public static SagaSubscriptionConfigurator<TSaga> Saga<TSaga>(
- this SubscriptionBusServiceConfigurator configurator, IComponentContext kernel)
+ this SubscriptionBusServiceConfigurator configurator, ILifetimeScope scope)
where TSaga : class, ISaga
{
- return configurator.Saga(kernel.Resolve<ISagaRepository<TSaga>>());
+ return configurator.Saga(scope.Resolve<ISagaRepository<TSaga>>());
}
- static IList<Type> FindTypes<T>(IComponentContext container, Func<Type, bool> filter)
+ static IList<Type> FindTypes<T>(ILifetimeScope scope, Func<Type, bool> filter)
{
- return container.ComponentRegistry.Registrations
+ return scope.ComponentRegistry.Registrations
.SelectMany(r => r.Services.OfType<IServiceWithType>(), (r, s) => new {r, s})
.Where(rs => rs.s.ServiceType.Implements<T>())
.Select(rs => rs.r.Activator.LimitType)
View
8 src/Containers/MassTransit.AutoFacIntegration/AutofacSagaRepositoryFactoryConfigurator.cs
@@ -22,11 +22,11 @@ namespace MassTransit.AutofacIntegration
public class AutofacSagaRepositoryFactoryConfigurator
{
readonly SubscriptionBusServiceConfigurator _configurator;
- readonly IComponentContext _container;
+ readonly ILifetimeScope _scope;
- public AutofacSagaRepositoryFactoryConfigurator(SubscriptionBusServiceConfigurator configurator, IComponentContext container)
+ public AutofacSagaRepositoryFactoryConfigurator(SubscriptionBusServiceConfigurator configurator, ILifetimeScope scope)
{
- _container = container;
+ _scope = scope;
_configurator = configurator;
}
@@ -39,7 +39,7 @@ public void ConfigureSaga(Type sagaType)
public void Configure<T>()
where T : class, ISaga
{
- _configurator.Saga(_container.Resolve<ISagaRepository<T>>());
+ _configurator.Saga(_scope.Resolve<ISagaRepository<T>>());
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.