Updated hosting to use service describer
This commit is contained in:
parent
063420067e
commit
cd9d3a3335
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.ConfigurationModel;
|
||||
using Microsoft.AspNet.DependencyInjection;
|
||||
using Microsoft.AspNet.Hosting.Builder;
|
||||
|
|
@ -18,69 +17,24 @@ namespace Microsoft.AspNet.Hosting
|
|||
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration)
|
||||
{
|
||||
yield return DescribeService<IHostingEngine, HostingEngine>(configuration);
|
||||
yield return DescribeService<IServerManager, ServerManager>(configuration);
|
||||
var describer = new ServiceDescriber(configuration);
|
||||
|
||||
yield return DescribeService<IStartupManager, StartupManager>(configuration);
|
||||
yield return DescribeService<IStartupLoaderProvider, StartupLoaderProvider>(configuration);
|
||||
yield return describer.Transient<IHostingEngine, HostingEngine>();
|
||||
yield return describer.Transient<IServerManager, ServerManager>();
|
||||
|
||||
yield return DescribeService<IBuilderFactory, BuilderFactory>(configuration);
|
||||
yield return DescribeService<IHttpContextFactory, HttpContextFactory>(configuration);
|
||||
yield return describer.Transient<IStartupManager, StartupManager>();
|
||||
yield return describer.Transient<IStartupLoaderProvider, StartupLoaderProvider>();
|
||||
|
||||
yield return DescribeService<ITypeActivator, TypeActivator>(configuration);
|
||||
yield return describer.Transient<IBuilderFactory, BuilderFactory>();
|
||||
yield return describer.Transient<IHttpContextFactory, HttpContextFactory>();
|
||||
|
||||
yield return describer.Transient<ITypeActivator, TypeActivator>();
|
||||
|
||||
// The default IDataProtectionProvider is a singleton.
|
||||
// Note: DPAPI isn't usable in IIS where the user profile hasn't been loaded, but loading DPAPI
|
||||
// is deferred until the first call to Protect / Unprotect. It's up to an IIS-based host to
|
||||
// replace this service as part of application initialization.
|
||||
yield return new ServiceDescriptor {
|
||||
ServiceType = typeof(IDataProtectionProvider),
|
||||
Lifecycle = LifecycleKind.Singleton,
|
||||
ImplementationInstance = DataProtectionProvider.CreateFromDpapi()
|
||||
};
|
||||
}
|
||||
|
||||
public static IServiceDescriptor DescribeService<TService, TImplementation>(IConfiguration configuration,
|
||||
LifecycleKind lifecycle = LifecycleKind.Transient)
|
||||
{
|
||||
return DescribeService(typeof(TService), typeof(TImplementation), configuration, lifecycle);
|
||||
}
|
||||
|
||||
public static IServiceDescriptor DescribeService(
|
||||
Type serviceType,
|
||||
Type implementationType,
|
||||
IConfiguration configuration,
|
||||
LifecycleKind lifecycle)
|
||||
{
|
||||
var serviceTypeName = serviceType.FullName;
|
||||
var implementationTypeName = configuration.Get(serviceTypeName);
|
||||
if (!String.IsNullOrEmpty(implementationTypeName))
|
||||
{
|
||||
try
|
||||
{
|
||||
implementationType = Type.GetType(implementationTypeName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(string.Format("TODO: unable to locate implementation {0} for service {1}", implementationTypeName, serviceTypeName), ex);
|
||||
}
|
||||
}
|
||||
return new ServiceTypeDescriptor(serviceType, implementationType, lifecycle);
|
||||
}
|
||||
|
||||
public class ServiceTypeDescriptor : IServiceDescriptor
|
||||
{
|
||||
public ServiceTypeDescriptor(Type serviceType, Type implementationType, LifecycleKind lifecycle)
|
||||
{
|
||||
ServiceType = serviceType;
|
||||
ImplementationType = implementationType;
|
||||
Lifecycle = lifecycle;
|
||||
}
|
||||
|
||||
public LifecycleKind Lifecycle { get; private set; }
|
||||
public Type ServiceType { get; private set; }
|
||||
public Type ImplementationType { get; private set; }
|
||||
public object ImplementationInstance { get; private set; }
|
||||
yield return describer.Instance<IDataProtectionProvider>(DataProtectionProvider.CreateFromDpapi());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue