【问题标题】:Why HttpContext.Current.Session["value"].ToString() gives null value inside App_Code class file, Object reference not set to an instance of an object为什么 HttpContext.Current.Session["value"].ToString() 在 App_Code 类文件中给出空值,对象引用未设置为对象的实例
【发布时间】:2012-08-16 10:10:50
【问题描述】:

这里的场景是 HttpContext.Current.Session["value"].ToString() 给出空值,甚至在用户登录时已经设置了会话值。

我的网络配置

<sessionState timeout="40" mode="InProc"/>

我的 global.asax

   void Session_Start(object sender, EventArgs e) 
    {
       // Code that runs when a new session is started
       Session["EmployeeId"] = "";
       this.Session["DomainName"] = "";
    }

在我的 Defaultpage.asppx.cs 中

Emp_grade empgrd = new Emp_grade(); 提供未设置为对象实例的对象引用

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;

public partial class EmpGrade : System.Web.UI.Page
{

   //here am getting error(The type initializer for 'Emp_grade' threw an exception)
   // stack error message  Object reference not set to an instance of an object.
   Emp_grade empgrd = new Emp_grade(); 

   protected void Page_Load(object sender, EventArgs e)
    {
      logic code...
      Datatable dt= empgrd.EmpRecord();
    }
}

在我的App_Code folder 我有类文件Emp_grade.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.SessionState;

public class Emp_grade  
{       
    public Emp_grade()
    {
        //TODO: Add constructor logic here
    }

    static string getConnection = HttpContext.Current.Session["DomainName"].ToString();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[getConnection].ConnectionString);

     public DataTable EmpRecord()
    {
       logic code
    }
 }

我的登录页面:

 protected void Page_Load(object sender, EventArgs e)
    { 
    //set some value
     Session["DomainName"] = domainname;
    }

屏幕截图(调试)

【问题讨论】:

  • 那么...您在为会话标签赋值后尝试从会话标签中获取该值?
  • 你的连接字符串有效吗?
  • connectionstring 工作,只有当我得到 getConnection 不是空值
  • @NewAmbition: 是的,我想检索那个 session["domainame"] 值
  • @satindersingh string domainName = Session["domainname"].ToString() 不起作用吗?

标签: c# asp.net session


【解决方案1】:

您可能应该重构一下。这个:

static string getConnection = HttpContext.Current.Session["DomainName"].ToString(); 

是一个带有初始化器的静态字段。它将运行一次,并保留它获得的任何值,直到应用程序重新启动。并且它会在没有明确定义的时间运行,只是保证它会在第一次访问之前的某个时间运行。

这意味着它很可能在没有HttpContext 存在的时候运行(就像Dan Puzey 写的那样),这将导致抛出异常。任何后续访问它的尝试都会(据我所知)导致抛出相同的异常。

但请记住,它只获取一次,因此即使初始化程序成功并找到了一个值,该值也将用于getConnection 的所有后续使用。不会为每个不同的用户重新获取它。

要解决这个问题,您可以将其包装在实例方法中:

private string GetDomainName()
{
    return HttpContext.Current.Session["DomainName"].ToString();
}
SqlConnection conn = null;

然后在构造函数中初始化conn

public Emp_grade()         
{         
    conn = new SqlConnection(ConfigurationManager.ConnectionStrings[GetDomainName()].ConnectionString);
}

还请注意,由于此类正在初始化和存储SqlConnection(即IDisposable),因此您的类还应实现IDisposable 接口。有关实现 IDisposable 的背景信息,请参阅例如 this answer

编辑:屏幕截图显示HttpContext.Current.Session 正在返回空值。

原因是这一行:

Emp_grade empgrd = new Emp_grade();

这是在 Page 类中声明一个实例成员,带有一个初始化器。初始化程序在请求管道中的早期运行,因此在它运行时 Session 尚未出现在上下文中。

改为:

Emp_grade empgrd = null;

protected void Page_Load(object sender, EventArgs e) 
{ 
  empgrd = new Emp_grade();
  Datatable dt = empgrd.EmpRecord(); 
} 

【讨论】:

  • 感谢回复,但还是同样的错误System.NullReferenceException:对象引用未设置为对象的实例。return HttpContext.Current.Session["DomainName"].ToString();
  • @satindersingh 然后您必须找出该行上哪个对象为空。可能是HttpContext.Current 为空,或者HttpContext.Current.Session 为空,或者只是密钥DomainName 的会话中没有任何内容。要找出哪个,在行上放置一个断点,然后当断点被击中时,将鼠标依次悬停在每个断点上,然后查看工具提示显示的内容。还要检查调用堆栈以查看类的更新位置。
  • 请查看截图,更新了我的问题,希望对您有所帮助
【解决方案2】:

您在静态字段的构造函数中引用 .Session["DomainName"]。这可能会在您的任何其他代码之前运行。很可能它会在任何 HttpContext 之外运行,因为它很可能会在应用程序初始化时运行,在第一个请求被处理之前。

我怀疑将getConnection 设为非静态可以解决问题。

【讨论】:

  • 我还怀疑在 Emp_grade 的每个实例中创建 SQL 连接并不是一个理想的解决方案。
  • getConnection non-static 将解决问题,给出错误“字段初始化程序无法引用非静态字段、方法或属性 'Emp_grade.getConnection'”
  • @satindersingh:那么,您应该将初始化移至构造函数。
猜你喜欢
  • 1970-01-01
  • 2019-03-20
  • 2012-06-06
  • 1970-01-01
  • 2020-06-12
  • 2017-02-12
  • 2020-03-05
  • 1970-01-01
  • 2015-01-24
相关资源
最近更新 更多