Hosting API Review changes

- Introduce WebHostBuilder
This commit is contained in:
Hao Kung 2015-04-23 23:51:29 -07:00
parent 663bf4f0f9
commit 234bbf82f2
18 changed files with 566 additions and 827 deletions

View File

@ -1,257 +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 System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Hosting.Builder;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting
{
public class HostingEngine : IHostingEngine
{
private readonly IServiceCollection _applicationServiceCollection;
private readonly IStartupLoader _startupLoader;
private readonly ApplicationLifetime _applicationLifetime;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _config;
// Start/ApplicationServices block use methods
private bool _useDisabled;
private IApplicationBuilderFactory _builderFactory;
private IApplicationBuilder _builder;
private IServiceProvider _applicationServices;
// Only one of these should be set
private string _startupAssemblyName;
private StartupMethods _startup;
// Only one of these should be set
private string _serverFactoryLocation;
private IServerFactory _serverFactory;
private IServerInformation _serverInstance;
public HostingEngine(IServiceCollection appServices, IStartupLoader startupLoader, IConfiguration config, IHostingEnvironment hostingEnv, string appName)
{
_config = config ?? new Configuration();
_applicationServiceCollection = appServices;
_startupLoader = startupLoader;
_startupAssemblyName = appName;
_applicationLifetime = new ApplicationLifetime();
_hostingEnvironment = hostingEnv;
}
public virtual IDisposable Start()
{
EnsureApplicationServices();
EnsureServer();
EnsureBuilder();
var applicationDelegate = BuildApplicationDelegate();
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
var contextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
var contextAccessor = _applicationServices.GetRequiredService<IHttpContextAccessor>();
var server = _serverFactory.Start(_serverInstance,
async features =>
{
var httpContext = contextFactory.CreateHttpContext(features);
var requestIdentifier = GetRequestIdentifier(httpContext);
using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
{
contextAccessor.HttpContext = httpContext;
await applicationDelegate(httpContext);
}
});
return new Disposable(() =>
{
_applicationLifetime.NotifyStopping();
server.Dispose();
_applicationLifetime.NotifyStopped();
});
}
private void EnsureApplicationServices()
{
_useDisabled = true;
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = _startup.ConfigureServicesDelegate(_applicationServiceCollection);
}
private void EnsureStartup()
{
if (_startup != null)
{
return;
}
var diagnosticMessages = new List<string>();
_startup = _startupLoader.Load(
_startupAssemblyName,
_hostingEnvironment.EnvironmentName,
diagnosticMessages);
if (_startup == null)
{
throw new ArgumentException(
diagnosticMessages.Aggregate("Failed to find a startup entry point for the web application.", (a, b) => a + "\r\n" + b),
_startupAssemblyName);
}
}
private void EnsureServer()
{
if (_serverFactory == null)
{
// Blow up if we don't have a server set at this point
if (_serverFactoryLocation == null)
{
throw new InvalidOperationException("UseStartup() is required for Start()");
}
_serverFactory = _applicationServices.GetRequiredService<IServerLoader>().LoadServerFactory(_serverFactoryLocation);
}
_serverInstance = _serverFactory.Initialize(_config);
}
private void EnsureBuilder()
{
if (_builderFactory == null)
{
_builderFactory = _applicationServices.GetRequiredService<IApplicationBuilderFactory>();
}
_builder = _builderFactory.CreateBuilder(_serverInstance);
_builder.ApplicationServices = _applicationServices;
}
private RequestDelegate BuildApplicationDelegate()
{
var startupFilters = _applicationServices.GetService<IEnumerable<IStartupFilter>>();
var configure = _startup.ConfigureDelegate;
foreach (var filter in startupFilters)
{
configure = filter.Configure(_builder, configure);
}
configure(_builder);
return _builder.Build();
}
public IServiceProvider ApplicationServices
{
get
{
EnsureApplicationServices();
return _applicationServices;
}
}
private void CheckUseAllowed()
{
if (_useDisabled)
{
throw new InvalidOperationException("HostingEngine has already been started.");
}
}
private Guid GetRequestIdentifier(HttpContext httpContext)
{
var requestIdentifierFeature = httpContext.GetFeature<IRequestIdentifierFeature>();
if (requestIdentifierFeature == null)
{
requestIdentifierFeature = new DefaultRequestIdentifierFeature();
httpContext.SetFeature<IRequestIdentifierFeature>(requestIdentifierFeature);
}
return requestIdentifierFeature.TraceIdentifier;
}
// Consider cutting
public IHostingEngine UseEnvironment(string environment)
{
CheckUseAllowed();
_hostingEnvironment.EnvironmentName = environment;
return this;
}
public IHostingEngine UseServer(string assemblyName)
{
CheckUseAllowed();
_serverFactoryLocation = assemblyName;
return this;
}
public IHostingEngine UseServer(IServerFactory factory)
{
CheckUseAllowed();
_serverFactory = factory;
return this;
}
public IHostingEngine UseStartup(string startupAssemblyName)
{
CheckUseAllowed();
_startupAssemblyName = startupAssemblyName;
return this;
}
public IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp)
{
return UseStartup(configureApp, configureServices: null);
}
public IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices)
{
CheckUseAllowed();
_startup = new StartupMethods(configureApp, configureServices);
return this;
}
public IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{
CheckUseAllowed();
_startup = new StartupMethods(configureApp,
services => {
if (configureServices != null)
{
configureServices(services);
}
return services.BuildServiceProvider();
});
return this;
}
private class Disposable : IDisposable
{
private Action _dispose;
public Disposable(Action dispose)
{
_dispose = dispose;
}
public void Dispose()
{
Interlocked.Exchange(ref _dispose, () => { }).Invoke();
}
}
}
}

View File

@ -1,35 +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 Microsoft.AspNet.Hosting.Internal;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
{
public class HostingFactory : IHostingFactory
{
public const string EnvironmentKey = "ASPNET_ENV";
private readonly RootHostingServiceCollectionInitializer _serviceInitializer;
private readonly IStartupLoader _startupLoader;
private readonly IApplicationEnvironment _applicationEnvironment;
private readonly IHostingEnvironment _hostingEnvironment;
public HostingFactory(RootHostingServiceCollectionInitializer initializer, IStartupLoader startupLoader, IApplicationEnvironment appEnv, IHostingEnvironment hostingEnv)
{
_serviceInitializer = initializer;
_startupLoader = startupLoader;
_applicationEnvironment = appEnv;
_hostingEnvironment = hostingEnv;
}
public IHostingEngine Create(IConfiguration config)
{
_hostingEnvironment.Initialize(_applicationEnvironment.ApplicationBasePath, config?[EnvironmentKey]);
return new HostingEngine(_serviceInitializer.Build(), _startupLoader, config, _hostingEnvironment, _applicationEnvironment.ApplicationName);
}
}
}

View File

@ -1,32 +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.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Hosting
{
public interface IHostingEngine
{
IDisposable Start();
// Accessing this will block Use methods
IServiceProvider ApplicationServices { get; }
// Use methods blow up after any of the above methods are called
IHostingEngine UseEnvironment(string environment);
// Mutually exclusive
IHostingEngine UseServer(string assemblyName);
IHostingEngine UseServer(IServerFactory factory);
// Mutually exclusive
IHostingEngine UseStartup(string startupAssemblyName);
IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp);
IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices);
IHostingEngine UseStartup(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices);
}
}

View File

@ -1,12 +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 Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Hosting
{
public interface IHostingFactory
{
IHostingEngine Create(IConfiguration config);
}
}

View File

@ -0,0 +1,184 @@
// 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 System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Builder;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting.Internal
{
public class HostingEngine : IHostingEngine
{
private readonly IServiceCollection _applicationServiceCollection;
private readonly IStartupLoader _startupLoader;
private readonly ApplicationLifetime _applicationLifetime;
private readonly IConfiguration _config;
private IServiceProvider _applicationServices;
// Only one of these should be set
internal string StartupAssemblyName { get; set; }
internal StartupMethods Startup { get; set; }
internal Type StartupType { get; set; }
// Only one of these should be set
internal IServerFactory ServerFactory { get; set; }
internal string ServerFactoryLocation { get; set; }
private IServerInformation _serverInstance;
public HostingEngine(
[NotNull] IServiceCollection appServices,
[NotNull] IStartupLoader startupLoader,
[NotNull] IConfiguration config)
{
_config = config;
_applicationServiceCollection = appServices;
_startupLoader = startupLoader;
_applicationLifetime = new ApplicationLifetime();
}
public IServiceProvider ApplicationServices
{
get
{
EnsureApplicationServices();
return _applicationServices;
}
}
public virtual IDisposable Start()
{
EnsureApplicationServices();
var application = BuildApplication();
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
var contextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
var contextAccessor = _applicationServices.GetRequiredService<IHttpContextAccessor>();
var server = ServerFactory.Start(_serverInstance,
async features =>
{
var httpContext = contextFactory.CreateHttpContext(features);
var requestIdentifier = GetRequestIdentifier(httpContext);
using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
{
contextAccessor.HttpContext = httpContext;
await application(httpContext);
}
});
return new Disposable(() =>
{
_applicationLifetime.NotifyStopping();
server.Dispose();
_applicationLifetime.NotifyStopped();
});
}
private void EnsureApplicationServices()
{
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection);
}
private void EnsureStartup()
{
if (Startup != null)
{
return;
}
if (StartupType == null)
{
var diagnosticTypeMessages = new List<string>();
StartupType = _startupLoader.FindStartupType(StartupAssemblyName, diagnosticTypeMessages);
if (StartupType == null)
{
throw new ArgumentException(
diagnosticTypeMessages.Aggregate("Failed to find a startup type for the web application.", (a, b) => a + "\r\n" + b),
StartupAssemblyName);
}
}
var diagnosticMessages = new List<string>();
Startup = _startupLoader.LoadMethods(StartupType, diagnosticMessages);
if (Startup == null)
{
throw new ArgumentException(
diagnosticMessages.Aggregate("Failed to find a startup entry point for the web application.", (a, b) => a + "\r\n" + b),
StartupAssemblyName);
}
}
private RequestDelegate BuildApplication()
{
if (ServerFactory == null)
{
// Blow up if we don't have a server set at this point
if (ServerFactoryLocation == null)
{
throw new InvalidOperationException("IHostingBuilder.UseServer() is required for " + nameof(Start) + "()");
}
ServerFactory = _applicationServices.GetRequiredService<IServerLoader>().LoadServerFactory(ServerFactoryLocation);
}
_serverInstance = ServerFactory.Initialize(_config);
var builderFactory = _applicationServices.GetRequiredService<IApplicationBuilderFactory>();
var builder = builderFactory.CreateBuilder(_serverInstance);
builder.ApplicationServices = _applicationServices;
var startupFilters = _applicationServices.GetService<IEnumerable<IStartupFilter>>();
var configure = Startup.ConfigureDelegate;
foreach (var filter in startupFilters)
{
configure = filter.Configure(builder, configure);
}
configure(builder);
return builder.Build();
}
private Guid GetRequestIdentifier(HttpContext httpContext)
{
var requestIdentifierFeature = httpContext.GetFeature<IRequestIdentifierFeature>();
if (requestIdentifierFeature == null)
{
requestIdentifierFeature = new DefaultRequestIdentifierFeature();
httpContext.SetFeature(requestIdentifierFeature);
}
return requestIdentifierFeature.TraceIdentifier;
}
private class Disposable : IDisposable
{
private Action _dispose;
public Disposable(Action dispose)
{
_dispose = dispose;
}
public void Dispose()
{
Interlocked.Exchange(ref _dispose, () => { }).Invoke();
}
}
}
}

View File

@ -0,0 +1,15 @@
// 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;
namespace Microsoft.AspNet.Hosting.Internal
{
public interface IHostingEngine
{
IDisposable Start();
// Accessing this will block Use methods
IServiceProvider ApplicationServices { get; }
}
}

View File

@ -1,66 +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.Hosting.Builder;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
namespace Microsoft.AspNet.Hosting.Internal
{
public class RootHostingServiceCollectionInitializer
{
private readonly IServiceProvider _fallbackServices;
private readonly Action<IServiceCollection> _configureServices;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ILoggerFactory _loggerFactory;
public RootHostingServiceCollectionInitializer(IServiceProvider fallbackServices, Action<IServiceCollection> configureServices)
{
_fallbackServices = fallbackServices;
_configureServices = configureServices;
_hostingEnvironment = new HostingEnvironment();
_loggerFactory = new LoggerFactory();
}
public IServiceCollection Build()
{
var services = new ServiceCollection();
// Import from manifest
var manifest = _fallbackServices.GetRequiredService<IServiceManifest>();
foreach (var service in manifest.Services)
{
services.AddTransient(service, sp => _fallbackServices.GetService(service));
}
services.AddInstance(_hostingEnvironment);
services.AddInstance(this);
services.AddInstance(_loggerFactory);
services.AddTransient<IHostingFactory, HostingFactory>();
services.AddTransient<IStartupLoader, StartupLoader>();
services.AddTransient<IServerLoader, ServerLoader>();
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
services.AddTransient<IHttpContextFactory, HttpContextFactory>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
// Conjure up a RequestServices
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
if (_configureServices != null)
{
_configureServices(services);
}
return services;
}
}
}

View File

@ -5,6 +5,7 @@ using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
@ -33,21 +34,10 @@ namespace Microsoft.AspNet.Hosting
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
config.AddCommandLine(args); config.AddCommandLine(args);
var engine = WebHost.CreateEngine(_serviceProvider, config); var host = new WebHostBuilder(_serviceProvider).Build();
var server = config.Get("server"); var serverShutdown = host.Start();
if (server != null) var loggerFactory = host.ApplicationServices.GetRequiredService<ILoggerFactory>();
{ var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
engine.UseServer(server);
}
var startup = config.Get("app");
if (startup != null)
{
engine.UseStartup(startup);
}
var serverShutdown = engine.Start();
var loggerFactory = engine.ApplicationServices.GetRequiredService<ILoggerFactory>();
var appShutdownService = engine.ApplicationServices.GetRequiredService<IApplicationShutdown>();
var shutdownHandle = new ManualResetEvent(false); var shutdownHandle = new ManualResetEvent(false);
appShutdownService.ShutdownRequested.Register(() => appShutdownService.ShutdownRequested.Register(() =>

View File

@ -8,9 +8,8 @@ namespace Microsoft.AspNet.Hosting.Startup
{ {
public interface IStartupLoader public interface IStartupLoader
{ {
StartupMethods Load( Type FindStartupType(string startupAssemblyName, IList<string> diagnosticMessages);
string startupAssemblyName,
string environmentName, StartupMethods LoadMethods(Type startupType, IList<string> diagnosticMessages);
IList<string> diagnosticMessages);
} }
} }

View File

@ -13,17 +13,19 @@ namespace Microsoft.AspNet.Hosting.Startup
public class StartupLoader : IStartupLoader public class StartupLoader : IStartupLoader
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services;
private readonly IHostingEnvironment _hostingEnv;
public StartupLoader(IServiceProvider services) public StartupLoader(IServiceProvider services, IHostingEnvironment hostingEnv)
{ {
_services = services; _services = services;
_hostingEnv = hostingEnv;
} }
public StartupMethods Load( public StartupMethods LoadMethods(
Type startupType, Type startupType,
string environmentName,
IList<string> diagnosticMessages) IList<string> diagnosticMessages)
{ {
var environmentName = _hostingEnv.EnvironmentName;
var configureMethod = FindConfigureDelegate(startupType, environmentName); var configureMethod = FindConfigureDelegate(startupType, environmentName);
var servicesMethod = FindConfigureServicesDelegate(startupType, environmentName); var servicesMethod = FindConfigureServicesDelegate(startupType, environmentName);
@ -36,11 +38,9 @@ namespace Microsoft.AspNet.Hosting.Startup
return new StartupMethods(configureMethod.Build(instance), servicesMethod?.Build(instance)); return new StartupMethods(configureMethod.Build(instance), servicesMethod?.Build(instance));
} }
public StartupMethods Load( public Type FindStartupType(string startupAssemblyName, IList<string> diagnosticMessages)
string startupAssemblyName,
string environmentName,
IList<string> diagnosticMessages)
{ {
var environmentName = _hostingEnv.EnvironmentName;
if (string.IsNullOrEmpty(startupAssemblyName)) if (string.IsNullOrEmpty(startupAssemblyName))
{ {
throw new ArgumentException("Value cannot be null or empty.", nameof(startupAssemblyName)); throw new ArgumentException("Value cannot be null or empty.", nameof(startupAssemblyName));
@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Hosting.Startup
startupAssemblyName)); startupAssemblyName));
} }
return Load(type, environmentName, diagnosticMessages); return type;
} }
private static ConfigureBuilder FindConfigureDelegate(Type startupType, string environmentName) private static ConfigureBuilder FindConfigureDelegate(Type startupType, string environmentName)

View File

@ -1,63 +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.Hosting.Internal;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Runtime.Infrastructure;
namespace Microsoft.AspNet.Hosting
{
public static class WebHost
{
public static IHostingEngine CreateEngine()
{
return CreateEngine(new Configuration());
}
public static IHostingEngine CreateEngine(Action<IServiceCollection> configureServices)
{
return CreateEngine(new Configuration(), configureServices);
}
public static IHostingEngine CreateEngine(IConfiguration config)
{
return CreateEngine(config, configureServices: null);
}
public static IHostingEngine CreateEngine(IConfiguration config, Action<IServiceCollection> configureServices)
{
return CreateEngine(fallbackServices: null, config: config, configureServices: configureServices);
}
public static IHostingEngine CreateEngine(IServiceProvider fallbackServices, IConfiguration config)
{
return CreateEngine(fallbackServices, config, configureServices: null);
}
public static IHostingEngine CreateEngine(IServiceProvider fallbackServices, IConfiguration config, Action<IServiceCollection> configureServices)
{
return CreateFactory(fallbackServices, configureServices).Create(config);
}
public static IHostingFactory CreateFactory()
{
return CreateFactory(fallbackServices: null, configureServices: null);
}
public static IHostingFactory CreateFactory(Action<IServiceCollection> configureServices)
{
return CreateFactory(fallbackServices: null, configureServices: configureServices);
}
public static IHostingFactory CreateFactory(IServiceProvider fallbackServices, Action<IServiceCollection> configureServices)
{
fallbackServices = fallbackServices ?? CallContextServiceLocator.Locator.ServiceProvider;
return new RootHostingServiceCollectionInitializer(fallbackServices, configureServices)
.Build()
.BuildServiceProvider()
.GetRequiredService<IHostingFactory>();
}
}
}

View File

@ -0,0 +1,189 @@
// 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.AspNet.Hosting.Builder;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
{
public class WebHostBuilder
{
public const string OldEnvironmentKey = "ASPNET_ENV";
public const string EnvironmentKey = "Hosting:Environment";
public const string OldApplicationKey = "app";
public const string ApplicationKey = "Hosting:Application";
public const string OldServerKey = "server";
public const string ServerKey = "Hosting:Server";
private readonly IServiceProvider _services;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ILoggerFactory _loggerFactory;
private readonly IConfiguration _config;
private Action<IServiceCollection> _configureServices;
// Only one of these should be set
private StartupMethods _startup;
private Type _startupType;
private string _startupAssemblyName;
// Only one of these should be set
private string _serverFactoryLocation;
private IServerFactory _serverFactory;
public WebHostBuilder([NotNull] IServiceProvider services) : this(services, config: new Configuration()) { }
public WebHostBuilder([NotNull] IServiceProvider services, [NotNull] IConfiguration config)
{
_hostingEnvironment = new HostingEnvironment();
_loggerFactory = new LoggerFactory();
_services = services;
_config = config;
}
private IServiceCollection BuildHostingServices()
{
var services = new ServiceCollection();
// Import from manifest
var manifest = _services.GetService<IServiceManifest>();
if (manifest != null)
{
foreach (var service in manifest.Services)
{
services.AddTransient(service, sp => _services.GetService(service));
}
}
services.AddInstance(_hostingEnvironment);
services.AddInstance(_loggerFactory);
services.AddTransient<IStartupLoader, StartupLoader>();
services.AddTransient<IServerLoader, ServerLoader>();
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
services.AddTransient<IHttpContextFactory, HttpContextFactory>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
// Conjure up a RequestServices
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
if (_configureServices != null)
{
_configureServices(services);
}
return services;
}
public IHostingEngine Build()
{
var hostingServices = BuildHostingServices();
var hostingContainer = hostingServices.BuildServiceProvider();
var appEnvironment = hostingContainer.GetRequiredService<IApplicationEnvironment>();
var startupLoader = hostingContainer.GetRequiredService<IStartupLoader>();
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _config?[EnvironmentKey] ?? _config?[OldEnvironmentKey]);
var engine = new HostingEngine(hostingServices, startupLoader, _config);
// Only one of these should be set, but they are used in priority
engine.ServerFactory = _serverFactory;
engine.ServerFactoryLocation = _config.Get(ServerKey) ?? _config.Get(OldServerKey) ?? _serverFactoryLocation;
// Only one of these should be set, but they are used in priority
engine.Startup = _startup;
engine.StartupType = _startupType;
engine.StartupAssemblyName = _config.Get(ApplicationKey) ?? _config.Get(OldApplicationKey) ?? appEnvironment.ApplicationName;
return engine;
}
public WebHostBuilder UseServices(Action<IServiceCollection> configureServices)
{
_configureServices = configureServices;
return this;
}
public WebHostBuilder UseEnvironment([NotNull] string environment)
{
_hostingEnvironment.EnvironmentName = environment;
return this;
}
public WebHostBuilder UseServer([NotNull] string assemblyName)
{
_serverFactoryLocation = assemblyName;
return this;
}
public WebHostBuilder UseServer(IServerFactory factory)
{
_serverFactory = factory;
return this;
}
public WebHostBuilder UseStartup([NotNull] string startupAssemblyName)
{
if (startupAssemblyName == null)
{
throw new ArgumentNullException(nameof(startupAssemblyName));
}
_startupAssemblyName = startupAssemblyName;
return this;
}
public WebHostBuilder UseStartup([NotNull] Type startupType)
{
if (startupType == null)
{
throw new ArgumentNullException(nameof(startupType));
}
_startupType = startupType;
return this;
}
public WebHostBuilder UseStartup<TStartup>() where TStartup : class
{
return UseStartup(typeof(TStartup));
}
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp)
{
return UseStartup(configureApp, configureServices: null);
}
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices)
{
_startup = new StartupMethods(configureApp, configureServices);
return this;
}
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{
_startup = new StartupMethods(configureApp,
services => {
if (configureServices != null)
{
configureServices(services);
}
return services.BuildServiceProvider();
});
return this;
}
}
}

View File

@ -12,6 +12,7 @@
"Microsoft.Framework.DependencyInjection": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*", "Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" },
"Newtonsoft.Json": "6.0.6" "Newtonsoft.Json": "6.0.6"
}, },
"frameworks": { "frameworks": {

View File

@ -12,6 +12,7 @@ using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Runtime.Infrastructure;
namespace Microsoft.AspNet.TestHost namespace Microsoft.AspNet.TestHost
{ {
@ -24,101 +25,66 @@ namespace Microsoft.AspNet.TestHost
private IDisposable _appInstance; private IDisposable _appInstance;
private bool _disposed = false; private bool _disposed = false;
public TestServer(IHostingEngine engine) public TestServer(WebHostBuilder builder)
{ {
_appInstance = engine.UseServer(this).Start(); _appInstance = builder.UseServer(this).Build().Start();
} }
public Uri BaseAddress { get; set; } = new Uri("http://localhost/"); public Uri BaseAddress { get; set; } = new Uri("http://localhost/");
public static TestServer Create() public static TestServer Create()
{ {
return Create(fallbackServices: null, config: null, configureApp: null, configureServices: null); return Create(services: null, config: null, configureApp: null, configureServices: null);
} }
public static TestServer Create(Action<IApplicationBuilder> configureApp) public static TestServer Create(Action<IApplicationBuilder> configureApp)
{ {
return Create(fallbackServices: null, config: null, configureApp: configureApp, configureServices: null); return Create(services: null, config: null, configureApp: configureApp, configureServices: null);
} }
public static TestServer Create(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) public static TestServer Create(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{ {
return Create(fallbackServices: null, config: null, configureApp: configureApp, configureServices: configureServices); return Create(services: null, config: null, configureApp: configureApp, configureServices: configureServices);
} }
public static TestServer Create(IServiceProvider fallbackServices, Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices) public static TestServer Create(IServiceProvider services, Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices)
{ {
return CreateBuilder(fallbackServices, config: null, configureApp: configureApp, configureServices: configureServices).Build(); return new TestServer(CreateBuilder(services, config: null, configureApp: configureApp, configureServices: configureServices));
} }
public static TestServer Create(IServiceProvider fallbackServices, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) public static TestServer Create(IServiceProvider services, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{ {
return CreateBuilder(fallbackServices, config, configureApp, configureServices).Build(); return new TestServer(CreateBuilder(services, config, configureApp, configureServices));
} }
public static TestServer Create<TStartup>() where TStartup : class public static WebHostBuilder CreateBuilder(IServiceProvider services, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{ {
return Create<TStartup>(fallbackServices: null, config: null, configureApp: null, configureServices: null); return CreateBuilder(services, config, configureApp,
} s =>
public static TestServer Create<TStartup>(Action<IApplicationBuilder> configureApp) where TStartup : class
{
return Create<TStartup>(fallbackServices: null, config: null, configureApp: configureApp, configureServices: null);
}
public static TestServer Create<TStartup>(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) where TStartup : class
{
return Create<TStartup>(fallbackServices: null, config: null, configureApp: configureApp, configureServices: configureServices);
}
public static TestServer Create<TStartup>(IServiceProvider fallbackServices, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) where TStartup : class
{
var builder = CreateBuilder(fallbackServices, config, configureApp, configureServices);
builder.StartupType = typeof(TStartup);
return builder.Build();
}
public static TestServerBuilder CreateBuilder<TStartup>() where TStartup : class
{
var builder = CreateBuilder(fallbackServices: null, config: null, configureApp: null, configureServices: null);
builder.StartupType = typeof(TStartup);
return builder;
}
public static TestServerBuilder CreateBuilder<TStartup>(IServiceProvider fallbackServices, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) where TStartup : class
{
var builder = CreateBuilder(fallbackServices, config, configureApp, configureServices);
builder.StartupType = typeof(TStartup);
return builder;
}
public static TestServerBuilder CreateBuilder(IServiceProvider fallbackServices, IConfiguration config, Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{
return CreateBuilder(fallbackServices, config, configureApp,
services =>
{ {
if (configureServices != null) if (configureServices != null)
{ {
configureServices(services); configureServices(s);
} }
return services.BuildServiceProvider(); return s.BuildServiceProvider();
}); });
} }
public static TestServerBuilder CreateBuilder(IServiceProvider fallbackServices, IConfiguration config) public static WebHostBuilder CreateBuilder(IServiceProvider services, IConfiguration config, Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices)
{ {
return new TestServerBuilder return CreateBuilder(services, config).UseStartup(configureApp, configureServices);
{
FallbackServices = fallbackServices,
Config = config
};
} }
public static TestServerBuilder CreateBuilder(IServiceProvider fallbackServices, IConfiguration config, Action<IApplicationBuilder> configureApp, ConfigureServicesDelegate configureServices) public static WebHostBuilder CreateBuilder(IServiceProvider services, IConfiguration config)
{ {
var builder = CreateBuilder(fallbackServices, config); return new WebHostBuilder(
builder.Startup = new StartupMethods(configureApp, configureServices); services ?? CallContextServiceLocator.Locator.ServiceProvider,
return builder; config ?? new Configuration());
}
public static WebHostBuilder CreateBuilder()
{
return CreateBuilder(services: null, config: null);
} }
public HttpMessageHandler CreateHandler() public HttpMessageHandler CreateHandler()

View File

@ -1,126 +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 System.Collections.Generic;
using System.Runtime.Versioning;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
namespace Microsoft.AspNet.TestHost
{
public class TestServerBuilder
{
public IServiceProvider FallbackServices { get; set; }
public string Environment { get; set; }
public string ApplicationName { get; set; }
public string ApplicationBasePath { get; set; }
public Type StartupType { get; set; }
public string StartupAssemblyName { get; set; }
public IConfiguration Config { get; set; }
public IServiceCollection AdditionalServices { get; } = new ServiceCollection();
public StartupMethods Startup { get; set; }
public TestServer Build()
{
var fallbackServices = FallbackServices ?? CallContextServiceLocator.Locator.ServiceProvider;
var config = Config ?? new Configuration();
if (Environment != null)
{
config[HostingFactory.EnvironmentKey] = Environment;
}
if (ApplicationName != null || ApplicationBasePath != null)
{
var appEnv = new TestApplicationEnvironment(fallbackServices.GetRequiredService<IApplicationEnvironment>());
appEnv.ApplicationBasePath = ApplicationBasePath;
appEnv.ApplicationName = ApplicationName;
AdditionalServices.AddInstance<IApplicationEnvironment>(appEnv);
}
var engine = WebHost.CreateEngine(fallbackServices,
config,
services => services.Add(AdditionalServices));
if (StartupType != null)
{
Startup = new StartupLoader(fallbackServices).Load(StartupType, Environment, new List<string>());
}
if (Startup != null)
{
engine.UseStartup(Startup.ConfigureDelegate, Startup.ConfigureServicesDelegate);
}
else if (StartupAssemblyName != null)
{
engine.UseStartup(StartupAssemblyName);
}
return new TestServer(engine);
}
private class TestApplicationEnvironment : IApplicationEnvironment
{
private readonly IApplicationEnvironment _appEnv;
private string _appName;
private string _appBasePath;
public TestApplicationEnvironment(IApplicationEnvironment appEnv)
{
_appEnv = appEnv;
}
public string ApplicationBasePath
{
get
{
return _appBasePath ?? _appEnv.ApplicationBasePath;
}
set
{
_appBasePath = value;
}
}
public string ApplicationName
{
get
{
return _appName ?? _appEnv.ApplicationName;
}
set
{
_appName = value;
}
}
public string Configuration
{
get
{
return _appEnv.Configuration;
}
}
public FrameworkName RuntimeFramework
{
get
{
return _appEnv.RuntimeFramework;
}
}
public string Version
{
get
{
throw new NotImplementedException();
}
}
}
}
}

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Hosting.Builder; using Microsoft.AspNet.Hosting.Builder;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Hosting.Startup; using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
@ -16,6 +17,7 @@ using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.Runtime.Infrastructure;
using Moq; using Moq;
using Xunit; using Xunit;
@ -24,19 +26,58 @@ namespace Microsoft.AspNet.Hosting
public class HostingEngineTests : IServerFactory public class HostingEngineTests : IServerFactory
{ {
private readonly IList<StartInstance> _startInstances = new List<StartInstance>(); private readonly IList<StartInstance> _startInstances = new List<StartInstance>();
private IFeatureCollection _featuresSupportedByThisHost = new FeatureCollection();
[Fact] [Fact]
public void HostingEngineThrowsWithNoServer() public void HostingEngineThrowsWithNoServer()
{ {
Assert.Throws<InvalidOperationException>(() => WebHost.CreateEngine().Start()); var ex = Assert.Throws<InvalidOperationException>(() => CreateBuilder().Build().Start());
Assert.True(ex.Message.Contains("UseServer()"));
}
[Fact]
public void UseStartupThrowsWithNull()
{
Assert.Throws<ArgumentNullException>(() => CreateBuilder().UseStartup((string)null));
}
[Fact]
public void CanStartWithOldServerConfig()
{
var vals = new Dictionary<string, string>
{
{ "server", "Microsoft.AspNet.Hosting.Tests" }
};
var config = new Configuration()
.Add(new MemoryConfigurationSource(vals));
var host = CreateBuilder(config).Build();
host.Start();
Assert.NotNull(host.ApplicationServices.GetRequiredService<IHostingEnvironment>());
}
[Fact]
public void CanStartWithServerConfig()
{
var vals = new Dictionary<string, string>
{
{ "Hosting:Server", "Microsoft.AspNet.Hosting.Tests" }
};
var config = new Configuration()
.Add(new MemoryConfigurationSource(vals));
var host = CreateBuilder(config).Build();
host.Start();
Assert.NotNull(host.ApplicationServices.GetRequiredService<IHostingEnvironment>());
} }
[Fact] [Fact]
public void HostingEngineCanBeStarted() public void HostingEngineCanBeStarted()
{ {
var engine = WebHost.CreateEngine() var engine = CreateBuilder()
.UseServer(this) .UseServer(this)
.UseStartup("Microsoft.AspNet.Hosting.Tests") .UseStartup("Microsoft.AspNet.Hosting.Tests")
.Build()
.Start(); .Start();
Assert.NotNull(engine); Assert.NotNull(engine);
@ -51,10 +92,11 @@ namespace Microsoft.AspNet.Hosting
[Fact] [Fact]
public void HostingEngineInjectsHostingEnvironment() public void HostingEngineInjectsHostingEnvironment()
{ {
var engine = WebHost.CreateEngine() var engine = CreateBuilder()
.UseServer(this) .UseServer(this)
.UseStartup("Microsoft.AspNet.Hosting.Tests") .UseStartup("Microsoft.AspNet.Hosting.Tests")
.UseEnvironment("WithHostingEnvironment"); .UseEnvironment("WithHostingEnvironment")
.Build();
using (var server = engine.Start()) using (var server = engine.Start())
{ {
@ -63,20 +105,13 @@ namespace Microsoft.AspNet.Hosting
} }
} }
[Fact]
public void CanReplaceHostingFactory()
{
var factory = WebHost.CreateFactory(services => services.AddTransient<IHostingFactory, TestEngineFactory>());
Assert.NotNull(factory as TestEngineFactory);
}
[Fact] [Fact]
public void CanReplaceStartupLoader() public void CanReplaceStartupLoader()
{ {
var engine = WebHost.CreateEngine(services => services.AddTransient<IStartupLoader, TestLoader>()) var engine = CreateBuilder().UseServices(services => services.AddTransient<IStartupLoader, TestLoader>())
.UseServer(this) .UseServer(this)
.UseStartup("Microsoft.AspNet.Hosting.Tests"); .UseStartup("Microsoft.AspNet.Hosting.Tests")
.Build();
Assert.Throws<NotImplementedException>(() => engine.Start()); Assert.Throws<NotImplementedException>(() => engine.Start());
} }
@ -84,20 +119,20 @@ namespace Microsoft.AspNet.Hosting
[Fact] [Fact]
public void CanCreateApplicationServicesWithAddedServices() public void CanCreateApplicationServicesWithAddedServices()
{ {
var engineStart = WebHost.CreateEngine(services => services.AddOptions()); var host = CreateBuilder().UseServices(services => services.AddOptions()).Build();
Assert.NotNull(engineStart.ApplicationServices.GetRequiredService<IOptions<object>>()); Assert.NotNull(host.ApplicationServices.GetRequiredService<IOptions<object>>());
} }
[Fact] [Fact]
public void EnvDefaultsToDevelopmentIfNoConfig() public void EnvDefaultsToDevelopmentIfNoConfig()
{ {
var engine = WebHost.CreateEngine(new Configuration()); var engine = CreateBuilder().Build();
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>(); var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Development", env.EnvironmentName); Assert.Equal("Development", env.EnvironmentName);
} }
[Fact] [Fact]
public void EnvDefaultsToDevelopmentConfigValueIfSpecified() public void EnvDefaultsToDevelopmentConfigValueIfSpecifiedWithOldKey()
{ {
var vals = new Dictionary<string, string> var vals = new Dictionary<string, string>
{ {
@ -107,7 +142,23 @@ namespace Microsoft.AspNet.Hosting
var config = new Configuration() var config = new Configuration()
.Add(new MemoryConfigurationSource(vals)); .Add(new MemoryConfigurationSource(vals));
var engine = WebHost.CreateEngine(config); var engine = CreateBuilder(config).Build();
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Staging", env.EnvironmentName);
}
[Fact]
public void EnvDefaultsToDevelopmentConfigValueIfSpecified()
{
var vals = new Dictionary<string, string>
{
{ "Hosting:Environment", "Staging" }
};
var config = new Configuration()
.Add(new MemoryConfigurationSource(vals));
var engine = CreateBuilder(config).Build();
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>(); var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Staging", env.EnvironmentName); Assert.Equal("Staging", env.EnvironmentName);
} }
@ -115,7 +166,7 @@ namespace Microsoft.AspNet.Hosting
[Fact] [Fact]
public void WebRootCanBeResolvedFromTheProjectJson() public void WebRootCanBeResolvedFromTheProjectJson()
{ {
var engine = WebHost.CreateEngine().UseServer(this); var engine = CreateBuilder().UseServer(this).Build();
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>(); var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath); Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath);
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists); Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
@ -124,8 +175,7 @@ namespace Microsoft.AspNet.Hosting
[Fact] [Fact]
public void IsEnvironment_Extension_Is_Case_Insensitive() public void IsEnvironment_Extension_Is_Case_Insensitive()
{ {
var engine = WebHost.CreateEngine().UseServer(this); var engine = CreateBuilder().UseServer(this).Build();
using (engine.Start()) using (engine.Start())
{ {
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>(); var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
@ -164,8 +214,7 @@ namespace Microsoft.AspNet.Hosting
httpContext = innerHttpContext; httpContext = innerHttpContext;
return Task.FromResult(0); return Task.FromResult(0);
}); });
var featuresSupportedByHost = new FeatureCollection(); var hostingEngine = CreateHostingEngine(requestDelegate);
var hostingEngine = CreateHostingEngine(featuresSupportedByHost, requestDelegate);
// Act // Act
var disposable = hostingEngine.Start(); var disposable = hostingEngine.Start();
@ -192,7 +241,9 @@ namespace Microsoft.AspNet.Hosting
featuresSupportedByHost featuresSupportedByHost
.Setup(fc => fc.Add(new KeyValuePair<Type, object>(It.IsAny<Type>(), It.IsAny<object>()))) .Setup(fc => fc.Add(new KeyValuePair<Type, object>(It.IsAny<Type>(), It.IsAny<object>())))
.Throws(new NotImplementedException()); .Throws(new NotImplementedException());
var hostingEngine = CreateHostingEngine(featuresSupportedByHost.Object, requestDelegate); _featuresSupportedByThisHost = featuresSupportedByHost.Object;
var hostingEngine = CreateHostingEngine(requestDelegate);
// Act // Act
var disposable = hostingEngine.Start(); var disposable = hostingEngine.Start();
@ -212,10 +263,9 @@ namespace Microsoft.AspNet.Hosting
httpContext = innerHttpContext; httpContext = innerHttpContext;
return Task.FromResult(0); return Task.FromResult(0);
}); });
var featuresSupportedByHost = new FeatureCollection();
var requestIdentifierFeature = new Mock<IRequestIdentifierFeature>().Object; var requestIdentifierFeature = new Mock<IRequestIdentifierFeature>().Object;
featuresSupportedByHost.Add(typeof(IRequestIdentifierFeature), requestIdentifierFeature); _featuresSupportedByThisHost.Add(typeof(IRequestIdentifierFeature), requestIdentifierFeature);
var hostingEngine = CreateHostingEngine(featuresSupportedByHost, requestDelegate); var hostingEngine = CreateHostingEngine(requestDelegate);
// Act // Act
var disposable = hostingEngine.Start(); var disposable = hostingEngine.Start();
@ -225,9 +275,7 @@ namespace Microsoft.AspNet.Hosting
Assert.Same(requestIdentifierFeature, httpContext.GetFeature<IRequestIdentifierFeature>()); Assert.Same(requestIdentifierFeature, httpContext.GetFeature<IRequestIdentifierFeature>());
} }
private IHostingEngine CreateHostingEngine( private IHostingEngine CreateHostingEngine(RequestDelegate requestDelegate)
IFeatureCollection featuresSupportedByHost,
RequestDelegate requestDelegate)
{ {
var applicationBuilder = new Mock<IApplicationBuilder>(); var applicationBuilder = new Mock<IApplicationBuilder>();
applicationBuilder.Setup(appBuilder => appBuilder.Build()).Returns(requestDelegate); applicationBuilder.Setup(appBuilder => appBuilder.Build()).Returns(requestDelegate);
@ -236,36 +284,25 @@ namespace Microsoft.AspNet.Hosting
.Setup(abf => abf.CreateBuilder(It.IsAny<object>())) .Setup(abf => abf.CreateBuilder(It.IsAny<object>()))
.Returns(applicationBuilder.Object); .Returns(applicationBuilder.Object);
var serviceCollection = new ServiceCollection(); var host = CreateBuilder()
serviceCollection.Add( .UseServer(this)
new ServiceDescriptor(typeof(IApplicationBuilderFactory), applicationBuilderFactory.Object)); .UseServices(s =>
serviceCollection.Add( {
new ServiceDescriptor(typeof(ILogger<HostingEngine>), new Mock<ILogger<HostingEngine>>().Object)); s.AddInstance(applicationBuilderFactory.Object);
serviceCollection.Add( s.AddInstance(new Mock<ILogger<HostingEngine>>().Object);
new ServiceDescriptor(typeof(IHttpContextFactory), new HttpContextFactory())); s.AddSingleton<IHttpContextFactory, HttpContextFactory>();
serviceCollection.Add( s.AddInstance(new Mock<IHttpContextAccessor>().Object);
new ServiceDescriptor(typeof(IHttpContextAccessor), new Mock<IHttpContextAccessor>().Object)); s.AddInstance(new Mock<IHostingEnvironment>().Object);
})
var startupLoader = new Mock<IStartupLoader>(); .UseStartup(
var startupMethods = new StartupMethods( appBuilder => { },
(appBuilder) => { }, configureServices => configureServices.BuildServiceProvider());
(configureServices) => configureServices.BuildServiceProvider()); return host.Build();
startupLoader.Setup(sl => sl.Load(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<List<string>>()))
.Returns(startupMethods);
var hostingEngine = new HostingEngine(
serviceCollection,
startupLoader.Object,
new Mock<IConfiguration>().Object,
new Mock<IHostingEnvironment>().Object,
"TestAppName");
return hostingEngine.UseServer(new TestServerFactory(featuresSupportedByHost));
} }
private void RunMapPath(string virtualPath, string expectedSuffix) private void RunMapPath(string virtualPath, string expectedSuffix)
{ {
var engine = WebHost.CreateEngine().UseServer(this); var engine = CreateBuilder().UseServer(this).Build();
using (engine.Start()) using (engine.Start())
{ {
@ -276,6 +313,11 @@ namespace Microsoft.AspNet.Hosting
} }
} }
private WebHostBuilder CreateBuilder(IConfiguration config = null)
{
return new WebHostBuilder(CallContextServiceLocator.Locator.ServiceProvider, config ?? new Configuration());
}
public IServerInformation Initialize(IConfiguration configuration) public IServerInformation Initialize(IConfiguration configuration)
{ {
return null; return null;
@ -285,6 +327,7 @@ namespace Microsoft.AspNet.Hosting
{ {
var startInstance = new StartInstance(application); var startInstance = new StartInstance(application);
_startInstances.Add(startInstance); _startInstances.Add(startInstance);
application(_featuresSupportedByThisHost);
return startInstance; return startInstance;
} }
@ -307,39 +350,15 @@ namespace Microsoft.AspNet.Hosting
private class TestLoader : IStartupLoader private class TestLoader : IStartupLoader
{ {
public StartupMethods Load(string startupAssemblyName, string environmentName, IList<string> diagnosticMessages) public Type FindStartupType(string startupAssemblyName, IList<string> diagnosticMessages)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
}
private class TestEngineFactory : IHostingFactory public StartupMethods LoadMethods(Type startupType, IList<string> diagnosticMessages)
{
public IHostingEngine Create(IConfiguration config)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
private class TestServerFactory : IServerFactory
{
private readonly IFeatureCollection _featuresSupportedByThisHost;
public TestServerFactory(IFeatureCollection featuresSupportedByThisHost)
{
_featuresSupportedByThisHost = featuresSupportedByThisHost;
}
public IServerInformation Initialize(IConfiguration configuration)
{
return null;
}
public IDisposable Start(IServerInformation serverInformation, Func<IFeatureCollection, Task> application)
{
application(_featuresSupportedByThisHost).Wait();
return null;
}
}
} }
} }

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection; using System.Reflection;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Fakes; using Microsoft.AspNet.Hosting.Fakes;
@ -26,7 +25,10 @@ namespace Microsoft.AspNet.Hosting.Tests
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>(); var diagnosticMessages = new List<string>();
var startup = new StartupLoader(services).Load("Microsoft.AspNet.Hosting.Tests", "WithServices", diagnosticMessages); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithServices" };
var loader = new StartupLoader(services, hostingEnv);
var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages);
var startup = loader.LoadMethods(type, diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -47,9 +49,12 @@ namespace Microsoft.AspNet.Hosting.Tests
public void StartupClassAddsConfigureServicesToApplicationServices(string environment) public void StartupClassAddsConfigureServicesToApplicationServices(string environment)
{ {
var services = new ServiceCollection().BuildServiceProvider(); var services = new ServiceCollection().BuildServiceProvider();
var diagnosticMesssages = new List<string>();
var startup = new StartupLoader(services).Load("Microsoft.AspNet.Hosting.Tests", environment ?? "", diagnosticMesssages); var diagnosticMessages = new List<string>();
var hostingEnv = new HostingEnvironment { EnvironmentName = environment };
var loader = new StartupLoader(services, hostingEnv);
var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages);
var startup = loader.LoadMethods(type, diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection());
@ -67,9 +72,13 @@ namespace Microsoft.AspNet.Hosting.Tests
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IFakeStartupCallback>(this); serviceCollection.AddInstance<IFakeStartupCallback>(this);
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>();
var ex = Assert.Throws<InvalidOperationException>(() => new StartupLoader(services).Load("Microsoft.AspNet.Hosting.Tests", "Boom", diagnosticMessages)); var diagnosticMessages = new List<string>();
var hostingEnv = new HostingEnvironment { EnvironmentName = "Boom" };
var loader = new StartupLoader(services, hostingEnv);
var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages);
var ex = Assert.Throws<InvalidOperationException>(() => loader.LoadMethods(type, diagnosticMessages));
Assert.Equal("A method named 'ConfigureBoom' or 'Configure' in the type 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' could not be found.", ex.Message); Assert.Equal("A method named 'ConfigureBoom' or 'Configure' in the type 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' could not be found.", ex.Message);
} }
@ -80,7 +89,10 @@ namespace Microsoft.AspNet.Hosting.Tests
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>(); var diagnosticMessages = new List<string>();
var startup = new StartupLoader(services).Load("Microsoft.AspNet.Hosting.Tests", "WithNullConfigureServices", diagnosticMessages); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithNullConfigureServices" };
var loader = new StartupLoader(services, hostingEnv);
var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages);
var startup = loader.LoadMethods(type, diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection());
@ -96,7 +108,10 @@ namespace Microsoft.AspNet.Hosting.Tests
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>(); var diagnosticMessages = new List<string>();
var startup = new StartupLoader(services).Load("Microsoft.AspNet.Hosting.Tests", "WithConfigureServices", diagnosticMessages); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithConfigureServices" };
var loader = new StartupLoader(services, hostingEnv);
var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages);
var startup = loader.LoadMethods(type, diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -113,7 +128,9 @@ namespace Microsoft.AspNet.Hosting.Tests
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>(); var diagnosticMessages = new List<string>();
var startup = new StartupLoader(services).Load(typeof(TestStartup), "", diagnosticMessages); var hostingEnv = new HostingEnvironment();
var loader = new StartupLoader(services, hostingEnv);
var startup = loader.LoadMethods(typeof(TestStartup), diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -130,7 +147,9 @@ namespace Microsoft.AspNet.Hosting.Tests
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var diagnosticMessages = new List<string>(); var diagnosticMessages = new List<string>();
var startup = new StartupLoader(services).Load(typeof(TestStartup), "No", diagnosticMessages); var hostingEnv = new HostingEnvironment { EnvironmentName = "No" };
var loader = new StartupLoader(services, hostingEnv);
var startup = loader.LoadMethods(typeof(TestStartup), diagnosticMessages);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);

View File

@ -2,20 +2,16 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Runtime.Versioning;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure; using Microsoft.Framework.Runtime.Infrastructure;
using Xunit; using Xunit;
@ -56,54 +52,6 @@ namespace Microsoft.AspNet.TestHost
Assert.Equal("RequestServices:True", result); Assert.Equal("RequestServices:True", result);
} }
[Fact]
public async Task CanChangeApplicationName()
{
var fallbackServices = CallContextServiceLocator.Locator.ServiceProvider;
var appName = "gobblegobble";
var builder = TestServer.CreateBuilder(fallbackServices, new Configuration(),
app =>
{
app.Run(context =>
{
var appEnv = app.ApplicationServices.GetRequiredService<IApplicationEnvironment>();
return context.Response.WriteAsync("AppName:" + appEnv.ApplicationName);
});
},
configureServices: null);
builder.ApplicationName = appName;
var server = builder.Build();
string result = await server.CreateClient().GetStringAsync("/path");
Assert.Equal("AppName:" + appName, result);
}
[Fact]
public async Task CanChangeAppPath()
{
var fallbackServices = CallContextServiceLocator.Locator.ServiceProvider;
var appPath = ".";
var builder = TestServer.CreateBuilder(fallbackServices, new Configuration(),
app =>
{
app.Run(context =>
{
var env = app.ApplicationServices.GetRequiredService<IApplicationEnvironment>();
return context.Response.WriteAsync("AppPath:" + env.ApplicationBasePath);
});
},
configureServices: null);
builder.ApplicationBasePath = appPath;
var server = builder.Build();
string result = await server.CreateClient().GetStringAsync("/path");
Assert.Equal("AppPath:" + appPath, result);
}
[Fact] [Fact]
public async Task CanAccessLogger() public async Task CanAccessLogger()
{ {
@ -242,7 +190,7 @@ namespace Microsoft.AspNet.TestHost
[Fact] [Fact]
public async Task CanCreateViaStartupType() public async Task CanCreateViaStartupType()
{ {
TestServer server = TestServer.Create<TestStartup>(); TestServer server = new TestServer(TestServer.CreateBuilder().UseStartup<TestStartup>());
HttpResponseMessage result = await server.CreateClient().GetAsync("/"); HttpResponseMessage result = await server.CreateClient().GetAsync("/");
Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("FoundService:True", await result.Content.ReadAsStringAsync()); Assert.Equal("FoundService:True", await result.Content.ReadAsStringAsync());
@ -251,9 +199,9 @@ namespace Microsoft.AspNet.TestHost
[Fact] [Fact]
public async Task CanCreateViaStartupTypeAndSpecifyEnv() public async Task CanCreateViaStartupTypeAndSpecifyEnv()
{ {
var builder = TestServer.CreateBuilder<TestStartup>(); TestServer server = new TestServer(TestServer.CreateBuilder()
builder.Environment = "Foo"; .UseStartup<TestStartup>()
var server = builder.Build(); .UseEnvironment("Foo"));
HttpResponseMessage result = await server.CreateClient().GetAsync("/"); HttpResponseMessage result = await server.CreateClient().GetAsync("/");
Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("FoundFoo:False", await result.Content.ReadAsStringAsync()); Assert.Equal("FoundFoo:False", await result.Content.ReadAsStringAsync());