注入的 HttpContext 始终为空

本文关键字:HttpContext 注入 | 更新日期: 2023-09-27 18:34:25

老实说,这令人兴奋,我无法让它工作。上下文始终为空,无论它被注入何处...

我是否必须在 Startup.cs 中添加上下文的实例?如果是,如何?

发起呼叫

services.AddTransient<SessionService>();
Toast.Configure(app.ApplicationServices.GetRequiredService<SessionService>());
_session.Set("foo", "bar");

吐司课

public class Toast
{
    private static SessionService _session;
    public static void Configure(SessionService sessionService)
    {
        _session = sessionService;
        Messages = new List<ToastMessage>();
    }
}

会话服务

public class SessionService
{
    private readonly IHttpContextAccessor _accessor;
    public SessionService(IHttpContextAccessor httpContextAccessor)
    {
        _accessor = httpContextAccessor;
    }
    public void Set(string key, string value)
    {
        _accessor.HttpContext.Session.SetString(key, value);
    }
}

异常

System.NullReferenceException
Object reference not set to an instance of an object.
System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.

堆栈跟踪

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at EveOps.Services.SessionService.Set(String key, Byte[] value) in C:'Users'youri'Documents'Visual Studio 2015'Projects'EveOps'src'EveOps'Services'SessionService.cs:line 25
   at EveOps.Toast.set_Messages(List`1 value) in C:'Users'youri'Documents'Visual Studio 2015'Projects'EveOps'src'EveOps'Toast'Toast.cs:line 41
   at EveOps.Toast.Configure(SessionService sessionService) in C:'Users'youri'Documents'Visual Studio 2015'Projects'EveOps'src'EveOps'Toast'Toast.cs:line 18
   at EveOps.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in C:'Users'youri'Documents'Visual Studio 2015'Projects'EveOps'src'EveOps'Startup.cs:line 162
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder)
   at Microsoft.AspNet.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder builder)
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()

注入的 HttpContext 始终为空

感谢@aguafrommars陈述显而易见的事实; 上下文仅对请求有效,即使我知道这一点,我对 DI 和服务的理解还没有达到很好的水平。

我的问题(伪)的解决方案:

App.Use() {
   Toast.Configure(app.ApplicationServices.GetRequiredService<SessionService>());
}