Can an exception during OnRelease cause the component to not be removed correctly?

I have the following connection scheme for NHibernate ISession in Autofac for an ASP.NET application:

builder.RegisterAdapter<ISessionFactory, ISession>(factory => factory.OpenSession())
    .InstancePerHttpRequest()
    .OnActivated(activatedArgs =>
                 {
                     var session = activatedArgs.Instance;
                     session.BeginTransaction();
                 })
    .OnRelease(session =>
               {
                   if (session.Transaction != null && session.Transaction.IsActive)
                   {
                       try
                       {
                           session.Transaction.Commit();
                       }
                       catch(Exception e)
                       {
                           session.Transaction.Rollback();
                           throw;
                       }
                   }
               });

Will the session be properly deleted even with an excluded throw to the commit? Is this the correct use of ISession with autofac?

+3
source share
2 answers

Cannot be thrown in Dispose()not very well with Autofac. Proper disposal of instances of other components is not guaranteed.

, - , WCF , . , Dispose() , .

Edit:

- , try/catch Autofac. , OnRelease() ? . - Autofac, ? , , .

, , .

+2

NHibernate ISession Autofac, Dispose(), . finally catch .

0

Source: https://habr.com/ru/post/1795082/


All Articles