Application_BeginRequest()未将对象引用设置为对象的实例

时间:2010-12-20 15:13:21

标签: nhibernate asp.net-mvc-2 .net-3.5 ninject-2

尝试创建连接时,我的MVC应用程序出错。我正在使用NHibenate和Ninject。

Global.asax.cs档案:

namespace Web.UI
{
    public class MvcApplication : NinjectHttpApplication // System.Web.HttpApplication
    {
        public static ISessionFactory SessionFactory { get; set; }

        public void CreateSessionFactory()
        {
            SessionFactory = (new Configuration()).Configure().BuildSessionFactory();
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected override IKernel CreateKernel()
        {
            return new StandardKernel(new MvcApplicationModule());
        }

        protected override void OnApplicationStarted()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        protected override void OnApplicationStopped()
        {
            SessionFactory.Dispose();
        }

        protected void Application_BeginRequest()
        {
            var sessionFactory = SessionFactory.OpenSession();
            CurrentSessionContext.Bind(sessionFactory);
        }

        protected void Application_EndRequest()
        {
            CurrentSessionContext.Unbind(SessionFactory);
        }
    }

    internal class MvcApplicationModule : NinjectModule
    {
        public override void Load()
        {

            // NHibernate Session
            Bind<ISession>().ToMethod(ctx => MvcApplication.SessionFactory.GetCurrentSession());

            Bind<IManagerRepository>().To<ManagerRepositoryImpl>();
        }
    }
}

Web.config档案:

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <!--
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      -->
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="connection.connection_string_name">MyConnString</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
      <property name="current_session_context_class">web</property>
      <mapping assembly="Infrastructure"/>
    </session-factory>
  </hibernate-configuration>

这是错误消息:

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 57:         protected void Application_BeginRequest()
Line 58:         {
Line 59:             var sessionFactory = SessionFactory.OpenSession();
Line 60:             CurrentSessionContext.Bind(sessionFactory);
Line 61:         }

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Web.UI.MvcApplication.Application_BeginRequest()

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +71
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +350
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
   System.Web.Util.ArglessEventHandlerProxy.Callback(Object sender, EventArgs e) +42
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053 

1 个答案:

答案 0 :(得分:0)

<强>的Global.asax.cs

protected override void OnApplicationStarted()
{
    RegisterRoutes(RouteTable.Routes);
    CreateSessionFactory();
}