473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Steven
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
Frinavale
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)

Thanks,

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

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

Similar topics

2
by: moondaddy | last post by:
I need to set a variable to a session variable (if that's what you call it) like this: dim ds as dataset = HttpContext.Current.Session("CustDataSet") But I get an exception if this variable...
4
by: Mat | last post by:
Hi, I've stumbled onto a problem when using the caching object in ASP.Net. I'm placing a static dataset to the cache as the data only changes once a day. Whilst writing to the cache I'm using...
2
by: Luis Esteban Valencia Muñoz | last post by:
I have a 2 base classes that do error handling -- one for pages (System.Web.UI.Page) and one for applications (System.Web.HttpApplication, Global.asax uses it). Are there any situations in...
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
4
by: Sami Rehman | last post by:
hi i have a vs solution in which there are 2 projects class library representing Security services and another one is web application project. i need to access http context in a static method in...
2
by: Dave | last post by:
After some digging, I discovered HttpContext.Current.Session is null when trying to access a session variable, username, in my upload.cs code which is in the App_Code folder. I just determined...
14
by: R.A.M. | last post by:
Hello, I have created ASP.NET project in which I have a file Admin.cs. It contains static class Admin with some methods and properties. The problem is that in property get a reference...
0
by: alister7 | last post by:
hi every1 im trying to download a music file from the database SQLserver.which i upload in the database.. The Code below works fine in wen i create a new project of an ASP.Net web application..m...
3
by: Madhur | last post by:
Hello I am delivering an asp.net 2.0 application to my customer. I need to know, If I need to check for the condition of HttpContext.Current to be null in my business logic. I have extensively...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.