mercredi 3 août 2022

How to read service parameters in DI container with ASP.NET Core 6

For example I have configured a service

Builder.Services.AddRazorPages(Sub(options As RazorPagesOptions)
                 options.RootDirectory = "/"
                 options.Conventions.AuthorizeAreaFolder("Identity", "/User")
           End Sub)

Than I want to read these options RootDirectory and Conventions.

I try to read, but failed. I can receive only list of parameters, for example

Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions
0. ViewLocationExpanders : Of IList`1
1. ViewLocationFormats : Of IList`1
2. AreaViewLocationFormats : Of IList`1
3. PageViewLocationFormats : Of IList`1
4. AreaPageViewLocationFormats : Of IList`1

Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions
0. Conventions : Of PageConventionCollection
1. RootDirectory : Of String

But for reading value by reflection (GetValue) I need instance of parameters, I don't understand how receive instance of service parameters.

Reading parameters list is simple, for example

Dim OptionTypeList = Builder.Services.Where(Function(X) X.ServiceType.IsGenericType).Select(Function(X) X.ServiceType.GetGenericArguments()(0)).Distinct.ToList
OptionTypeList.ToList.ForEach(Sub(OneOptionType)
     Debug.Print($"{OneOptionType.FullName}")

     For J As Integer = 0 To OneOptionType.GetProperties.Count - 1
         Dim Prop As Reflection.PropertyInfo = DirectCast(OneOptionType, System.Reflection.TypeInfo).DeclaredProperties(J)

         If Prop IsNot Nothing Then
             Debug.Print($"{J}. {Prop.Name} : Of {Prop.PropertyType.Name}")
                                   '(0): {Microsoft.AspNetCore.Mvc.ApplicationModels.PageConventionCollection Conventions}
                                   '(1): {System.String RootDirectory}

But how to receive value of parameters? For example value of Conventions - where is the instance? Or reading parameters are possible without reflection GetValue?





Aucun commentaire:

Enregistrer un commentaire