注入的HttpContext始终为null [英] Injected HttpContext is always null
本文介绍了注入的HttpContext始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说实话,这令人难以置信,我无法正常工作.无论注入到哪里,上下文始终为null ...
This is honestly mindblowing, I can't get it to work. The context is always null, wherever it is injected...
我是否必须在Startup.cs中添加上下文的实例?如果可以,怎么办?
Do I have to add an instance of the context in Startup.cs? If Yes, how?
发起呼叫
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.
Stacktrace
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()
推荐答案
感谢@aguafrommars陈述了显而易见的内容;该上下文仅对请求有效,即使我知道这一点,但我对DI和服务的理解还不够好.
Thanks to @aguafrommars for stating the obvious; the context is only valid for requests, and even though I knew that, my understanding of DI and services is not yet at a good level.
我的问题的解决方案(伪):
The solution for my problem (pseudo):
App.Use() {
Toast.Configure(app.ApplicationServices.GetRequiredService<SessionService>());
}
这篇关于注入的HttpContext始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文