[英]Getting NullReferenceException and object reference not set to an instance of an object when checking if session is null with ASP.NET 5 and MVC 6
I am trying to get session to work in my project but I am getting 我正在尝试让会话在我的项目中工作,但我正在
NullReferenceException
NullReferenceException
and 和
Object reference not set to an instance of an object
你调用的对象是空的
when checking if a specific session exists using this code: 使用以下代码检查特定会话是否存在时:
if (_httpContextAccessor.HttpContext.Session.GetString("CompanyCode") != null)
{
queryArgs.CompanyCode = _httpContextAccessor.HttpContext.Session.GetString("CompanyCode").ToString();
} else { ... }
The exception is thrown on the if statement line, which have me guessing the session object must not be initialized correctly, otherwise it would simply move through it. 如果在if语句行上引发了异常,则我猜该会话对象必须未正确初始化,否则它将在其中移动。
I have added session to the project with these settings 我已使用这些设置将会话添加到项目中
project.json: project.json:
"Microsoft.AspNet.Session": "1.0.0-rc1-final"
Startup.cs: Startup.cs:
services.AddCaching();
services.AddSession();
services.AddTransient<GQFacade>();
app.UseSession();
Finally I have injected it in the class I'm using with: 最后,我将其注入了我正在使用的类中:
private static IHttpContextAccessor _httpContextAccessor;
public GQFacade(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
I used these guides as sources: 我将这些指南用作资料来源:
https://neelbhatt40.wordpress.com/2015/09/07/implement-sessions-in-asp-net-5vnext-and-mvc-6/ https://neelbhatt40.wordpress.com/2015/09/07/implement-sessions-in-asp-net-5vnext-and-mvc-6/
http://benjii.me/2015/07/using-sessions-and-httpcontext-in-aspnet5-and-mvc6/ http://benjii.me/2015/07/using-sessions-and-httpcontext-in-aspnet5-and-mvc6/
Any help would be much appreciated! 任何帮助将非常感激!
Edit: This question has been identified as a possible duplicate to this question: What is a NullReferenceException, and how do I fix it? 编辑:此问题已被标识为该问题的可能重复: 什么是NullReferenceException,我该如何解决?
I read that post before i posted this one actually and I decided it was not the same type of question. 在我实际发布此帖子之前,我已阅读了该帖子,因此我决定这不是同一类型的问题。 This is how I reasoned:
这就是我的推理方式:
That question is about what nullreference exception is, and what I can tell the basic solution to that question is to check whether it is null or not before doing anything with your code. 这个问题是关于什么是nullreference异常,我可以告诉该问题的基本解决方案是在对代码进行任何操作之前检查它是否为null。 That is exactly what I'm doing.
那正是我在做什么。 Secondly, my question is about why it throws the nullreference exception when I am CHECKING if there is null.
其次,我的问题是为什么我在检查是否存在null时为什么抛出nullreference异常。 Hence, it is ON the if statement where my problem lies (at least it's there the exception is thrown).
因此,我的问题所在的if语句处于ON状态(至少在该异常存在的地方)。
Try without GetString in the if condition 在if条件下尝试不使用GetString
if (_httpContextAccessor.HttpContext.Session["CompanyCode"] != null)
{
queryArgs.CompanyCode = _httpContextAccessor.HttpContext.Session.GetString("CompanyCode").ToString();
}
When the session expires the session variable become null 当会话到期时,会话变量变为null
In order to eliminate this error use try and catch
为了消除此错误,请
try and catch
使用try and catch
@try
{
if (_httpContextAccessor.HttpContext.Session.GetString("CompanyCode") != null)
{
queryArgs.CompanyCode = _httpContextAccessor.HttpContext.Session.GetString("CompanyCode").ToString();
}
}
catch(Exception ex)
{
Response.Redirect("controllerName/ActionName");
}
when the session expires, it will redirect to the action. 会话到期时,它将重定向到操作。 Make sure the user logout when session expires by redirecting to the logout action
通过重定向到注销操作来确保会话到期时用户注销
Ok, I solved it. 好的,我解决了。 I found this post and the solution worked for me too, so this could be a possible duplicate to that question.
我找到了这篇文章,该解决方案也对我有用,因此这可能是该问题的重复。 Injected HttpContext is always null
注入的HttpContext始终为null
What I did was to add this in Startup.cs in the configure method and then it worked fine. 我所做的就是在configure方法中的Startup.cs中添加它,然后工作正常。
app.ApplicationServices.GetRequiredService<GQFacade>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.