尝试创建连接时,我的 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