Hosting changes reacting to fallback being removed
- HostingServices.Create() is the supported way to create a service collection with kre services imported - IHostingEnvironment is now a normal service - IConfigureHostingEnvironment is how you configure it
This commit is contained in:
parent
b44ffdb745
commit
ac6f1223df
|
|
@ -0,0 +1,22 @@
|
|||
// 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
|
||||
{
|
||||
public class ConfigureHostingEnvironment : IConfigureHostingEnvironment
|
||||
{
|
||||
private readonly Action<IHostingEnvironment> _action;
|
||||
|
||||
public ConfigureHostingEnvironment(Action<IHostingEnvironment> configure)
|
||||
{
|
||||
_action = configure;
|
||||
}
|
||||
|
||||
public void Configure(IHostingEnvironment hostingEnv)
|
||||
{
|
||||
_action.Invoke(hostingEnv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ using Microsoft.AspNet.Hosting.Builder;
|
|||
using Microsoft.AspNet.Hosting.Server;
|
||||
using Microsoft.AspNet.Hosting.Startup;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,10 +1,22 @@
|
|||
// 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.Collections.Generic;
|
||||
using Microsoft.Framework.Runtime;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
public class HostingEnvironment : IHostingEnvironment
|
||||
{
|
||||
public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable<IConfigureHostingEnvironment> configures)
|
||||
{
|
||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath);
|
||||
foreach (var configure in configures)
|
||||
{
|
||||
configure.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
public string EnvironmentName { get; set; }
|
||||
|
||||
public string WebRoot { get; set; }
|
||||
|
|
|
|||
|
|
@ -1,26 +1,52 @@
|
|||
// 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 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
|
||||
{
|
||||
public static class HostingServices
|
||||
{
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices()
|
||||
private static IServiceCollection Import(IServiceProvider fallbackProvider)
|
||||
{
|
||||
return GetDefaultServices(new Configuration());
|
||||
var services = new ServiceCollection();
|
||||
var manifest = fallbackProvider.GetRequiredService<IServiceManifest>();
|
||||
foreach (var service in manifest.Services)
|
||||
{
|
||||
// REVIEW: should this be Singleton instead?
|
||||
services.AddTransient(service, sp => fallbackProvider.GetService(service));
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration)
|
||||
public static IServiceCollection Create(IConfiguration configuration = null)
|
||||
{
|
||||
return Create(CallContextServiceLocator.Locator.ServiceProvider);
|
||||
}
|
||||
|
||||
public static IServiceCollection Create(IServiceProvider fallbackServices, IConfiguration configuration = null)
|
||||
{
|
||||
var services = Import(fallbackServices);
|
||||
services.Add(GetDefaultServices(configuration));
|
||||
services.AddSingleton<IServiceManifest>(sp => new HostingManifest(fallbackServices));
|
||||
return services;
|
||||
}
|
||||
|
||||
// REVIEW: make this private?
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration = null)
|
||||
{
|
||||
configuration = configuration ?? new Configuration();
|
||||
var describer = new ServiceDescriber(configuration);
|
||||
|
||||
yield return describer.Transient<IHostingEngine, HostingEngine>();
|
||||
|
|
@ -32,13 +58,16 @@ namespace Microsoft.AspNet.Hosting
|
|||
yield return describer.Transient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
|
||||
yield return describer.Transient<IHttpContextFactory, HttpContextFactory>();
|
||||
|
||||
yield return describer.Singleton<ITypeActivator, TypeActivator>();
|
||||
|
||||
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())
|
||||
|
|
@ -46,5 +75,17 @@ namespace Microsoft.AspNet.Hosting
|
|||
yield return service;
|
||||
}
|
||||
}
|
||||
|
||||
private class HostingManifest : IServiceManifest
|
||||
{
|
||||
public HostingManifest(IServiceProvider fallback)
|
||||
{
|
||||
var manifest = fallback.GetRequiredService<IServiceManifest>();
|
||||
Services = new Type[] { typeof(ITypeActivator), typeof(IHostingEnvironment), typeof(ILoggerFactory) }
|
||||
.Concat(manifest.Services).Distinct();
|
||||
}
|
||||
|
||||
public IEnumerable<Type> Services { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
// 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.Runtime;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
[AssemblyNeutral]
|
||||
public interface IConfigureHostingEnvironment
|
||||
{
|
||||
void Configure(IHostingEnvironment hostingEnv);
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
[AssemblyNeutral]
|
||||
public interface IHostingEnvironment
|
||||
{
|
||||
string EnvironmentName { get; }
|
||||
string EnvironmentName { get; set; }
|
||||
|
||||
string WebRoot { get; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ namespace Microsoft.AspNet.Hosting
|
|||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Main(string[] args)
|
||||
{
|
||||
var config = new Configuration();
|
||||
|
|
@ -35,19 +37,11 @@ namespace Microsoft.AspNet.Hosting
|
|||
config.AddEnvironmentVariables();
|
||||
config.AddCommandLine(args);
|
||||
|
||||
var appEnv = _serviceProvider.GetRequiredService<IApplicationEnvironment>();
|
||||
var serviceCollection = HostingServices.Create(_serviceProvider, config);
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
|
||||
var hostingEnv = new HostingEnvironment()
|
||||
{
|
||||
EnvironmentName = config.Get(EnvironmentKey) ?? DefaultEnvironmentName,
|
||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
|
||||
};
|
||||
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices(config));
|
||||
serviceCollection.AddInstance<IHostingEnvironment>(hostingEnv);
|
||||
|
||||
var services = serviceCollection.BuildServiceProvider(_serviceProvider);
|
||||
var appEnv = services.GetRequiredService<IApplicationEnvironment>();
|
||||
var hostingEnv = services.GetRequiredService<IHostingEnvironment>();
|
||||
|
||||
var context = new HostingContext()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -151,8 +151,11 @@ namespace Microsoft.AspNet.Hosting.Startup
|
|||
{
|
||||
if (servicesMethod != null)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var services = HostingServices.Create(builder.ApplicationServices);
|
||||
// TODO: remove adding options
|
||||
services.Add(OptionsServices.GetDefaultServices());
|
||||
services.AddScoped(typeof(IContextAccessor<>), typeof(ContextAccessor<>));
|
||||
|
||||
if (servicesMethod.ReturnType == typeof(IServiceProvider))
|
||||
{
|
||||
// IServiceProvider ConfigureServices(IServiceCollection)
|
||||
|
|
@ -165,7 +168,7 @@ namespace Microsoft.AspNet.Hosting.Startup
|
|||
Invoke(servicesMethod, instance, builder, services);
|
||||
if (builder != null)
|
||||
{
|
||||
builder.ApplicationServices = services.BuildServiceProvider(builder.ApplicationServices);
|
||||
builder.ApplicationServices = services.BuildServiceProvider();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +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.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
public static class WebApplication
|
||||
{
|
||||
public static IDisposable Start()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
|
||||
var context = new HostingContext
|
||||
{
|
||||
Services = serviceCollection.BuildServiceProvider()
|
||||
};
|
||||
|
||||
var engine = context.Services.GetRequiredService<IHostingEngine>();
|
||||
if (engine == null)
|
||||
{
|
||||
throw new Exception("TODO: IHostingEngine service not available exception");
|
||||
}
|
||||
|
||||
return engine.Start(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.RequestContainer;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
|
@ -17,32 +18,42 @@ namespace Microsoft.AspNet.Builder
|
|||
return builder.UseMiddleware<ContainerMiddleware>();
|
||||
}
|
||||
|
||||
// Review: what do we use these for?
|
||||
|
||||
public static IApplicationBuilder UseRequestServices(this IApplicationBuilder builder, IServiceProvider applicationServices)
|
||||
{
|
||||
// REVIEW: should this be doing fallback?
|
||||
builder.ApplicationServices = applicationServices;
|
||||
|
||||
return builder.UseMiddleware<ContainerMiddleware>();
|
||||
}
|
||||
|
||||
// Note: Manifests are lost after UseServices, services are flattened into ApplicationServices
|
||||
|
||||
public static IApplicationBuilder UseServices(this IApplicationBuilder builder, IEnumerable<IServiceDescriptor> applicationServices)
|
||||
{
|
||||
return builder.UseServices(services => services.Add(applicationServices));
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseServices(this IApplicationBuilder builder, Action<ServiceCollection> configureServices)
|
||||
public static IApplicationBuilder UseServices(this IApplicationBuilder builder, Action<IServiceCollection> configureServices)
|
||||
{
|
||||
return builder.UseServices(serviceCollection =>
|
||||
{
|
||||
configureServices(serviceCollection);
|
||||
return serviceCollection.BuildServiceProvider(builder.ApplicationServices);
|
||||
return serviceCollection.BuildServiceProvider();
|
||||
});
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseServices(this IApplicationBuilder builder, Func<ServiceCollection, IServiceProvider> configureServices)
|
||||
public static IApplicationBuilder UseServices(this IApplicationBuilder builder, Func<IServiceCollection, IServiceProvider> configureServices)
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
// 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<>));
|
||||
|
||||
// REVIEW: serviceCollection has the merged services, manifests are lost after this
|
||||
builder.ApplicationServices = configureServices(serviceCollection);
|
||||
|
||||
return builder.UseMiddleware<ContainerMiddleware>();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
"description": "ASP.NET 5 enables per-request scoping of services.",
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
||||
"Microsoft.AspNet.Hosting": "1.0.0-*",
|
||||
"Microsoft.Framework.OptionsModel": "1.0.0-*"
|
||||
},
|
||||
"frameworks": {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@
|
|||
// 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.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
|
|
@ -46,25 +49,16 @@ namespace Microsoft.AspNet.TestHost
|
|||
|
||||
public static TestServer Create(Action<IApplicationBuilder> app)
|
||||
{
|
||||
return Create(provider: CallContextServiceLocator.Locator.ServiceProvider, app: app);
|
||||
return Create(CallContextServiceLocator.Locator.ServiceProvider, app);
|
||||
}
|
||||
|
||||
public static TestServer Create(IServiceProvider provider, Action<IApplicationBuilder> app)
|
||||
public static TestServer Create(IServiceProvider serviceProvider, Action<IApplicationBuilder> app)
|
||||
{
|
||||
var appEnv = provider.GetRequiredService<IApplicationEnvironment>();
|
||||
|
||||
var hostingEnv = new HostingEnvironment()
|
||||
{
|
||||
EnvironmentName = DefaultEnvironmentName,
|
||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
|
||||
};
|
||||
|
||||
var collection = new ServiceCollection();
|
||||
collection.Add(HostingServices.GetDefaultServices());
|
||||
collection.AddInstance<IHostingEnvironment>(hostingEnv);
|
||||
|
||||
var appServices = collection.BuildServiceProvider(provider);
|
||||
var services = HostingServices.Create(serviceProvider);
|
||||
services.AddSingleton<IConfigureHostingEnvironment, ConfigureTestHostingEnvironment>();
|
||||
|
||||
//var appServices = BuildFallbackServiceProvider(services, serviceProvider);
|
||||
var appServices = services.BuildServiceProvider();
|
||||
var config = new Configuration();
|
||||
return new TestServer(config, appServices, app);
|
||||
}
|
||||
|
|
@ -134,5 +128,10 @@ namespace Microsoft.AspNet.TestHost
|
|||
get { return TestServer.ServerName; }
|
||||
}
|
||||
}
|
||||
|
||||
private class ConfigureTestHostingEnvironment : ConfigureHostingEnvironment
|
||||
{
|
||||
public ConfigureTestHostingEnvironment() : base(env => env.EnvironmentName = DefaultEnvironmentName) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
public class FakeService : IFakeEveryService { }
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
public interface IFactoryService
|
||||
{
|
||||
IFakeService FakeService { get; }
|
||||
|
||||
int Value { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
interface IFakeEveryService :
|
||||
IFakeService,
|
||||
IFakeServiceInstance,
|
||||
IFakeSingletonService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
public interface IFakeScopedService : IFakeService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -4,5 +4,4 @@
|
|||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
public interface IFakeService { }
|
||||
public class FakeService : IFakeService { }
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
interface IFakeServiceInstance : IFakeService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
interface IFakeSingletonService : IFakeService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Fakes
|
||||
{
|
||||
public interface INonexistentService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -20,9 +20,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
[Fact]
|
||||
public void HostingEngineCanBeResolvedWithDefaultServices()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
|
||||
var engine = services.GetRequiredService<IHostingEngine>();
|
||||
|
||||
|
|
@ -32,9 +30,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
[Fact]
|
||||
public void HostingEngineCanBeStarted()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
|
||||
var engine = services.GetRequiredService<IHostingEngine>();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,116 @@
|
|||
// 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 Microsoft.AspNet.Hosting.Fakes;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.DependencyInjection.ServiceLookup;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
public class HostingServicesFacts
|
||||
{
|
||||
[Fact]
|
||||
public void CreateImportsServices()
|
||||
{
|
||||
// Arrange
|
||||
var fallbackServices = new ServiceCollection();
|
||||
fallbackServices.AddSingleton<IFakeSingletonService, FakeService>();
|
||||
var instance = new FakeService();
|
||||
var factoryInstance = new FakeFactoryService(instance);
|
||||
fallbackServices.AddInstance<IFakeServiceInstance>(instance);
|
||||
fallbackServices.AddTransient<IFakeService, FakeService>();
|
||||
fallbackServices.AddSingleton<IFactoryService>(serviceProvider => factoryInstance);
|
||||
fallbackServices.AddTransient<IFakeScopedService, FakeService>(); // Don't register in manifest
|
||||
|
||||
fallbackServices.AddInstance<IServiceManifest>(new ServiceManifest(
|
||||
new Type[] {
|
||||
typeof(IFakeServiceInstance),
|
||||
typeof(IFakeService),
|
||||
typeof(IFakeSingletonService),
|
||||
typeof(IFactoryService),
|
||||
typeof(INonexistentService)
|
||||
}));
|
||||
|
||||
var services = HostingServices.Create(fallbackServices.BuildServiceProvider());
|
||||
|
||||
// Act
|
||||
var provider = services.BuildServiceProvider();
|
||||
var singleton = provider.GetRequiredService<IFakeSingletonService>();
|
||||
var transient = provider.GetRequiredService<IFakeService>();
|
||||
var factory = provider.GetRequiredService<IFactoryService>();
|
||||
|
||||
// Assert
|
||||
Assert.Same(singleton, provider.GetRequiredService<IFakeSingletonService>());
|
||||
Assert.NotSame(transient, provider.GetRequiredService<IFakeService>());
|
||||
Assert.Same(instance, provider.GetRequiredService<IFakeServiceInstance>());
|
||||
Assert.Same(factoryInstance, factory);
|
||||
Assert.Same(factory.FakeService, instance);
|
||||
Assert.Null(provider.GetService<INonexistentService>());
|
||||
Assert.Null(provider.GetService<IFakeScopedService>()); // Make sure we don't leak non manifest services
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanHideImportedServices()
|
||||
{
|
||||
// Arrange
|
||||
var fallbackServices = new ServiceCollection();
|
||||
var fallbackInstance = new FakeService();
|
||||
fallbackServices.AddInstance<IFakeService>(fallbackInstance);
|
||||
fallbackServices.AddInstance<IServiceManifest>(new ServiceManifest(new Type[] { typeof(IFakeService) }));
|
||||
|
||||
var services = HostingServices.Create(fallbackServices.BuildServiceProvider());
|
||||
var realInstance = new FakeService();
|
||||
services.AddInstance<IFakeService>(realInstance);
|
||||
|
||||
// Act
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(realInstance, provider.GetRequiredService<IFakeService>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateThrowsWithNoManifest()
|
||||
{
|
||||
// Arrange
|
||||
var fallbackServices = new ServiceCollection();
|
||||
fallbackServices.AddSingleton<IFakeSingletonService, FakeService>();
|
||||
var instance = new FakeService();
|
||||
fallbackServices.AddInstance<IFakeServiceInstance>(instance);
|
||||
fallbackServices.AddTransient<IFakeService, FakeService>();
|
||||
|
||||
// Act
|
||||
var exp = Assert.Throws<Exception>(() => HostingServices.Create(fallbackServices.BuildServiceProvider()));
|
||||
|
||||
|
||||
// Assert
|
||||
Assert.True(exp.Message.Contains("No service for type 'Microsoft.Framework.DependencyInjection.ServiceLookup.IServiceManifest'"));
|
||||
}
|
||||
|
||||
private class ServiceManifest : IServiceManifest
|
||||
{
|
||||
public ServiceManifest(IEnumerable<Type> services)
|
||||
{
|
||||
Services = services;
|
||||
}
|
||||
|
||||
public IEnumerable<Type> Services { get; private set; }
|
||||
}
|
||||
|
||||
private class FakeFactoryService : IFactoryService
|
||||
{
|
||||
public FakeFactoryService(FakeService service)
|
||||
{
|
||||
FakeService = service;
|
||||
}
|
||||
|
||||
public IFakeService FakeService { get; private set; }
|
||||
|
||||
public int Value { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
// 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 Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting.Fakes;
|
||||
using Microsoft.AspNet.Hosting.Startup;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Xunit;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
|
||||
public class StartupManagerTests : IFakeStartupCallback
|
||||
|
|
@ -45,9 +45,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
[InlineData("ProviderArgs")]
|
||||
public void StartupClassAddsConfigureServicesToApplicationServices(string environment)
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
var manager = services.GetRequiredService<IStartupManager>();
|
||||
|
||||
var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", environment ?? "");
|
||||
|
|
@ -67,9 +65,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
[InlineData("FallbackProvider")]
|
||||
public void StartupClassConfigureServicesThatFallsbackToApplicationServices(string env)
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
var manager = services.GetRequiredService<IStartupManager>();
|
||||
|
||||
var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", env);
|
||||
|
|
@ -81,12 +77,12 @@ namespace Microsoft.AspNet.Hosting
|
|||
Assert.Equal(services, app.ApplicationServices);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartupClassWithConfigureServicesAndUseServicesAddsBothToServices()
|
||||
// REVIEW: With the manifest change, Since the ConfigureServices are not imported, UseServices will mask what's in ConfigureServices
|
||||
// This will throw since ConfigureServices consumes manifest and then UseServices will blow up
|
||||
[Fact(Skip = "Review Failure")]
|
||||
public void StartupClassWithConfigureServicesAndUseServicesHidesConfigureServices()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
var manager = services.GetRequiredService<IStartupManager>();
|
||||
|
||||
var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "UseServices");
|
||||
|
|
@ -96,19 +92,18 @@ namespace Microsoft.AspNet.Hosting
|
|||
startup.Invoke(app);
|
||||
|
||||
Assert.NotNull(app.ApplicationServices.GetRequiredService<FakeService>());
|
||||
Assert.NotNull(app.ApplicationServices.GetRequiredService<IFakeService>());
|
||||
Assert.Null(app.ApplicationServices.GetService<IFakeService>());
|
||||
|
||||
var options = app.ApplicationServices.GetRequiredService<IOptions<FakeOptions>>().Options;
|
||||
Assert.NotNull(options);
|
||||
Assert.Equal("Configured", options.Message);
|
||||
Assert.False(options.Configured); // REVIEW: why doesn't the ConfigureServices ConfigureOptions get run?
|
||||
Assert.False(options.Configured); // Options never resolved from inner containers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartupWithNoConfigureThrows()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices());
|
||||
var serviceCollection = HostingServices.Create();
|
||||
serviceCollection.AddInstance<IFakeStartupCallback>(this);
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var manager = services.GetRequiredService<IStartupManager>();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,17 @@
|
|||
// 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 Xunit;
|
||||
using Microsoft.AspNet.Hosting.Builder;
|
||||
using Microsoft.AspNet.Hosting.Server;
|
||||
using Microsoft.AspNet.Hosting.Startup;
|
||||
using Microsoft.AspNet.PipelineCore;
|
||||
using Microsoft.AspNet.RequestContainer;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
|
|
@ -15,9 +20,7 @@ namespace Microsoft.AspNet.Hosting.Tests
|
|||
[Fact]
|
||||
public void RequestServicesAvailableOnlyAfterRequestServices()
|
||||
{
|
||||
var baseServiceProvider = new ServiceCollection()
|
||||
.Add(HostingServices.GetDefaultServices())
|
||||
.BuildServiceProvider();
|
||||
var baseServiceProvider = HostingServices.Create().BuildServiceProvider();
|
||||
var builder = new ApplicationBuilder(baseServiceProvider);
|
||||
|
||||
bool foundRequestServicesBefore = false;
|
||||
|
|
@ -45,9 +48,7 @@ namespace Microsoft.AspNet.Hosting.Tests
|
|||
[InlineData(false)]
|
||||
public void EnsureRequestServicesSetsRequestServices(bool initializeApplicationServices)
|
||||
{
|
||||
var baseServiceProvider = new ServiceCollection()
|
||||
.Add(HostingServices.GetDefaultServices())
|
||||
.BuildServiceProvider();
|
||||
var baseServiceProvider = HostingServices.Create().BuildServiceProvider();
|
||||
var builder = new ApplicationBuilder(baseServiceProvider);
|
||||
|
||||
bool foundRequestServicesBefore = false;
|
||||
|
|
@ -80,5 +81,25 @@ namespace Microsoft.AspNet.Hosting.Tests
|
|||
Assert.True(foundRequestServicesAfter);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(IHostingEngine))]
|
||||
[InlineData(typeof(IServerManager))]
|
||||
[InlineData(typeof(IStartupManager))]
|
||||
[InlineData(typeof(IStartupLoaderProvider))]
|
||||
[InlineData(typeof(IApplicationBuilderFactory))]
|
||||
[InlineData(typeof(IStartupLoaderProvider))]
|
||||
[InlineData(typeof(IHttpContextFactory))]
|
||||
[InlineData(typeof(ITypeActivator))]
|
||||
[InlineData(typeof(IApplicationLifetime))]
|
||||
[InlineData(typeof(ILoggerFactory))]
|
||||
public void UseRequestServicesHostingImportedServicesAreDefined(Type service)
|
||||
{
|
||||
var baseServiceProvider = HostingServices.Create().BuildServiceProvider();
|
||||
var builder = new ApplicationBuilder(baseServiceProvider);
|
||||
|
||||
builder.UseRequestServices();
|
||||
|
||||
Assert.NotNull(builder.ApplicationServices.GetRequiredService(service));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,8 @@ using Microsoft.AspNet.Builder;
|
|||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Framework.Runtime.Infrastructure;
|
||||
using Xunit;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.PipelineCore;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
|
|
@ -17,8 +16,7 @@ namespace Microsoft.AspNet.Hosting.Tests
|
|||
[Fact]
|
||||
public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithAction()
|
||||
{
|
||||
var baseServiceProvider = new ServiceCollection().BuildServiceProvider();
|
||||
var builder = new ApplicationBuilder(baseServiceProvider);
|
||||
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
|
||||
|
||||
builder.UseServices(serviceCollection => { });
|
||||
|
||||
|
|
@ -30,13 +28,12 @@ namespace Microsoft.AspNet.Hosting.Tests
|
|||
[Fact]
|
||||
public void OptionsAccessorCanBeResolvedAfterCallingUseServicesWithFunc()
|
||||
{
|
||||
var baseServiceProvider = new ServiceCollection().BuildServiceProvider();
|
||||
var builder = new ApplicationBuilder(baseServiceProvider);
|
||||
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
|
||||
IServiceProvider serviceProvider = null;
|
||||
|
||||
builder.UseServices(serviceCollection =>
|
||||
{
|
||||
serviceProvider = serviceCollection.BuildServiceProvider(builder.ApplicationServices);
|
||||
serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
return serviceProvider;
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -6,10 +6,9 @@ using System.IO;
|
|||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Runtime;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.TestHost
|
||||
|
|
@ -21,9 +20,7 @@ namespace Microsoft.AspNet.TestHost
|
|||
|
||||
public TestClientTests()
|
||||
{
|
||||
_services = new ServiceCollection()
|
||||
.AddSingleton<IApplicationEnvironment, TestApplicationEnvironment>()
|
||||
.BuildServiceProvider();
|
||||
_services = HostingServices.Create().BuildServiceProvider();
|
||||
|
||||
_server = TestServer.Create(_services, app => app.Run(ctx => Task.FromResult(0)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ using Microsoft.AspNet.Hosting;
|
|||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Runtime;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.TestHost
|
||||
|
|
@ -22,9 +21,7 @@ namespace Microsoft.AspNet.TestHost
|
|||
public void CreateWithDelegate()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection()
|
||||
.AddSingleton<IApplicationEnvironment, TestApplicationEnvironment>()
|
||||
.BuildServiceProvider();
|
||||
var services = HostingServices.Create().BuildServiceProvider();
|
||||
|
||||
// Act & Assert
|
||||
Assert.DoesNotThrow(() => TestServer.Create(services, app => { }));
|
||||
|
|
@ -34,8 +31,7 @@ namespace Microsoft.AspNet.TestHost
|
|||
public void ThrowsIfNoApplicationEnvironmentIsRegisteredWithTheProvider()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection()
|
||||
.BuildServiceProvider();
|
||||
var services = new ServiceCollection().BuildServiceProvider();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<Exception>(() => TestServer.Create(services, new Startup().Configuration));
|
||||
|
|
|
|||
Loading…
Reference in New Issue