开发者

Exception Handling with Structuremap

开发者 https://www.devze.com 2022-12-11 13:17 出处:网络
Is there anyway to recover gracefully in an ASP.NET MVC application if the database is not found for some reason when I try to get an instance of my NHibernate Session from Structuremap?

Is there anyway to recover gracefully in an ASP.NET MVC application if the database is not found for some reason when I try to get an instance of my NHibernate Session from Structuremap?

public static class StructureMap
{
    private static Configuration Cfg
    {
        get
        {
            var configuration = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
            configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, "MyConnectionString");
            return configuration;
  开发者_如何学Python      }
    }

    public static void ConfigureStuctureMap()
    {
        ObjectFactory.Initialize(x =>
             {
                 x.ForRequestedType<ISessionFactory>()
                     .CacheBy(InstanceScope.Singleton)
                     .TheDefault.Is.ConstructedBy(Cfg.BuildSessionFactory);

                 x.ForRequestedType<ISession>()
                     .CacheBy(InstanceScope.HttpContext)
                     .TheDefault.Is.ConstructedBy(c => c.GetInstance<NHibernateSessionFactory>().GetCurrentSession());

             });
    }
}

Right now the application will display a runtime error YSOD. I have a custom static html error page setup in the web config but it doesn't display even though I have the custom errors mode set to "On"


Catch any NHibernate/StructureMap initialization errors, and override your controller factory to handle the case when it is impossible to create a controller that depends on StructureMap-related dependencies. This will allow you to handle errors and redirect to the action/controller which does not depend on neither NHibernate nor StructureMap - either from controller factory or using [HandleError] attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消