Home > OS >  Can I receive configured services directly in Program.cs without controller or view (net core 6)?
Can I receive configured services directly in Program.cs without controller or view (net core 6)?

Time:08-09

I can read options of services in controller by the same way

Public Class HomeController
    Inherits Controller

    Private ReadOnly _logger As ILogger(Of HomeController)
    Private ReadOnly _RazorPagesOptions As RazorPages.RazorPagesOptions
    Private ReadOnly _RazorViewEngineOptions As Razor.RazorViewEngineOptions
    Private ReadOnly _MvcViewOptions As Mvc.MvcViewOptions
    Private ReadOnly _IdentityOptions As Identity.IdentityOptions
    Private ReadOnly _MvcOptions As Mvc.MvcOptions
    Private ReadOnly _CookieAuthenticationOptions As Authentication.Cookies.CookieAuthenticationOptions
    Private ReadOnly _AuthenticationOptions As Authentication.AuthenticationOptions
    Private ReadOnly _RouteOptions As Routing.RouteOptions
    Private ReadOnly _HostOptions As Microsoft.Extensions.Hosting.HostOptions
    Private ReadOnly _KestrelServerOptions As Server.Kestrel.Core.KestrelServerOptions
    Private ReadOnly _HostFilteringOptions As HostFiltering.HostFilteringOptions
    Private ReadOnly _ApiBehaviorOptions As Mvc.ApiBehaviorOptions
    Private ReadOnly _AntiforgeryOptions As Antiforgery.AntiforgeryOptions

    Public Sub New(ByVal logger As ILogger(Of HomeController),
                   RazorPagesOptions As IOptions(Of RazorPages.RazorPagesOptions),
                   RazorViewEngineOptions As IOptions(Of Razor.RazorViewEngineOptions),
                   MvcViewOptions As IOptions(Of Mvc.MvcViewOptions),
                   IdentityOptions As IOptions(Of Identity.IdentityOptions),
                   MvcOptions As IOptions(Of Mvc.MvcOptions),
                   CookieAuthenticationOptions As IOptions(Of Authentication.Cookies.CookieAuthenticationOptions),
                   AuthenticationOptions As IOptions(Of Authentication.AuthenticationOptions),
                   RouteOptions As IOptions(Of Routing.RouteOptions),
                   HostOptions As IOptions(Of Microsoft.Extensions.Hosting.HostOptions),
                   KestrelServerOptions As IOptions(Of Server.Kestrel.Core.KestrelServerOptions),
                   HostFilteringOptions As IOptions(Of HostFiltering.HostFilteringOptions),
                   ApiBehaviorOptions As IOptions(Of Mvc.ApiBehaviorOptions),
                   AntiforgeryOptions As IOptions(Of Antiforgery.AntiforgeryOptions))
        _logger = logger

        _RazorPagesOptions = RazorPagesOptions.Value
        _RazorViewEngineOptions = RazorViewEngineOptions.Value
        _MvcViewOptions = MvcViewOptions.Value
        _IdentityOptions = IdentityOptions.Value
        _MvcOptions = MvcOptions.Value
        _CookieAuthenticationOptions = CookieAuthenticationOptions.Value
        _AuthenticationOptions = AuthenticationOptions.Value
        _RouteOptions = RouteOptions.Value
        _HostOptions = HostOptions.Value
        _KestrelServerOptions = KestrelServerOptions.Value
        _HostFilteringOptions = HostFilteringOptions.Value
        _ApiBehaviorOptions = ApiBehaviorOptions.Value
        _AntiforgeryOptions = AntiforgeryOptions.Value

or I can inject services to View with directive @inject

But maybe there is a way to do the same task without controller or View? Maybe it's possible to do the same directly in Program.CS/Program.VB ? Something instead App.Run or before App.Run to receive the same list of project configured options.

Sub Main(Args() As String)
    Dim Builder = WebApplication.CreateBuilder(Args)
    ....
    Dim App = Builder.Build
    ...
    App.Run()
End Sub

CodePudding user response:

You can resolve services using the App.Services. In C# it will look like this:

var s1 = App.Services.GetService<SomeService>();
// or 
var s2 = App.Services.GetRequiredService<SomeService>();

In VB I guess it will look like:

Dim s1 = App.Services.GetService(Of SomeService)
' Or
Dim s1 = App.Services.GetRequiredService(Of SomeService)

Note that to resolve services registered as scoped you will need to Inspect service options directly without controller

  • Related