0
嘗試創建連接時,我在我的MVC應用程序中出現錯誤。我使用NHibenate和Ninject。Application_BeginRequest()未將對象引用設置爲對象的實例
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
你初始化了SessionFactory嗎? – 2010-12-20 16:19:15
你在哪裏調用CreateSessionFactory()?看起來異常是指向你正確的問題。例如該SessionFactory爲null。 – 2010-12-21 03:14:23
是的,我忘了在我的OnApplicationStarted()中調用CreateSessionFactory()。 – 2010-12-21 15:17:17