From 0ab560e08695e709fc650eb31026c8c0b2a44b31 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 16 Jun 2014 18:30:51 -0700 Subject: [PATCH] Add UseServices overload that takes a Func returning IServiceProvider - This should be useful for third-party IoC containers --- .../ContainerExtensions.cs | 12 +++++- .../Microsoft.AspNet.Hosting.Tests.kproj | 1 + .../UseServicesFacts.cs | 43 +++++++++++++++++++ .../project.json | 1 + 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs diff --git a/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs b/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs index 5e60f67127..45b7375683 100644 --- a/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs +++ b/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs @@ -63,12 +63,20 @@ namespace Microsoft.AspNet.Builder } public static IBuilder UseServices(this IBuilder builder, Action configureServices) + { + return builder.UseServices(serviceCollection => + { + configureServices(serviceCollection); + return serviceCollection.BuildServiceProvider(builder.ApplicationServices); + }); + } + + public static IBuilder UseServices(this IBuilder builder, Func configureServices) { var serviceCollection = new ServiceCollection(); serviceCollection.Add(OptionsServices.GetDefaultServices()); - configureServices(serviceCollection); - builder.ApplicationServices = serviceCollection.BuildServiceProvider(builder.ApplicationServices); + builder.ApplicationServices = configureServices(serviceCollection); return builder.UseMiddleware(typeof(ContainerMiddleware)); } diff --git a/test/Microsoft.AspNet.Hosting.Tests/Microsoft.AspNet.Hosting.Tests.kproj b/test/Microsoft.AspNet.Hosting.Tests/Microsoft.AspNet.Hosting.Tests.kproj index 0ffb62252d..412181c036 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/Microsoft.AspNet.Hosting.Tests.kproj +++ b/test/Microsoft.AspNet.Hosting.Tests/Microsoft.AspNet.Hosting.Tests.kproj @@ -26,6 +26,7 @@ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs b/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs new file mode 100644 index 0000000000..df88e1e855 --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs @@ -0,0 +1,43 @@ +using System; +using Microsoft.AspNet.Builder; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; +using Xunit; + +namespace Microsoft.AspNet.Hosting.Tests +{ + public class UseServicesFacts + { + [Fact] + public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithAction() + { + var baseServiceProvider = new ServiceCollection().BuildServiceProvider(); + var builder = new Microsoft.AspNet.Builder.Builder(baseServiceProvider); + + builder.UseServices(serviceCollection => { }); + + var optionsAccessor = builder.ApplicationServices.GetService>(); + Assert.NotNull(optionsAccessor); + } + + + [Fact] + public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithFunc() + { + var baseServiceProvider = new ServiceCollection().BuildServiceProvider(); + var builder = new Microsoft.AspNet.Builder.Builder(baseServiceProvider); + IServiceProvider serviceProvider = null; + + builder.UseServices(serviceCollection => + { + serviceProvider = serviceCollection.BuildServiceProvider(builder.ApplicationServices); + return serviceProvider; + }); + + Assert.Same(serviceProvider, builder.ApplicationServices); + var optionsAccessor = builder.ApplicationServices.GetService>(); + Assert.NotNull(optionsAccessor); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/project.json b/test/Microsoft.AspNet.Hosting.Tests/project.json index 50ffba28ed..7ad9bce822 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/project.json +++ b/test/Microsoft.AspNet.Hosting.Tests/project.json @@ -2,6 +2,7 @@ "version": "0.1-alpha-*", "dependencies": { "Microsoft.AspNet.Hosting": "", + "Microsoft.AspNet.RequestContainer": "", "xunit.abstractions": "2.0.0-aspnet-*", "xunit.assert": "2.0.0-aspnet-*", "xunit.core": "2.0.0-aspnet-*",