// Copyright (c) Microsoft Open Technologies, Inc. // All Rights Reserved // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING // WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF // TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR // NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing // permissions and limitations under the License. using System; using System.Collections.Generic; using Microsoft.AspNet.Hosting.Builder; using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.Hosting.Startup; using Microsoft.AspNet.Security.DataProtection; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Hosting { public static class HostingServices { public static IEnumerable GetDefaultServices() { return GetDefaultServices(new Configuration()); } public static IEnumerable GetDefaultServices(IConfiguration configuration) { var describer = new ServiceDescriber(configuration); yield return describer.Transient(); yield return describer.Transient(); yield return describer.Transient(); yield return describer.Transient(); yield return describer.Transient(); yield return describer.Transient(); yield return describer.Transient(); // TODO: We expect this to be provide by the runtime eventually. yield return describer.Instance(new NullLoggerFactory()); yield return new ServiceDescriptor { ServiceType = typeof(IContextAccessor<>), ImplementationType = typeof(ContextAccessor<>), Lifecycle = LifecycleKind.Scoped }; // TODO: Review whether this is the right long term home, and whether Scoped is correct lifetime yield return new ServiceDescriptor { ServiceType = typeof(IOptionsAccessor<>), ImplementationType = typeof(OptionsAccessor<>), Lifecycle = LifecycleKind.Scoped }; if (PlatformHelper.IsMono) { #if NET45 yield return describer.Instance(DataProtectionProvider.CreateFromLegacyDpapi()); #endif } else { // 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 describer.Instance(DataProtectionProvider.CreateFromDpapi()); } } // TODO: Temp workaround until the runtime reliably provides logging. // If ILoggerFactory is never guaranteed, move this fallback into Microsoft.AspNet.Logging. private class NullLoggerFactory : ILoggerFactory { public ILogger Create(string name) { return new NullLogger(); } } private class NullLogger : ILogger { public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) { return false; } } } }