Why is HttpContext.Current set to null?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moorcroft
    New Member
    • Mar 2008
    • 57

    Why is HttpContext.Current set to null?

    Hi the following lump of code throws a null exception because the .Current value is set to null (line 6). Could someone please help me understand why? I've taken this from a previous working project..

    Code:
            private static ISessionFactory SessionFactory
            {
                get
                {
                    ISessionFactory sessionFactory = null;
                    sessionFactory = HttpContext.Current.Application["sessionFactory"] as ISessionFactory;
                    if (sessionFactory == null)
                    {
                        Configuration configuration = new Configuration().Configure();
                        sessionFactory = configuration.BuildSessionFactory();
                        HttpContext.Current.Application.Add("sessionFactory", sessionFactory);
                    }
                    return sessionFactory;                
                }
            }
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Line five tells sessionFactory = null. If you want the value of sessionFactory (HttpContext.Cu rrent.Applicati on["sessionFactory "]) on line 6, it still is null. You didn't change the value between the point you assign null to it and the point you want to use the value.

    Steven

    Comment

    • moorcroft
      New Member
      • Mar 2008
      • 57

      #3
      Yeh I know that part, I need it to be set to null so that it will go into the if part, however HttpContext.Cur rent is null and therefore it doesnt know how to do HttpContext.Cur rent.Applicatio n.

      If you know what I'm trying to say?

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        HttpContext.Cur rent.Applicatio n["sessionFactory "] on line 6 equals null. For as far as I can see, you're trying to declare a null value as ISessionFactory . I think that is where your error is coming from.

        If you want to enter the if-statement, try to comment out line 6 and see what happens.

        Steven

        Comment

        • moorcroft
          New Member
          • Mar 2008
          • 57

          #5
          No you are wrong here, because the following shows an extract from a different project:

          Code:
                  private static ISessionFactory SessionFactory
                  {
                      get
                      {
                          ISessionFactory sessionFactory = null;
                          using (Tracer trace = new Tracer("Debug"))
                          {
                              sessionFactory = HttpContext.Current.Application["sessionFactory"] as ISessionFactory;
                              if (sessionFactory == null)
                              {
                                  Configuration configuration = new Configuration().Configure();
                                  sessionFactory = configuration.BuildSessionFactory();
                                  HttpContext.Current.Application.Add("sessionFactory", sessionFactory);
                              }
                              return sessionFactory;
                          }
                      }
                  }
          In this block of code, on line 8 sessionFactory gets set up with values and is not null. You are looking at it the wrong way, in this example that is working, HttpContext.Cur rent is set up with the properties Application, ApplicationInst ance, Cache, etc etc. In my project HttpContext.Cur rent is set as null. That is the where I am having the problem..

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            What file is this code part of?
            If it's part of a Global.asax file it could be possible that the HttpContext is not available at the point where you are trying to use it.

            Please elaborate the code and what you're project is (is it a desktop application or is it an asp.net application)

            Thanks,

            -Frinny

            Comment

            • moorcroft
              New Member
              • Mar 2008
              • 57

              #7
              Hi I fixed the issue, the project is an ASP .Net web service written with the web service software factory and using NHibernate for the database mapping. I realised I didn't actually need to instantiate this class in the first place so I just set up the session variable manually

              Comment

              Working...