How to configure services based on request in ASP.NET Core

后端 未结 3 2127
一个人的身影
一个人的身影 2021-02-09 02:33

In ASP.NET Core we can register all dependencies during start up, which executed when application starts. Then registered dependencies will be injected in controller constructor

3条回答
  •  无人及你
    2021-02-09 02:56

    It is possible by using the HttpContextAccessor in Startup.cs

    services.AddHttpContextAccessor();
    services.AddScoped(provider =>
                {
                    var contextAccessor = provider.GetService();
                    var httpContext = contextAccessor.HttpContext;
                    
                    var contextVariable = httpContext. ...
                    
                    // Return implementation of IYourService that corresponds to your contextVariable
    
                  
                });
    

提交回复
热议问题