Hi I have the following block of code copied from a previous project and it throws a null exception. This is because the value of HttpContext.Current is set to null and it therefore cannot return HttpContext.Current.Application. Can someone help me work out why this value is null?

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;                
            }
        }

Recommended Answers

All 7 Replies

Yeah ... that happens if you call that property before the session initializes. That happened to me -- I think in your Global.asax if you call it in your Application_Start it will throw that error. Check the call stack and report back where it is being called from.

Sorry I'm not too sure what you mean, I don't have a global.asax and the last call in my call stack before it errors is the SessionFactory.get(), which is what I am in..

This is a web application ... right ? Something has to initiate the call to session factory. Where is it coming from? static constructor for a class perhaps?

It's a web service

The following instantiates the call to SessionFactory:

private Officer FetchStaffDetails(string initials)
        {
            using (new Tracer("Debug"))
            {
                using (DataManager manager = new DataManager(SessionFactory))
                {
                    return manager.FetchStaffDetail(initials, bool.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("UserOfficerLink")));
                }
            }
        }

HttpContext.Current will be null if you call that method outside of a page request, or if that assembly is linked to another non-http and called from there. *When* and *By What* is this method being called.

It's on the same page, I have :

namespace PlnEnf.ServiceImplementation
{
  [System.Web.Services.WebService(Namespace = "http://www.mysite.com/Services/MessageTypes/DC/2009/08", Name = "PlEnf")]
    [System.Web.Services.WebServiceBindingAttribute(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true)]
  public class PlnEnf : IPlnEnf
    {
        #region SessionFactory
        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;                
            }
        }

        #endregion

        #region

        // more code in here.....

        private Officer FetchStaffDetails(string initials)
        {
            using (new Tracer("Debug"))
            {
                using (DataManager manager = new DataManager(SessionFactory))
                {
                    return manager.FetchStaffDetail(initials, bool.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("UserOfficerLink")));
                }
            }
        }
        
        #endregion
    }
}

That still gives no indication as to when the method is called, which doesn't help resolve the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.