Nhibnerate .net Core 3.1 Initialisation

117 views Asked by At

I have a problem which I have never come across previously (I have been using NHibernate with .NET framework for many many years)

In my current project I am creating my session factory Fluently.Configure().....BuildSessionFactory() and adding it to my DI, in the ConfigureServices method (.net core 3.1)

that factory is later used to open the session and execute sql .

In the happy path this is great no problems, all works great.

The problem is, if there is an issue with the sql instance - like the database is taken offline, the startup of the website at the point when BuildSessionFactory() executes will error. This then fails to add a session factory to the DI and the site fails to load. Further more, because the initialisation of the site fails, it cannot redirect to a custom error page.

And further to this, in .NET core 3 the initialisation will not be retried., therefore I get the same error regardless of if the database is now available.

When trying to work around the issue and investigate it, I did manage to hackily replace the session factory (on error) with a temporary fake one (default implementation), to circumvent the initialisation of the app. And then further down the line redirect to the error page if this instance was found. But obviously, then i'm stuck in this situation until the app pool is recycled and the process starts again.

Is there a reason that the DB connection is made at this initialisation point - would there be a way to move this part outside of the creation of the factory, so that in subsequent requests its able to retry?

1

There are 1 answers

0
sianabanana On

https://github.com/nhibernate/nhibernate-core/discussions/2638

Helpfully someone answered my question on the nhibernate forum.

solution was to add

.ExposeConfiguration(cfg =>
{
cfg.SetProperty("hbm2ddl.keywords", "none");
})

I also added

Dialect dialect = Dialect.GetDialect(cfg.GetDerivedProperties());
SchemaMetadataUpdater.QuoteTableAndColumns(cfg, dialect);

hoping that will quote tables and columns but i havent tested that