From a9827a43105f97ec8ff6e1c4aa6f3f1f59d59b1e Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 24 Nov 2014 17:33:11 -0800 Subject: [PATCH] GetDefaultServices -> AddHosting Also stop adding options --- .../HostingServices.cs | 42 +-------------- .../HostingServicesCollectionExtensions.cs | 53 +++++++++++++++++++ .../Startup/StartupLoader.cs | 6 +-- .../ContainerExtensions.cs | 6 +-- .../Fakes/Startup.cs | 11 ++-- .../StartupManagerTests.cs | 3 +- .../UseServicesFacts.cs | 45 ---------------- 7 files changed, 67 insertions(+), 99 deletions(-) create mode 100644 src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs delete mode 100644 test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs diff --git a/src/Microsoft.AspNet.Hosting/HostingServices.cs b/src/Microsoft.AspNet.Hosting/HostingServices.cs index e2372db87d..3ff28e403f 100644 --- a/src/Microsoft.AspNet.Hosting/HostingServices.cs +++ b/src/Microsoft.AspNet.Hosting/HostingServices.cs @@ -4,14 +4,10 @@ using System; using System.Collections.Generic; using System.Linq; -using Microsoft.AspNet.Hosting.Builder; -using Microsoft.AspNet.Hosting.Server; -using Microsoft.AspNet.Hosting.Startup; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.ServiceLookup; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.Runtime.Infrastructure; namespace Microsoft.AspNet.Hosting @@ -24,7 +20,6 @@ namespace Microsoft.AspNet.Hosting var manifest = fallbackProvider.GetRequiredService(); foreach (var service in manifest.Services) { - // REVIEW: should this be Singleton instead? services.AddTransient(service, sp => fallbackProvider.GetService(service)); } return services; @@ -39,45 +34,12 @@ namespace Microsoft.AspNet.Hosting { configuration = configuration ?? new Configuration(); var services = Import(fallbackServices); - services.Add(GetDefaultServices(configuration)); + services.AddHosting(configuration); services.AddSingleton(sp => new HostingManifest(fallbackServices)); - services.AddInstance(new ConfigureHostingEnvironment(configuration)); return services; } - // REVIEW: make this private? - public static IEnumerable GetDefaultServices(IConfiguration configuration = null) - { - configuration = configuration ?? new Configuration(); - var describer = new ServiceDescriber(configuration); - - yield return describer.Transient(); - yield return describer.Transient(); - - yield return describer.Transient(); - yield return describer.Transient(); - - yield return describer.Transient(); - yield return describer.Transient(); - - yield return describer.Instance(new ApplicationLifetime()); - - // These three services as exported in the manifest - yield return describer.Singleton(); - yield return describer.Singleton(); - // TODO: Do we expect this to be provide by the runtime eventually? - yield return describer.Singleton(); - - // TODO: Remove the below services and push the responsibility to frameworks to add - - yield return describer.Scoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>)); - - foreach (var service in OptionsServices.GetDefaultServices()) - { - yield return service; - } - } - + // Manifest exposes the fallback manifest in addition to ITypeActivator, IHostingEnvironment, and ILoggerFactory private class HostingManifest : IServiceManifest { public HostingManifest(IServiceProvider fallback) diff --git a/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs new file mode 100644 index 0000000000..ac661999b2 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Hosting; +using Microsoft.AspNet.Hosting.Builder; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Hosting.Startup; +using Microsoft.Framework.ConfigurationModel; +using Microsoft.Framework.Logging; + +namespace Microsoft.Framework.DependencyInjection +{ + public static class HostingServicesExtensions + { + // REVIEW: Logging doesn't depend on DI, where should this live? + public static IServiceCollection AddLogging(this IServiceCollection services, IConfiguration config = null) + { + var describe = new ServiceDescriber(config); + services.TryAdd(describe.Singleton()); + return services; + } + + public static IServiceCollection AddHosting(this IServiceCollection services, IConfiguration configuration = null) + { + var describer = new ServiceDescriber(configuration); + + services.TryAdd(describer.Transient()); + services.TryAdd(describer.Transient()); + + services.TryAdd(describer.Transient()); + services.TryAdd(describer.Transient()); + + services.TryAdd(describer.Transient()); + services.TryAdd(describer.Transient()); + + services.TryAdd(describer.Instance(new ApplicationLifetime())); + + services.AddTypeActivator(configuration); + // TODO: Do we expect this to be provide by the runtime eventually? + services.AddLogging(configuration); + // REVIEW: okay to use existing hosting environment/httpcontext if specified? + services.TryAdd(describer.Singleton()); + + // TODO: Remove this once we have IHttpContextAccessor + services.AddContextAccessor(configuration); + + // REVIEW: don't try add because we pull out IEnumerable? + services.AddInstance(new ConfigureHostingEnvironment(configuration)); + + return services; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs index cf41930701..c76153e9d0 100644 --- a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs @@ -152,10 +152,8 @@ namespace Microsoft.AspNet.Hosting.Startup if (servicesMethod != null) { var services = HostingServices.Create(builder.ApplicationServices); - // TODO: remove adding options - services.Add(OptionsServices.GetDefaultServices()); - services.AddScoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>)); - + // TODO: remove this once IHttpContextAccessor service is added + services.AddContextAccessor(); if (servicesMethod.ReturnType == typeof(IServiceProvider)) { // IServiceProvider ConfigureServices(IServiceCollection) diff --git a/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs b/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs index 31c6f51b09..9813e9164f 100644 --- a/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs +++ b/src/Microsoft.AspNet.RequestContainer/ContainerExtensions.cs @@ -7,7 +7,6 @@ using Microsoft.AspNet.RequestContainer; using Microsoft.AspNet.Hosting; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -49,9 +48,8 @@ namespace Microsoft.AspNet.Builder // Import services from hosting/KRE as fallback var serviceCollection = HostingServices.Create(builder.ApplicationServices); - // TODO: should remove OptionServices here soon... - serviceCollection.Add(OptionsServices.GetDefaultServices()); - serviceCollection.AddScoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>)); + // TODO: remove this once IHttpContextAccessor service is added + serviceCollection.AddContextAccessor(); // REVIEW: serviceCollection has the merged services, manifests are lost after this builder.ApplicationServices = configureServices(serviceCollection); diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs index 5a85b82357..e657841566 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs @@ -17,11 +17,13 @@ namespace Microsoft.AspNet.Hosting.Fakes public void ConfigureServices(IServiceCollection services) { + services.AddOptions(); services.Configure(o => o.Configured = true); } public void ConfigureDevServices(IServiceCollection services) { + services.AddOptions(); services.Configure(o => { o.Configured = true; @@ -31,6 +33,7 @@ namespace Microsoft.AspNet.Hosting.Fakes public void ConfigureRetailServices(IServiceCollection services) { + services.AddOptions(); services.Configure(o => { o.Configured = true; @@ -40,6 +43,7 @@ namespace Microsoft.AspNet.Hosting.Fakes public static void ConfigureStaticServices(IServiceCollection services) { + services.AddOptions(); services.Configure(o => { o.Configured = true; @@ -49,8 +53,7 @@ namespace Microsoft.AspNet.Hosting.Fakes public static IServiceProvider ConfigureStaticProviderServices() { - var services = new ServiceCollection(); - services.Add(OptionsServices.GetDefaultServices()); + var services = new ServiceCollection().AddOptions(); services.Configure(o => { o.Configured = true; @@ -71,6 +74,7 @@ namespace Microsoft.AspNet.Hosting.Fakes public IServiceProvider ConfigureProviderServices(IServiceCollection services) { + services.AddOptions(); services.Configure(o => { o.Configured = true; @@ -81,8 +85,7 @@ namespace Microsoft.AspNet.Hosting.Fakes public IServiceProvider ConfigureProviderArgsServices(IApplicationBuilder me) { - var services = new ServiceCollection(); - services.Add(OptionsServices.GetDefaultServices()); + var services = new ServiceCollection().AddOptions(); services.Configure(o => { o.Configured = true; diff --git a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs index a1e083864c..10016cdd1b 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs @@ -21,8 +21,7 @@ namespace Microsoft.AspNet.Hosting.Tests [Fact] public void StartupClassMayHaveHostingServicesInjected() { - var serviceCollection = new ServiceCollection(); - serviceCollection.Add(HostingServices.GetDefaultServices()); + var serviceCollection = new ServiceCollection().AddHosting(); serviceCollection.AddInstance(this); var services = serviceCollection.BuildServiceProvider(); diff --git a/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs b/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs deleted file mode 100644 index 54b9b50eeb..0000000000 --- a/test/Microsoft.AspNet.Hosting.Tests/UseServicesFacts.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Builder; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.OptionsModel; -using Microsoft.Framework.Runtime.Infrastructure; -using Xunit; - -namespace Microsoft.AspNet.Hosting.Tests -{ - public class UseServicesFacts - { - [Fact] - public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithAction() - { - var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); - - builder.UseServices(serviceCollection => { }); - - var optionsAccessor = builder.ApplicationServices.GetRequiredService>(); - Assert.NotNull(optionsAccessor); - } - - - [Fact] - public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithFunc() - { - var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); - IServiceProvider serviceProvider = null; - - builder.UseServices(serviceCollection => - { - serviceProvider = serviceCollection.BuildServiceProvider(); - return serviceProvider; - }); - - Assert.Same(serviceProvider, builder.ApplicationServices); - var optionsAccessor = builder.ApplicationServices.GetRequiredService>(); - Assert.NotNull(optionsAccessor); - } - } -} \ No newline at end of file