// 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.AspNet.Mvc.ApplicationModels; using Microsoft.AspNet.Mvc.Description; using Microsoft.AspNet.Mvc.Filters; using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor.OptionDescriptors; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Security; using Microsoft.Framework.Cache.Memory; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.NestedProviders; using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Mvc { public class MvcServices { public static IEnumerable GetDefaultServices(IConfiguration configuration = null) { var describe = new ServiceDescriber(configuration); // Options and core services. yield return describe.Transient, MvcOptionsSetup>(); yield return describe.Transient, RazorViewEngineOptionsSetup>(); yield return describe.Transient(); yield return describe.Transient(typeof(INestedProviderManager<>), typeof(NestedProviderManager<>)); yield return describe.Transient( typeof(INestedProviderManagerAsync<>), typeof(NestedProviderManagerAsync<>)); yield return describe.Transient(); // Core action discovery, filters and action execution. // These are consumed only when creating action descriptors, then they can be de-allocated yield return describe.Transient(); yield return describe.Transient(); // This accesses per-request services to activate the controller yield return describe.Transient(); // This has a cache, so it needs to be a singleton yield return describe.Singleton(); // This accesses per-reqest services yield return describe.Transient(); // This provider needs access to the per-request services, but might be used many times for a given // request. yield return describe.Scoped, DefaultActionConstraintProvider>(); yield return describe.Singleton(); yield return describe.Scoped(); yield return describe.Transient(); yield return describe.Transient, ControllerActionDescriptorProvider>(); yield return describe.Transient, ControllerActionInvokerProvider>(); yield return describe.Singleton(); // The IGlobalFilterProvider is used to build the action descriptors (likely once) and so should // remain transient to avoid keeping it in memory. yield return describe.Transient(); yield return describe.Transient, DefaultFilterProvider>(); yield return describe.Transient(); // Dataflow - ModelBinding, Validation and Formatting yield return describe.Transient(); yield return describe.Transient(); yield return describe.Scoped(); yield return describe.Transient(); yield return describe.Transient(); yield return describe.Transient(); yield return describe.Instance(new JsonOutputFormatter()); yield return describe.Transient(); yield return describe.Transient(); yield return describe.Transient(); // Razor, Views and runtime compilation // The provider is inexpensive to initialize and provides ViewEngines that may require request // specific services. yield return describe.Scoped(); yield return describe.Transient(); // Transient since the IViewLocationExpanders returned by the instance is cached by view engines. yield return describe.Transient(); // Caches view locations that are valid for the lifetime of the application. yield return describe.Singleton(); yield return describe.Singleton(); // The host is designed to be discarded after consumption and is very inexpensive to initialize. yield return describe.Transient(serviceProvider => { var cachedFileProvider = serviceProvider.GetRequiredService(); return new MvcRazorHost(cachedFileProvider); }); // Caches compilation artifacts across the lifetime of the application. yield return describe.Singleton(); // This caches compilation related details that are valid across the lifetime of the application // and is required to be a singleton. yield return describe.Singleton(); // Both the compiler cache and roslyn compilation service hold on the compilation related // caches. RazorCompilation service is just an adapter service, and it is transient to ensure // the IMvcRazorHost dependency does not maintain state. yield return describe.Transient(); // The ViewStartProvider needs to be able to consume scoped instances of IRazorPageFactory yield return describe.Scoped(); yield return describe.Transient(); yield return describe.Singleton(); // Virtual path view factory needs to stay scoped so views can get get scoped services. yield return describe.Scoped(); // View and rendering helpers yield return describe.Transient(); yield return describe.Transient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>)); yield return describe.Scoped(); // Only want one ITagHelperActivator so it can cache Type activation information. Types won't conflict. yield return describe.Singleton(); // Consumed by the Cache tag helper to cache results across the lifetime of the application. yield return describe.Singleton(); // DefaultHtmlGenerator is pretty much stateless but depends on Scoped services such as IUrlHelper and // IActionBindingContextProvider. Therefore it too is scoped. yield return describe.Transient(); yield return describe.Transient(); yield return describe.Singleton(); yield return describe.Transient(); yield return describe.Transient, DefaultViewComponentInvokerProvider>(); yield return describe.Transient(); // Security and Authorization yield return describe.Singleton(); yield return describe.Singleton(); yield return describe.Singleton(); // Api Description yield return describe.Singleton(); yield return describe.Transient, DefaultApiDescriptionProvider>(); } } }