Skip to content

Commit 7b80a00

Browse files
committed
Added AddControllerActivation overload that specifies the Lifestyle. Fixes #8.
1 parent aa551b1 commit 7b80a00

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/SimpleInjector.Integration.AspNetCore.Mvc.Core/SimpleInjectorAspNetCoreBuilderMvcCoreExtensions.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public static class SimpleInjectorAspNetCoreBuilderMvcCoreExtensions
2323
{
2424
/// <summary>
2525
/// Registers all application's controllers in Simple Injector and instructs ASP.NET Core to let
26-
/// Simple Injector create those controllers.
26+
/// Simple Injector create those controllers. Those controllers will be registered using the
27+
/// <see cref="Lifestyle.Transient"/> lifestyle, unless this is overridden using a custom
28+
/// <see cref="ContainerOptions.DefaultLifestyle">DefaultLifestyle</see> or by overriding the default
29+
/// <see cref="ContainerOptions.LifestyleSelectionBehavior">LifestyleSelectionBehavior</see>.
2730
/// </summary>
2831
/// <param name="builder">The builder instance.</param>
2932
/// <returns>The supplied <paramref name="builder"/> instance.</returns>
@@ -32,16 +35,41 @@ public static SimpleInjectorAspNetCoreBuilder AddControllerActivation(
3235
{
3336
Requires.IsNotNull(builder, nameof(builder));
3437

38+
AddControllerActivationInternal(builder, lifestyle: null);
39+
40+
return builder;
41+
}
42+
43+
/// <summary>
44+
/// Registers all application's controllers in Simple Injector and instructs ASP.NET Core to let
45+
/// Simple Injector create those controllers. Those controllers will be registered using the supplied
46+
/// <paramref name="lifestyle"/>.
47+
/// </summary>
48+
/// <param name="builder">The builder instance.</param>
49+
/// <param name="lifestyle">The lifestyle used for registering the controllers.</param>
50+
/// <returns>The supplied <paramref name="builder"/> instance.</returns>
51+
public static SimpleInjectorAspNetCoreBuilder AddControllerActivation(
52+
this SimpleInjectorAspNetCoreBuilder builder, Lifestyle lifestyle)
53+
{
54+
Requires.IsNotNull(builder, nameof(builder));
55+
Requires.IsNotNull(lifestyle, nameof(lifestyle));
56+
57+
AddControllerActivationInternal(builder, lifestyle);
58+
59+
return builder;
60+
}
61+
62+
private static void AddControllerActivationInternal(
63+
SimpleInjectorAspNetCoreBuilder builder, Lifestyle? lifestyle)
64+
{
3565
ApplicationPartManager manager = GetApplicationPartManager(
3666
builder.Services,
3767
nameof(AddControllerActivation));
3868

39-
RegisterMvcControllers(builder.Container, manager);
69+
RegisterMvcControllers(builder.Container, manager, lifestyle);
4070

4171
builder.Services.AddSingleton<IControllerActivator>(
4272
new SimpleInjectorControllerActivator(builder.Container));
43-
44-
return builder;
4573
}
4674

4775
private static ApplicationPartManager GetApplicationPartManager(
@@ -85,25 +113,27 @@ private static ApplicationPartManager GetApplicationPartManager(
85113
}
86114
}
87115

88-
private static void RegisterMvcControllers(Container container, ApplicationPartManager manager)
116+
private static void RegisterMvcControllers(
117+
Container container, ApplicationPartManager manager, Lifestyle? lifestyle)
89118
{
90119
var feature = new ControllerFeature();
91120
manager.PopulateFeature(feature);
92121
var controllerTypes = feature.Controllers.Select(t => t.AsType());
93122

94-
RegisterControllerTypes(container, controllerTypes);
123+
RegisterControllerTypes(container, controllerTypes, lifestyle);
95124
}
96125

97-
private static void RegisterControllerTypes(this Container container, IEnumerable<Type> types)
126+
private static void RegisterControllerTypes(
127+
this Container container, IEnumerable<Type> types, Lifestyle? lifestyle)
98128
{
99129
foreach (Type type in types.ToArray())
100130
{
101-
var registration = CreateConcreteRegistration(container, type);
131+
var registration = CreateConcreteRegistration(container, type, lifestyle);
102132

103133
// Microsoft.AspNetCore.Mvc.Controller implements IDisposable (which is a design flaw).
104134
// This will cause false positives in Simple Injector's diagnostic services, so we suppress
105135
// this warning in case the registered type doesn't override Dispose from Controller.
106-
if (ShouldSuppressDisposingControllers(type))
136+
if (registration.Lifestyle == Lifestyle.Transient && ShouldSuppressDisposingControllers(type))
107137
{
108138
registration.SuppressDiagnosticWarning(
109139
DiagnosticType.DisposableTransientComponent,
@@ -162,9 +192,9 @@ private static bool ShouldSuppressDisposingControllers(Type type)
162192
return null;
163193
}
164194

165-
private static Registration CreateConcreteRegistration(Container container, Type concreteType) =>
166-
container.Options.LifestyleSelectionBehavior
167-
.SelectLifestyle(concreteType)
195+
private static Registration CreateConcreteRegistration(
196+
Container container, Type concreteType, Lifestyle? lifestyle) =>
197+
(lifestyle ?? container.Options.LifestyleSelectionBehavior.SelectLifestyle(concreteType))
168198
.CreateRegistration(concreteType, container);
169199
}
170200
}

0 commit comments

Comments
 (0)