469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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..

Expand|Select|Wrap|Line Numbers
  1.         private static ISessionFactory SessionFactory
  2.         {
  3.             get
  4.             {
  5.                 ISessionFactory sessionFactory = null;
  6.                 sessionFactory = HttpContext.Current.Application["sessionFactory"] as ISessionFactory;
  7.                 if (sessionFactory == null)
  8.                 {
  9.                     Configuration configuration = new Configuration().Configure();
  10.                     sessionFactory = configuration.BuildSessionFactory();
  11.                     HttpContext.Current.Application.Add("sessionFactory", sessionFactory);
  12.                 }
  13.                 return sessionFactory;                
  14.             }
  15.         }
Aug 5 '09 #1
6 12361
569 Expert 512MB
Line five tells sessionFactory = null. If you want the value of sessionFactory (HttpContext.Current.Application["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.

Aug 5 '09 #2
Yeh I know that part, I need it to be set to null so that it will go into the if part, however HttpContext.Current is null and therefore it doesnt know how to do HttpContext.Current.Application.

If you know what I'm trying to say?
Aug 5 '09 #3
569 Expert 512MB
HttpContext.Current.Application["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.

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

Expand|Select|Wrap|Line Numbers
  1.         private static ISessionFactory SessionFactory
  2.         {
  3.             get
  4.             {
  5.                 ISessionFactory sessionFactory = null;
  6.                 using (Tracer trace = new Tracer("Debug"))
  7.                 {
  8.                     sessionFactory = HttpContext.Current.Application["sessionFactory"] as ISessionFactory;
  9.                     if (sessionFactory == null)
  10.                     {
  11.                         Configuration configuration = new Configuration().Configure();
  12.                         sessionFactory = configuration.BuildSessionFactory();
  13.                         HttpContext.Current.Application.Add("sessionFactory", sessionFactory);
  14.                     }
  15.                     return sessionFactory;
  16.                 }
  17.             }
  18.         }
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.Current is set up with the properties Application, ApplicationInstance, Cache, etc etc. In my project HttpContext.Current is set as null. That is the where I am having the problem..
Aug 5 '09 #5
9,735 Expert Mod 8TB
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)


Aug 5 '09 #6
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
Aug 6 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Mat | last post: by
2 posts views Thread by Luis Esteban Valencia Muñoz | last post: by
reply views Thread by Aaron Morton | last post: by
4 posts views Thread by Sami Rehman | last post: by
2 posts views Thread by Dave | last post: by
3 posts views Thread by Madhur | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.