From c3eb8c025b9cf9225385f61e97049e148914cff9 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 18 Jun 2017 09:29:34 -0700 Subject: [PATCH 1/2] Create a scoped service provider for the call to Configure - This allows scoped dependencies to be injected into the Configure method. It means you can resolve the DbContext or any other scoped service without the hassle of the CreateScope boiler plate. As a side effect, it also makes Startup.Configure a bit more testable. --- .../Internal/ConfigureBuilder.cs | 45 ++++++++++--------- .../Fakes/StartupWithScopedServices.cs | 16 +++++++ .../StartupManagerTests.cs | 30 +++++++++++++ 3 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/ConfigureBuilder.cs b/src/Microsoft.AspNetCore.Hosting/Internal/ConfigureBuilder.cs index 54c93841..37b715c5 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/ConfigureBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/ConfigureBuilder.cs @@ -21,34 +21,39 @@ public ConfigureBuilder(MethodInfo configure) private void Invoke(object instance, IApplicationBuilder builder) { - var serviceProvider = builder.ApplicationServices; - var parameterInfos = MethodInfo.GetParameters(); - var parameters = new object[parameterInfos.Length]; - for (var index = 0; index < parameterInfos.Length; index++) + // Create a scope for Configure, this allows creating scoped dependencies + // without the hassle of manually creating a scope. + using (var scope = builder.ApplicationServices.CreateScope()) { - var parameterInfo = parameterInfos[index]; - if (parameterInfo.ParameterType == typeof(IApplicationBuilder)) + var serviceProvider = scope.ServiceProvider; + var parameterInfos = MethodInfo.GetParameters(); + var parameters = new object[parameterInfos.Length]; + for (var index = 0; index < parameterInfos.Length; index++) { - parameters[index] = builder; - } - else - { - try + var parameterInfo = parameterInfos[index]; + if (parameterInfo.ParameterType == typeof(IApplicationBuilder)) { - parameters[index] = serviceProvider.GetRequiredService(parameterInfo.ParameterType); + parameters[index] = builder; } - catch (Exception ex) + else { - throw new Exception(string.Format( - "Could not resolve a service of type '{0}' for the parameter '{1}' of method '{2}' on type '{3}'.", - parameterInfo.ParameterType.FullName, - parameterInfo.Name, - MethodInfo.Name, - MethodInfo.DeclaringType.FullName), ex); + try + { + parameters[index] = serviceProvider.GetRequiredService(parameterInfo.ParameterType); + } + catch (Exception ex) + { + throw new Exception(string.Format( + "Could not resolve a service of type '{0}' for the parameter '{1}' of method '{2}' on type '{3}'.", + parameterInfo.ParameterType.FullName, + parameterInfo.Name, + MethodInfo.Name, + MethodInfo.DeclaringType.FullName), ex); + } } } + MethodInfo.Invoke(instance, parameters); } - MethodInfo.Invoke(instance, parameters); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs b/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs new file mode 100644 index 00000000..df330cd4 --- /dev/null +++ b/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.AspNetCore.Builder; +using static Microsoft.AspNetCore.Hosting.Tests.StartupManagerTests; + +namespace Microsoft.AspNetCore.Hosting.Tests.Fakes +{ + public class StartupWithScopedServices + { + public void Configure(IApplicationBuilder builder, DisposableService disposable) + { + + } + } +} diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs index 31ba1c90..045893e2 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs @@ -36,6 +36,26 @@ public void StartupClassMayHaveHostingServicesInjected() Assert.Equal(2, callbackStartup.MethodsCalled); } + [Fact] + public void StartupClassMayHaveScopedServicesInjected() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddSingleton>(new DefaultServiceProviderFactory(new ServiceProviderOptions + { + ValidateScopes = true + })); + + serviceCollection.AddScoped(); + var services = serviceCollection.BuildServiceProvider(); + + var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithScopedServices"); + var startup = StartupLoader.LoadMethods(services, type, "WithScopedServices"); + + var app = new ApplicationBuilder(services); + app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); + startup.ConfigureDelegate(app); + } + [Theory] [InlineData(null)] [InlineData("Dev")] @@ -405,5 +425,15 @@ public void ConfigurationMethodCalled(object instance) _configurationMethodCalledList.Add(instance); } } + + public class DisposableService : IDisposable + { + public bool Disposed { get; set; } + + public void Dispose() + { + Disposed = true; + } + } } } From 391ee991ca2fbad69a64e3dec17d4be0c6403d87 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 18 Jun 2017 09:56:07 -0700 Subject: [PATCH 2/2] Assert DisposableService is disposed --- src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs | 2 +- src/Microsoft.AspNetCore.Hosting/Internal/StartupMethods.cs | 4 +++- .../Fakes/StartupWithScopedServices.cs | 6 ++++-- .../StartupManagerTests.cs | 5 +++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs b/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs index ba8da1d0..cc65dd30 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs @@ -60,7 +60,7 @@ public static StartupMethods LoadMethods(IServiceProvider hostingServiceProvider return applicationServiceProvider ?? services.BuildServiceProvider(); }; - return new StartupMethods(configureMethod.Build(instance), configureServices); + return new StartupMethods(instance, configureMethod.Build(instance), configureServices); } public static Type FindStartupType(string startupAssemblyName, string environmentName) diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/StartupMethods.cs b/src/Microsoft.AspNetCore.Hosting/Internal/StartupMethods.cs index 5e5e4888..f854c859 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/StartupMethods.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/StartupMethods.cs @@ -10,15 +10,17 @@ namespace Microsoft.AspNetCore.Hosting.Internal { public class StartupMethods { - public StartupMethods(Action configure, Func configureServices) + public StartupMethods(object instance, Action configure, Func configureServices) { Debug.Assert(configure != null); Debug.Assert(configureServices != null); + StartupInstance = instance; ConfigureDelegate = configure; ConfigureServicesDelegate = configureServices; } + public object StartupInstance { get; } public Func ConfigureServicesDelegate { get; } public Action ConfigureDelegate { get; } diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs b/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs index df330cd4..985f9204 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/Fakes/StartupWithScopedServices.cs @@ -4,13 +4,15 @@ using Microsoft.AspNetCore.Builder; using static Microsoft.AspNetCore.Hosting.Tests.StartupManagerTests; -namespace Microsoft.AspNetCore.Hosting.Tests.Fakes +namespace Microsoft.AspNetCore.Hosting.Fakes { public class StartupWithScopedServices { + public DisposableService DisposableService { get; set; } + public void Configure(IApplicationBuilder builder, DisposableService disposable) { - + DisposableService = disposable; } } } diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs index 045893e2..fc655799 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/StartupManagerTests.cs @@ -50,10 +50,15 @@ public void StartupClassMayHaveScopedServicesInjected() var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithScopedServices"); var startup = StartupLoader.LoadMethods(services, type, "WithScopedServices"); + Assert.NotNull(startup.StartupInstance); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); + + var instance = (StartupWithScopedServices)startup.StartupInstance; + Assert.NotNull(instance.DisposableService); + Assert.True(instance.DisposableService.Disposed); } [Theory]