简体   繁体   English

Nhibernate无法在生产环境中工作

[英]Nhibernate not working in production environment

I'm using C#.NET 3.5 Web App with NHibernate. 我正在将C#.NET 3.5 Web App与NHibernate一起使用。 Everything works well in development environment, but when I deploy the app in IIS, I get the following exception: 在开发环境中一切正常,但是当我在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

Here's my NHibernateHelper class: 这是我的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();
    }
}

And my Solution looks like this: 我的解决方案如下所示:

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

Other important information, is that I'm using XML mappings (not fluent or class mappings). 其他重要信息是,我正在使用XML映射(不是流利的或类的映射)。 I already tried: 我已经尝试过:

  • Put the "hibernate.cfg.xml" content in "Web.config" file (and, of course, changing parameters in "config.Configure()" method; 将“ hibernate.cfg.xml”内容放入“ Web.config”文件中(当然,还要在“ config.Configure()”方法中更改参数;
  • Not passing any values to "config.Configure()" method; 不将任何值传递给“ config.Configure()”方法;
  • Put a hardcoded string to "config.Configure()", like "config.Configure("bin\\hibernate.cfg.xml"); 将一个硬编码的字符串放入“ config.Configure()”,例如“ config.Configure(“ bin \\ hibernate.cfg.xml”);
  • Cry

I can't understand why my NHibernate configuration works in development but not in production. 我不明白为什么我的NHibernate配置可以在开发中工作而不能在生产中工作。 What am I doing wrong? 我究竟做错了什么?

Why not use HttpContext.Current.Server.MapPath to get the file path in the server. 为什么不使用HttpContext.Current.Server.MapPath来获取服务器中的文件路径。

Also check the folder permissions to execute and read, maybe the user configured in the application pool don't have permission to read in the bin folder. 还要检查文件夹权限以执行和读取,也许在应用程序池中配置的用户无权读取bin文件夹。 Do you already try to put the nhibernate.cfg.xml in other folder, maybe in other folder like App_data? 您是否已经尝试将nhibernate.cfg.xml放在其他文件夹中,也许放在App_data等其他文件夹中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlTableDependency 在生产环境中不起作用,但在测试环境中起作用 - SqlTableDependency not working in Production Environment but works in Test Environment .core mvc发布的应用程序局部视图在ASPNETCORE_ENVIRONMENT“生产”中不起作用,但在“开发”中工作良好 - .Core mvc published app partial views doesnt work in ASPNETCORE_ENVIRONMENT “production” but working fine with “development” 生成NHibernate生产模式的最佳实践 - Best practices to generate schema for production with NHibernate 在生产环境上部署Click一次 - Deploying Click once on production environment 生产环境transactionScope和ServiceBus的问题 - Problems with transactionScope and ServiceBus in production environment SaveFileDialog在生产环境中无法打开 - SaveFileDialog does not open in production environment 仅在生产环境中运行代码 - Run code only in production environment Summernote fonts 生产环境不可用 - Summernote fonts are unavailable in production environment ELMAH-在生产环境中未收到电子邮件 - ELMAH - Email not received in production environment 流利的NHibernate配置不起作用 - fluent nhibernate configuration not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM