locked
context.Session not working when deployed on server RRS feed

  • Question

  • User303506745 posted

    Hi All,

    I am getting error of "Object reference not set to an instance of an object". I am using .ashx.

    Here's the code:

    context.Session["idList"] = idList;

    I understand that session id is null but it is working on my machine during development/testing but i can't make it work in the server. In my machine, it seems that it ignores that error but not on server.

    Thanks in advance.

    Wednesday, September 12, 2018 7:50 AM

All replies

  • User753101303 posted

    Hi,

    "session id" that is ? This is context or context.Session which is null?  This error happens when you try to use an object member while the object is null. Assigning a null value to something else should work (so idList being null shouldn't cause an issue here).

    Wednesday, September 12, 2018 8:05 AM
  • User303506745 posted

    hi PatriceSc, yes it's context.Session. Can you suggest a solution? Here's my code:

    List<int> idList = new List<int>();

    foreach (CalendarEvent cevent in EventDAO.getEvents(start, end, assId))
    {
    tasksList.Add(new ImproperCalendarEvent
    {
      id = cevent.id,
      title = cevent.title,
      start = String.Format("{0:s}", cevent.start),
      end = String.Format("{0:s}", cevent.end),
      description = cevent.description,
      allDay = cevent.allDay
    });
    idList.Add(cevent.id);

    context.Session["idList"] = idList;

    I'm sure idList isn't null.

    Wednesday, September 12, 2018 8:39 AM
  • User303506745 posted

    I tried to initialize the session id in Page_Load and I got the same error. So now I know that the problem isn't in the .ashx but also in .aspx.

    protected void Page_Load(object sender, EventArgs e)
    Line 130:        {
    Line 131:            System.Web.HttpContext.Current.Session.Add("idList","assetcodes");
    Line 133:        }

    Again, this error is raised only when i deploy it on server but not in my machine where I develop.

    Wednesday, September 12, 2018 9:32 AM
  • User753101303 posted

    It seems session state is not enabled by default on this machine ? Have you tried with something such as 

     <system.web>
          <pages enableSessionState="true" /> 
     </system.web>

    in your web.config file.

    Wednesday, September 12, 2018 11:00 AM
  • User303506745 posted

    Yes, i tried it but still the same.

    also tried adding it to web.config

    <sessionState mode ="StateServer" cookieless="false"></sessionState>

    and

    <system.serviceModel>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>

    nothing seems to work

    Thursday, September 13, 2018 2:51 AM
  • User839733648 posted

    Hi gaganiaro,

    In the ashx file, if you want to successfully read and write to the Session, you should add the interface IRequiresSessionState after using the Session class.

    You could try below:

    public class YourHandleName : IHttpHandler, IRequiresSessionState 
    {
    ...
    }

    Best Regards,

    Jenifer

    Friday, September 14, 2018 10:43 AM