4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.NET CoreでIServiceProviderに登録される情報を確認する

Posted at

やること

ASP.NET Core Web Applicationで、IServiceProvider(DIコンテナ)に登録されるコンポーネントの情報を確認してみます。

環境

  • Visual Strudio 2015
  • ASP.NET Core Web Application (.NET Core)を「認証なし」で作成し、1.1化した状態

手順

Startupクラスに下記のようなShowComponents()メソッドを追加し、ConfigureServices()の最後で呼び出します。

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    ShowComponents(services);
}

private static void ShowComponents(IServiceCollection services)
{
    foreach (var service in services)
    {
        if (service.ImplementationType != null)
        {
            Debug.WriteLine(
                "Type\t{0}\t{1}\t{2}",
                service.Lifetime,
                service.ServiceType,
                service.ServiceType == service.ImplementationType ? "-" : service.ImplementationType.ToString());
        }
        else if (service.ImplementationFactory != null)
        {
            Debug.WriteLine(
                "Factory\t{0}\t{1}",
                service.Lifetime,
                service.ServiceType);
        }
        else
        {
            Debug.WriteLine(
                "Constant\t{0}\t{1}\t{2}",
                service.Lifetime,
                service.ServiceType,
                service.ImplementationInstance.GetType());
        }
    }
}

上記により、IServiceProviderに登録されるコンポーネントの情報がわかります。

登録されるコンポーネントのインスタンス解決方法については、以下の3種類があります。

種別 概要
Type ImplementationTypeのTypeを元にインスタンスを生成
Factory ImplementationFactory(Func)による生成
Constant ImplementationInstanceへの参照

また、ライフサイクルについては以下の三種類があります。

種別 概要
Singleton 単一のインスタンスが返される
Transient 要求毎にインスタンスが生成される
Scoped 同一リクエスト中は同じインスタンスが返される

出力結果

AddMvc()しただけのプロジェクトでの出力結果を以下に載せておきます。

種別 ライフサイクル キーとなる型 解決されるインスタンスの型
Constant Singleton M.A.Hosting.IApplicationLifetime M.A.Hosting.Internal.ApplicationLifetime
Constant Singleton M.A.Hosting.IHostingEnvironment M.A.Hosting.Internal.HostingEnvironment
Constant Singleton M.A.Hosting.IStartupFilter M.A.Server.IISIntegration.IISSetupFilter
Constant Singleton M.A.Mvc.ApplicationParts.ApplicationPartManager M.A.Mvc.ApplicationParts.ApplicationPartManager
Constant Singleton M.E.Logging.ILoggerFactory M.E.Logging.LoggerFactory
Constant Singleton M.E.Options.IConfigureOptions`1[M.A.Builder.ForwardedHeadersOptions] M.E.Options.ConfigureOptions`1[M.A.Builder.ForwardedHeadersOptions]
Constant Singleton S.Buffers.ArrayPool`1[M.A.Mvc.ViewFeatures.Internal.ViewBufferValue] S.Buffers.DefaultArrayPool`1[M.A.Mvc.ViewFeatures.Internal.ViewBufferValue]
Constant Singleton S.Buffers.ArrayPool`1[S.Byte] S.Buffers.DefaultArrayPool`1[S.Byte]
Constant Singleton S.Buffers.ArrayPool`1[S.Char] S.Buffers.DefaultArrayPool`1[S.Char]
Constant Singleton S.Diagnostics.DiagnosticListener S.Diagnostics.DiagnosticListener
Constant Singleton S.Diagnostics.DiagnosticSource S.Diagnostics.DiagnosticListener
Constant Singleton S.Text.Encodings.Web.UrlEncoder S.Text.Encodings.Web.DefaultUrlEncoder
Factory Singleton M.A.DataProtection.AuthenticatedEncryption.ConfigurationModel.IAuthenticatedEncryptorConfiguration
Factory Singleton M.A.DataProtection.IDataProtectionProvider
Factory Singleton M.A.DataProtection.KeyManagement.IKeyManager
Factory Singleton M.A.DataProtection.KeyManagement.Internal.IDefaultKeyServices
Factory Singleton M.A.Hosting.IStartup
Factory Singleton M.A.Mvc.Formatters.JsonOutputFormatter
Factory Singleton M.A.Mvc.ModelBinding.Validation.IObjectModelValidator
Factory Singleton M.A.Mvc.Razor.Directives.IChunkTreeCache
Factory Singleton M.A.Razor.Runtime.TagHelpers.ITagHelperDescriptorFactory
Factory Singleton M.E.ObjectPool.ObjectPool`1[M.A.Antiforgery.Internal.AntiforgerySerializationContext]
Factory Singleton M.E.ObjectPool.ObjectPool`1[M.A.Routing.Internal.UriBuildingContext]
Factory Singleton S.Text.Encodings.Web.HtmlEncoder
Factory Singleton S.Text.Encodings.Web.JavaScriptEncoder
Factory Transient M.A.Mvc.ModelBinding.Metadata.ICompositeMetadataDetailsProvider
Factory Transient M.E.Options.IConfigureOptions`1[M.A.DataProtection.DataProtectionOptions]
Type Scoped M.A.Mvc.ViewFeatures.Internal.IViewBufferScope M.A.Mvc.ViewFeatures.Internal.MemoryPoolViewBufferScope
Type Scoped M.E.Options.IOptionsSnapshot`1[TOptions] M.E.Options.OptionsSnapshot`1[TOptions]
Type Singleton M.A.Antiforgery.IAntiforgery M.A.Antiforgery.Internal.DefaultAntiforgery
Type Singleton M.A.Antiforgery.IAntiforgeryAdditionalDataProvider M.A.Antiforgery.Internal.DefaultAntiforgeryAdditionalDataProvider
Type Singleton M.A.Antiforgery.Internal.IAntiforgeryTokenGenerator M.A.Antiforgery.Internal.DefaultAntiforgeryTokenGenerator
Type Singleton M.A.Antiforgery.Internal.IAntiforgeryTokenSerializer M.A.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer
Type Singleton M.A.Antiforgery.Internal.IAntiforgeryTokenStore M.A.Antiforgery.Internal.DefaultAntiforgeryTokenStore
Type Singleton M.A.Antiforgery.Internal.IClaimUidExtractor M.A.Antiforgery.Internal.DefaultClaimUidExtractor
Type Singleton M.A.DataProtection.Internal.IActivator M.A.DataProtection.RC1ForwardingActivator
Type Singleton M.A.DataProtection.Internal.IActivator M.A.DataProtection.RC1ForwardingActivator
Type Singleton M.A.DataProtection.Internal.IActivator M.A.DataProtection.RC1ForwardingActivator
Type Singleton M.A.DataProtection.Internal.IActivator M.A.DataProtection.RC1ForwardingActivator
Type Singleton M.A.Hosting.Server.IServer M.A.Server.Kestrel.KestrelServer
Type Singleton M.A.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider M.A.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider
Type Singleton M.A.Mvc.Controllers.IControllerFactory M.A.Mvc.Controllers.DefaultControllerFactory
Type Singleton M.A.Mvc.DataAnnotations.IValidationAttributeAdapterProvider M.A.Mvc.DataAnnotations.Internal.ValidationAttributeAdapterProvider
Type Singleton M.A.Mvc.Filters.IFilterProvider M.A.Mvc.Internal.DefaultFilterProvider
Type Singleton M.A.Mvc.Formatters.FormatFilter -
Type Singleton M.A.Mvc.Formatters.Json.Internal.JsonResultExecutor -
Type Singleton M.A.Mvc.Infrastructure.IActionDescriptorCollectionProvider M.A.Mvc.Internal.ActionDescriptorCollectionProvider
Type Singleton M.A.Mvc.Infrastructure.IActionInvokerFactory M.A.Mvc.Internal.ActionInvokerFactory
Type Singleton M.A.Mvc.Infrastructure.IActionSelector M.A.Mvc.Internal.ActionSelector
Type Singleton M.A.Mvc.Internal.ActionConstraintCache -
Type Singleton M.A.Mvc.Internal.ClientValidatorCache -
Type Singleton M.A.Mvc.Internal.ContentResultExecutor -
Type Singleton M.A.Mvc.Internal.ControllerActionInvokerCache -
Type Singleton M.A.Mvc.Internal.FileContentResultExecutor -
Type Singleton M.A.Mvc.Internal.FileStreamResultExecutor -
Type Singleton M.A.Mvc.Internal.IActionSelectorDecisionTreeProvider M.A.Mvc.Internal.ActionSelectorDecisionTreeProvider
Type Singleton M.A.Mvc.Internal.IControllerArgumentBinder M.A.Mvc.Internal.DefaultControllerArgumentBinder
Type Singleton M.A.Mvc.Internal.IHttpRequestStreamReaderFactory M.A.Mvc.Internal.MemoryPoolHttpRequestStreamReaderFactory
Type Singleton M.A.Mvc.Internal.IHttpResponseStreamWriterFactory M.A.Mvc.Internal.MemoryPoolHttpResponseStreamWriterFactory
Type Singleton M.A.Mvc.Internal.ITypeActivatorCache M.A.Mvc.Internal.TypeActivatorCache
Type Singleton M.A.Mvc.Internal.LocalRedirectResultExecutor -
Type Singleton M.A.Mvc.Internal.MiddlewareFilterBuilder -
Type Singleton M.A.Mvc.Internal.MiddlewareFilterConfigurationProvider -
Type Singleton M.A.Mvc.Internal.MvcMarkerService -
Type Singleton M.A.Mvc.Internal.MvcRouteHandler -
Type Singleton M.A.Mvc.Internal.ObjectResultExecutor -
Type Singleton M.A.Mvc.Internal.PhysicalFileResultExecutor -
Type Singleton M.A.Mvc.Internal.RedirectResultExecutor -
Type Singleton M.A.Mvc.Internal.RedirectToActionResultExecutor -
Type Singleton M.A.Mvc.Internal.RedirectToRouteResultExecutor -
Type Singleton M.A.Mvc.Internal.VirtualFileResultExecutor -
Type Singleton M.A.Mvc.ModelBinding.IModelBinderFactory M.A.Mvc.ModelBinding.ModelBinderFactory
Type Singleton M.A.Mvc.ModelBinding.IModelMetadataProvider M.A.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider
Type Singleton M.A.Mvc.Razor.Compilation.ICompilationService M.A.Mvc.Razor.Internal.DefaultRoslynCompilationService
Type Singleton M.A.Mvc.Razor.Internal.CSharpCompiler -
Type Singleton M.A.Mvc.Razor.Internal.ICompilerCacheProvider M.A.Mvc.Razor.Internal.DefaultCompilerCacheProvider
Type Singleton M.A.Mvc.Razor.Internal.IRazorViewEngineFileProviderAccessor M.A.Mvc.Razor.Internal.DefaultRazorViewEngineFileProviderAccessor
Type Singleton M.A.Mvc.Razor.Internal.RazorReferenceManager -
Type Singleton M.A.Mvc.Razor.Internal.ViewComponentTagHelperDescriptorResolver -
Type Singleton M.A.Mvc.Razor.IRazorPageActivator M.A.Mvc.Razor.RazorPageActivator
Type Singleton M.A.Mvc.Razor.IRazorViewEngine M.A.Mvc.Razor.RazorViewEngine
Type Singleton M.A.Mvc.Razor.ITagHelperActivator M.A.Mvc.Razor.Internal.DefaultTagHelperActivator
Type Singleton M.A.Mvc.Razor.ITagHelperFactory M.A.Mvc.Razor.Internal.DefaultTagHelperFactory
Type Singleton M.A.Mvc.Rendering.IJsonHelper M.A.Mvc.ViewFeatures.JsonHelper
Type Singleton M.A.Mvc.Routing.IUrlHelperFactory M.A.Mvc.Routing.UrlHelperFactory
Type Singleton M.A.Mvc.TagHelpers.Cache.IDistributedCacheTagHelperService M.A.Mvc.TagHelpers.Cache.DistributedCacheTagHelperService
Type Singleton M.A.Mvc.ViewComponents.IViewComponentActivator M.A.Mvc.ViewComponents.DefaultViewComponentActivator
Type Singleton M.A.Mvc.ViewComponents.IViewComponentDescriptorCollectionProvider M.A.Mvc.ViewComponents.DefaultViewComponentDescriptorCollectionProvider
Type Singleton M.A.Mvc.ViewComponents.IViewComponentFactory M.A.Mvc.ViewComponents.DefaultViewComponentFactory
Type Singleton M.A.Mvc.ViewComponents.IViewComponentInvokerFactory M.A.Mvc.ViewComponents.DefaultViewComponentInvokerFactory
Type Singleton M.A.Mvc.ViewComponents.IViewComponentSelector M.A.Mvc.ViewComponents.DefaultViewComponentSelector
Type Singleton M.A.Mvc.ViewEngines.ICompositeViewEngine M.A.Mvc.ViewEngines.CompositeViewEngine
Type Singleton M.A.Mvc.ViewFeatures.IHtmlGenerator M.A.Mvc.ViewFeatures.DefaultHtmlGenerator
Type Singleton M.A.Mvc.ViewFeatures.IModelExpressionProvider M.A.Mvc.ViewFeatures.ModelExpressionProvider
Type Singleton M.A.Mvc.ViewFeatures.Internal.AutoValidateAntiforgeryTokenAuthorizationFilter -
Type Singleton M.A.Mvc.ViewFeatures.Internal.ExpressionTextCache -
Type Singleton M.A.Mvc.ViewFeatures.Internal.PartialViewResultExecutor -
Type Singleton M.A.Mvc.ViewFeatures.Internal.SaveTempDataFilter -
Type Singleton M.A.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter -
Type Singleton M.A.Mvc.ViewFeatures.Internal.ViewComponentInvokerCache -
Type Singleton M.A.Mvc.ViewFeatures.Internal.ViewComponentResultExecutor -
Type Singleton M.A.Mvc.ViewFeatures.Internal.ViewResultExecutor -
Type Singleton M.A.Mvc.ViewFeatures.ITempDataDictionaryFactory M.A.Mvc.ViewFeatures.TempDataDictionaryFactory
Type Singleton M.A.Mvc.ViewFeatures.ITempDataProvider M.A.Mvc.ViewFeatures.SessionStateTempDataProvider
Type Singleton M.A.Mvc.ViewFeatures.ValidationHtmlAttributeProvider M.A.Mvc.ViewFeatures.DefaultValidationHtmlAttributeProvider
Type Singleton M.A.Razor.Compilation.TagHelpers.ITagHelperDescriptorResolver M.A.Mvc.Razor.Internal.CompositeTagHelperDescriptorResolver
Type Singleton M.A.Razor.Runtime.TagHelpers.ITagHelperTypeResolver M.A.Razor.Runtime.TagHelpers.TagHelperTypeResolver
Type Singleton M.A.Razor.Runtime.TagHelpers.TagHelperDescriptorResolver -
Type Singleton M.A.Routing.Internal.RoutingMarkerService -
Type Singleton M.E.Caching.Distributed.IDistributedCache M.E.Caching.Distributed.MemoryDistributedCache
Type Singleton M.E.Caching.Memory.IMemoryCache M.E.Caching.Memory.MemoryCache
Type Singleton M.E.Logging.ILogger`1[TCategoryName] M.E.Logging.Logger`1[T]
Type Singleton M.E.ObjectPool.ObjectPoolProvider M.E.ObjectPool.DefaultObjectPoolProvider
Type Singleton M.E.Options.IOptions`1[TOptions] M.E.Options.OptionsManager`1[TOptions]
Type Singleton M.E.Options.IOptionsMonitor`1[TOptions] M.E.Options.OptionsMonitor`1[TOptions]
Type Transient M.A.Authorization.IAuthorizationEvaluator M.A.Authorization.DefaultAuthorizationEvaluator
Type Transient M.A.Authorization.IAuthorizationHandler M.A.Authorization.Infrastructure.PassThroughAuthorizationHandler
Type Transient M.A.Authorization.IAuthorizationHandlerContextFactory M.A.Authorization.DefaultAuthorizationHandlerContextFactory
Type Transient M.A.Authorization.IAuthorizationPolicyProvider M.A.Authorization.DefaultAuthorizationPolicyProvider
Type Transient M.A.Authorization.IAuthorizationService M.A.Authorization.DefaultAuthorizationService
Type Transient M.A.Cors.Infrastructure.ICorsPolicyProvider M.A.Cors.Infrastructure.DefaultCorsPolicyProvider
Type Transient M.A.Cors.Infrastructure.ICorsService M.A.Cors.Infrastructure.CorsService
Type Transient M.A.Hosting.Builder.IApplicationBuilderFactory M.A.Hosting.Builder.ApplicationBuilderFactory
Type Transient M.A.Hosting.IStartupFilter M.A.Hosting.Internal.AutoRequestServicesStartupFilter
Type Transient M.A.Http.IHttpContextFactory M.A.Http.HttpContextFactory
Type Transient M.A.Mvc.Abstractions.IActionDescriptorProvider M.A.Mvc.Internal.ControllerActionDescriptorProvider
Type Transient M.A.Mvc.Abstractions.IActionInvokerProvider M.A.Mvc.Internal.ControllerActionInvokerProvider
Type Transient M.A.Mvc.ActionConstraints.IActionConstraintProvider M.A.Mvc.Internal.DefaultActionConstraintProvider
Type Transient M.A.Mvc.ApiExplorer.IApiDescriptionProvider M.A.Mvc.ApiExplorer.DefaultApiDescriptionProvider
Type Transient M.A.Mvc.ApplicationModels.IApplicationModelProvider M.A.Mvc.Cors.Internal.CorsApplicationModelProvider
Type Transient M.A.Mvc.ApplicationModels.IApplicationModelProvider M.A.Mvc.Internal.AuthorizationApplicationModelProvider
Type Transient M.A.Mvc.ApplicationModels.IApplicationModelProvider M.A.Mvc.Internal.DefaultApplicationModelProvider
Type Transient M.A.Mvc.Controllers.IControllerActivator M.A.Mvc.Controllers.DefaultControllerActivator
Type Transient M.A.Mvc.Cors.CorsAuthorizationFilter -
Type Transient M.A.Mvc.Internal.IControllerPropertyActivator M.A.Mvc.Internal.DefaultControllerPropertyActivator
Type Transient M.A.Mvc.Internal.IControllerPropertyActivator M.A.Mvc.ViewFeatures.ViewDataDictionaryControllerPropertyActivator
Type Transient M.A.Mvc.Internal.MvcAttributeRouteHandler -
Type Transient M.A.Mvc.IViewComponentHelper M.A.Mvc.ViewComponents.DefaultViewComponentHelper
Type Transient M.A.Mvc.Razor.Compilation.IRazorCompilationService M.A.Mvc.Razor.Internal.RazorCompilationService
Type Transient M.A.Mvc.Razor.IMvcRazorHost M.A.Mvc.Razor.MvcRazorHost
Type Transient M.A.Mvc.Razor.IRazorPageFactoryProvider M.A.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider
Type Transient M.A.Mvc.Rendering.IHtmlHelper M.A.Mvc.ViewFeatures.HtmlHelper
Type Transient M.A.Mvc.Rendering.IHtmlHelper`1[TModel] M.A.Mvc.ViewFeatures.HtmlHelper`1[TModel]
Type Transient M.A.Mvc.TagHelpers.Cache.IDistributedCacheTagHelperFormatter M.A.Mvc.TagHelpers.Cache.DistributedCacheTagHelperFormatter
Type Transient M.A.Mvc.TagHelpers.Cache.IDistributedCacheTagHelperStorage M.A.Mvc.TagHelpers.Cache.DistributedCacheTagHelperStorage
Type Transient M.A.Mvc.ViewComponents.IViewComponentDescriptorProvider M.A.Mvc.ViewComponents.DefaultViewComponentDescriptorProvider
Type Transient M.A.Routing.IInlineConstraintResolver M.A.Routing.DefaultInlineConstraintResolver
Type Transient M.A.Routing.Tree.TreeRouteBuilder -
Type Transient M.E.DependencyInjection.IServiceProviderFactory`1[M.E.DependencyInjection.IServiceCollection] M.E.DependencyInjection.DefaultServiceProviderFactory
Type Transient M.E.Options.IConfigureOptions`1[M.A.Antiforgery.AntiforgeryOptions] M.A.Antiforgery.Internal.AntiforgeryOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcOptions] M.A.Mvc.DataAnnotations.Internal.MvcDataAnnotationsMvcOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcOptions] M.A.Mvc.Formatters.Json.Internal.MvcJsonMvcOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcOptions] M.A.Mvc.Internal.MvcCoreMvcOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcOptions] M.A.Mvc.ViewFeatures.Internal.TempDataMvcOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcViewOptions] M.A.Mvc.Razor.Internal.MvcRazorMvcViewOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.MvcViewOptions] M.A.Mvc.ViewFeatures.Internal.MvcViewOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.Razor.RazorViewEngineOptions] M.A.Mvc.Razor.Internal.DependencyContextRazorViewEngineOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Mvc.Razor.RazorViewEngineOptions] M.A.Mvc.RazorViewEngineOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Routing.RouteOptions] M.A.Mvc.Internal.MvcCoreRouteOptionsSetup
Type Transient M.E.Options.IConfigureOptions`1[M.A.Server.Kestrel.KestrelServerOptions] M.A.Server.Kestrel.Internal.KestrelServerOptionsSetup

順序についてはソート済みで、名前空間は一部略記にしてあります。

名前空間の略記は、「M.A」が「Microsoft.AspNetCore」、「M.E」が「Microsoft.Extensions」、「S」が「System」の略になります。

一部、同一の情報が登録されているものがありますが、これはAddXxx()メソッド内などにおいて、それぞれの処理が必要とするコンポーネントの情報が個別に登録されているためです。

ASP.NET Coreでは同一の登録情報が複数件あった場合、そのうちの1件の情報を元にインスタンスの解決が行われることを想定しています。

標準のIServiceProvider(Microsoft.Extensions.DependencyInjection)では最後に登録された情報が使用されます。

うさコメ

色々なAddXxx()をした状態で登録されているものを確認してみると面白いかも(・ω・)

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?