GetDefaultServices -> AddHosting
Also stop adding options
This commit is contained in:
parent
aebfecdf87
commit
a9827a4310
|
|
@ -4,14 +4,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
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.ConfigurationModel;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.DependencyInjection.ServiceLookup;
|
using Microsoft.Framework.DependencyInjection.ServiceLookup;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
using Microsoft.Framework.Runtime.Infrastructure;
|
using Microsoft.Framework.Runtime.Infrastructure;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Hosting
|
namespace Microsoft.AspNet.Hosting
|
||||||
|
|
@ -24,7 +20,6 @@ namespace Microsoft.AspNet.Hosting
|
||||||
var manifest = fallbackProvider.GetRequiredService<IServiceManifest>();
|
var manifest = fallbackProvider.GetRequiredService<IServiceManifest>();
|
||||||
foreach (var service in manifest.Services)
|
foreach (var service in manifest.Services)
|
||||||
{
|
{
|
||||||
// REVIEW: should this be Singleton instead?
|
|
||||||
services.AddTransient(service, sp => fallbackProvider.GetService(service));
|
services.AddTransient(service, sp => fallbackProvider.GetService(service));
|
||||||
}
|
}
|
||||||
return services;
|
return services;
|
||||||
|
|
@ -39,45 +34,12 @@ namespace Microsoft.AspNet.Hosting
|
||||||
{
|
{
|
||||||
configuration = configuration ?? new Configuration();
|
configuration = configuration ?? new Configuration();
|
||||||
var services = Import(fallbackServices);
|
var services = Import(fallbackServices);
|
||||||
services.Add(GetDefaultServices(configuration));
|
services.AddHosting(configuration);
|
||||||
services.AddSingleton<IServiceManifest>(sp => new HostingManifest(fallbackServices));
|
services.AddSingleton<IServiceManifest>(sp => new HostingManifest(fallbackServices));
|
||||||
services.AddInstance<IConfigureHostingEnvironment>(new ConfigureHostingEnvironment(configuration));
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
// REVIEW: make this private?
|
// Manifest exposes the fallback manifest in addition to ITypeActivator, IHostingEnvironment, and ILoggerFactory
|
||||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration = null)
|
|
||||||
{
|
|
||||||
configuration = configuration ?? new Configuration();
|
|
||||||
var describer = new ServiceDescriber(configuration);
|
|
||||||
|
|
||||||
yield return describer.Transient<IHostingEngine, HostingEngine>();
|
|
||||||
yield return describer.Transient<IServerManager, ServerManager>();
|
|
||||||
|
|
||||||
yield return describer.Transient<IStartupManager, StartupManager>();
|
|
||||||
yield return describer.Transient<IStartupLoaderProvider, StartupLoaderProvider>();
|
|
||||||
|
|
||||||
yield return describer.Transient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
|
|
||||||
yield return describer.Transient<IHttpContextFactory, HttpContextFactory>();
|
|
||||||
|
|
||||||
yield return describer.Instance<IApplicationLifetime>(new ApplicationLifetime());
|
|
||||||
|
|
||||||
// These three services as exported in the manifest
|
|
||||||
yield return describer.Singleton<ITypeActivator, TypeActivator>();
|
|
||||||
yield return describer.Singleton<IHostingEnvironment, HostingEnvironment>();
|
|
||||||
// TODO: Do we expect this to be provide by the runtime eventually?
|
|
||||||
yield return describer.Singleton<ILoggerFactory, LoggerFactory>();
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class HostingManifest : IServiceManifest
|
private class HostingManifest : IServiceManifest
|
||||||
{
|
{
|
||||||
public HostingManifest(IServiceProvider fallback)
|
public HostingManifest(IServiceProvider fallback)
|
||||||
|
|
|
||||||
|
|
@ -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<ILoggerFactory, LoggerFactory>());
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddHosting(this IServiceCollection services, IConfiguration configuration = null)
|
||||||
|
{
|
||||||
|
var describer = new ServiceDescriber(configuration);
|
||||||
|
|
||||||
|
services.TryAdd(describer.Transient<IHostingEngine, HostingEngine>());
|
||||||
|
services.TryAdd(describer.Transient<IServerManager, ServerManager>());
|
||||||
|
|
||||||
|
services.TryAdd(describer.Transient<IStartupManager, StartupManager>());
|
||||||
|
services.TryAdd(describer.Transient<IStartupLoaderProvider, StartupLoaderProvider>());
|
||||||
|
|
||||||
|
services.TryAdd(describer.Transient<IApplicationBuilderFactory, ApplicationBuilderFactory>());
|
||||||
|
services.TryAdd(describer.Transient<IHttpContextFactory, HttpContextFactory>());
|
||||||
|
|
||||||
|
services.TryAdd(describer.Instance<IApplicationLifetime>(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<IHostingEnvironment, HostingEnvironment>());
|
||||||
|
|
||||||
|
// TODO: Remove this once we have IHttpContextAccessor
|
||||||
|
services.AddContextAccessor(configuration);
|
||||||
|
|
||||||
|
// REVIEW: don't try add because we pull out IEnumerable<IConfigureHostingEnvironment>?
|
||||||
|
services.AddInstance<IConfigureHostingEnvironment>(new ConfigureHostingEnvironment(configuration));
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -152,10 +152,8 @@ namespace Microsoft.AspNet.Hosting.Startup
|
||||||
if (servicesMethod != null)
|
if (servicesMethod != null)
|
||||||
{
|
{
|
||||||
var services = HostingServices.Create(builder.ApplicationServices);
|
var services = HostingServices.Create(builder.ApplicationServices);
|
||||||
// TODO: remove adding options
|
// TODO: remove this once IHttpContextAccessor service is added
|
||||||
services.Add(OptionsServices.GetDefaultServices());
|
services.AddContextAccessor();
|
||||||
services.AddScoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>));
|
|
||||||
|
|
||||||
if (servicesMethod.ReturnType == typeof(IServiceProvider))
|
if (servicesMethod.ReturnType == typeof(IServiceProvider))
|
||||||
{
|
{
|
||||||
// IServiceProvider ConfigureServices(IServiceCollection)
|
// IServiceProvider ConfigureServices(IServiceCollection)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ using Microsoft.AspNet.RequestContainer;
|
||||||
using Microsoft.AspNet.Hosting;
|
using Microsoft.AspNet.Hosting;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Builder
|
namespace Microsoft.AspNet.Builder
|
||||||
{
|
{
|
||||||
|
|
@ -49,9 +48,8 @@ namespace Microsoft.AspNet.Builder
|
||||||
// Import services from hosting/KRE as fallback
|
// Import services from hosting/KRE as fallback
|
||||||
var serviceCollection = HostingServices.Create(builder.ApplicationServices);
|
var serviceCollection = HostingServices.Create(builder.ApplicationServices);
|
||||||
|
|
||||||
// TODO: should remove OptionServices here soon...
|
// TODO: remove this once IHttpContextAccessor service is added
|
||||||
serviceCollection.Add(OptionsServices.GetDefaultServices());
|
serviceCollection.AddContextAccessor();
|
||||||
serviceCollection.AddScoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>));
|
|
||||||
|
|
||||||
// REVIEW: serviceCollection has the merged services, manifests are lost after this
|
// REVIEW: serviceCollection has the merged services, manifests are lost after this
|
||||||
builder.ApplicationServices = configureServices(serviceCollection);
|
builder.ApplicationServices = configureServices(serviceCollection);
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,13 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.Configure<FakeOptions>(o => o.Configured = true);
|
services.Configure<FakeOptions>(o => o.Configured = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConfigureDevServices(IServiceCollection services)
|
public void ConfigureDevServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
@ -31,6 +33,7 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public void ConfigureRetailServices(IServiceCollection services)
|
public void ConfigureRetailServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
@ -40,6 +43,7 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public static void ConfigureStaticServices(IServiceCollection services)
|
public static void ConfigureStaticServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
@ -49,8 +53,7 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public static IServiceProvider ConfigureStaticProviderServices()
|
public static IServiceProvider ConfigureStaticProviderServices()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection().AddOptions();
|
||||||
services.Add(OptionsServices.GetDefaultServices());
|
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
@ -71,6 +74,7 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public IServiceProvider ConfigureProviderServices(IServiceCollection services)
|
public IServiceProvider ConfigureProviderServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
@ -81,8 +85,7 @@ namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
||||||
public IServiceProvider ConfigureProviderArgsServices(IApplicationBuilder me)
|
public IServiceProvider ConfigureProviderArgsServices(IApplicationBuilder me)
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection().AddOptions();
|
||||||
services.Add(OptionsServices.GetDefaultServices());
|
|
||||||
services.Configure<FakeOptions>(o =>
|
services.Configure<FakeOptions>(o =>
|
||||||
{
|
{
|
||||||
o.Configured = true;
|
o.Configured = true;
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ namespace Microsoft.AspNet.Hosting.Tests
|
||||||
[Fact]
|
[Fact]
|
||||||
public void StartupClassMayHaveHostingServicesInjected()
|
public void StartupClassMayHaveHostingServicesInjected()
|
||||||
{
|
{
|
||||||
var serviceCollection = new ServiceCollection();
|
var serviceCollection = new ServiceCollection().AddHosting();
|
||||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
|
||||||
serviceCollection.AddInstance<IFakeStartupCallback>(this);
|
serviceCollection.AddInstance<IFakeStartupCallback>(this);
|
||||||
var services = serviceCollection.BuildServiceProvider();
|
var services = serviceCollection.BuildServiceProvider();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<IOptions<object>>();
|
|
||||||
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<IOptions<object>>();
|
|
||||||
Assert.NotNull(optionsAccessor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue