Skip to content

Commit

Permalink
Merge pull request #53 from Particular/backport-adapter-fix
Browse files Browse the repository at this point in the history
Use matching lifetime for interface registrations (#51)
  • Loading branch information
danielmarbach authored Apr 2, 2020
2 parents 691b9dc + af65dd7 commit 0ece9be
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/NServiceBus.Extensions.Hosting/ServiceCollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public void ConfigureComponent(Type concreteComponent, DependencyLifecycle depen
return;
}

serviceCollection.Add(new ServiceDescriptor(concreteComponent, concreteComponent, Map(dependencyLifecycle)));
RegisterInterfaces(concreteComponent);
var serviceLifetime = Map(dependencyLifecycle);
serviceCollection.Add(new ServiceDescriptor(concreteComponent, concreteComponent, serviceLifetime));
RegisterInterfaces(concreteComponent,serviceLifetime);
}

public void ConfigureComponent<T>(DependencyLifecycle dependencyLifecycle)
Expand All @@ -37,8 +38,9 @@ public void ConfigureComponent<T>(Func<T> componentFactory, DependencyLifecycle
return;
}

serviceCollection.Add(new ServiceDescriptor(componentType, p => componentFactory(), Map(dependencyLifecycle)));
RegisterInterfaces(componentType);
var serviceLifetime = Map(dependencyLifecycle);
serviceCollection.Add(new ServiceDescriptor(componentType, p => componentFactory(), serviceLifetime));
RegisterInterfaces(componentType, serviceLifetime);
}

public void ConfigureComponent<T>(Func<IBuilder, T> componentFactory, DependencyLifecycle dependencyLifecycle)
Expand All @@ -49,8 +51,9 @@ public void ConfigureComponent<T>(Func<IBuilder, T> componentFactory, Dependency
return;
}

serviceCollection.Add(new ServiceDescriptor(componentType, p => componentFactory(new ServiceProviderAdapter(p)), Map(dependencyLifecycle)));
RegisterInterfaces(componentType);
var serviceLifetime = Map(dependencyLifecycle);
serviceCollection.Add(new ServiceDescriptor(componentType, p => componentFactory(new ServiceProviderAdapter(p)), serviceLifetime));
RegisterInterfaces(componentType, serviceLifetime);
}

public bool HasComponent<T>()
Expand All @@ -73,13 +76,13 @@ public void RegisterSingleton<T>(T instance)
RegisterSingleton(typeof(T), instance);
}

void RegisterInterfaces(Type component)
void RegisterInterfaces(Type component, ServiceLifetime serviceLifetime)
{
var interfaces = component.GetInterfaces();
foreach (var serviceType in interfaces)
{
// see https://andrewlock.net/how-to-register-a-service-with-multiple-interfaces-for-in-asp-net-core-di/
serviceCollection.Add(new ServiceDescriptor(serviceType, sp => sp.GetService(component), ServiceLifetime.Transient));
serviceCollection.Add(new ServiceDescriptor(serviceType, sp => sp.GetService(component), serviceLifetime));
}
}

Expand Down

0 comments on commit 0ece9be

Please sign in to comment.