【问题标题】:Nhibernate not working in production environmentNhibernate在生产环境中不起作用
【发布时间】:2013-11-05 17:55:35
【问题描述】:

我正在使用带有 NHibernate 的 C#.NET 3.5 Web 应用程序。在开发环境中一切正常,但是当我在 IIS 中部署应用程序时,出现以下异常:

 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: 

An unhandled exception was generated during the execution of the current web request.     Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
SVC_Cadu_Dal.Helper.NHibernateHelper.get_SessionFactory() in C:\workspace\SVC_Applications\cadu\SVC_Cadu\SVC_Cadu_Dal\Helper\NHibernateHelper.cs:73
SVC_Cadu_Dal.Helper.NHibernateHelper.OpenSession() in C:\workspace\SVC_Applications\cadu\SVC_Cadu\SVC_Cadu_Dal\Helper\NHibernateHelper.cs:82
SVC_Cadu.PessoaPage.initDropDownList(DropDownList ddl, Type type) in C:\workspace\SVC_Applications\cadu\SVC_Cadu\SVC_Cadu\PessoaPage.aspx.cs:84
SVC_Cadu.PessoaPage.initComponents() in C:\workspace\SVC_Applications\cadu\SVC_Cadu\SVC_Cadu\PessoaPage.aspx.cs:66
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +52
System.Web.UI.Control.OnLoad(EventArgs e) +137
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2524

这是我的 NHibernateHelper 类:

public class NHibernateHelper {
    private static ISessionFactory sessionFactory;
    private static ISession session;

    private static ISessionFactory SessionFactory {
        get {
            if(sessionFactory == null) {
                try {
                    var config = new Configuration();
                    // ----------------------------------------------------
                    // Configuration for DEVELOPMENT and PRODUCTION envs.
                    config.Configure(Path.Combine(
                            AppDomain.CurrentDomain.BaseDirectory,
                            "bin\\hibernate.cfg.xml"));
                    // ----------------------------------------------------
                    HbmSerializer.Default.Validate = true;
                    //log4net.Config.XmlConfigurator.Configure();
                    sessionFactory = config.BuildSessionFactory();
                } catch(Exception e) {
                    throw(e.InnerException);
                }
            }
            return sessionFactory;
        }
    }

    public static ISession OpenSession() {
        return session != null && session.IsOpen ? session
                : session = SessionFactory.OpenSession();
    }
}

我的解决方案如下所示:

- Solution
    - WebApp <project>
    - Dal <project>
        - "Mappings" <folder>
        - hibernate.cfg.xml (Embedded Resource, Copy Always) <file>
        - Helpers <folder>
            - NHibernateHelper.cs
    - Model <project>
        - "Models" <classes>
    - (...)

其他重要信息是我正在使用 XML 映射(不是流利或类映射)。 我已经试过了:

  • 将“hibernate.cfg.xml”内容放入“Web.config”文件中(当然,也可以在“config.Configure()”方法中更改参数;
  • 不向“config.Configure()”方法传递任何值;
  • 将硬编码字符串放入“config.Configure()”,如“config.Configure("bin\hibernate.cfg.xml");
  • 哭泣

我不明白为什么我的 NHibernate 配置在开发中有效,但在生产中无效。我做错了什么?

【问题讨论】:

    标签: c# nhibernate iis


    【解决方案1】:

    为什么不使用 HttpContext.Current.Server.MapPath 来获取服务器中的文件路径。

    还要检查文件夹的执行和读取权限,可能应用程序池中配置的用户没有bin文件夹的读取权限。您是否已经尝试将 nhibernate.cfg.xml 放在其他文件夹中,也许在 App_data 之类的其他文件夹中?

    【讨论】:

    • HttpContext.Current.Server.MapPath("bin\hibernate.cfg.xml") 就像一个魅力!谢谢。但对我来说似乎有点 MacGyver,因为我的 NHibernateHelper 课程在“Dal”(图书馆)项目中。我会尝试其他技巧。现在我收到另一个错误:登录失败,因为我使用的是“Integrated Security=SSPI”选项。不过没关系,我能应付。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多