c# - Can an exception during OnRelease cause a component to not be disposed correctly? -
i've got following code wiring nhibernate isession in autofac 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 session disposed thrown exception in commit? correct usage of isession autofac?
no- throwing in dispose()
isn't idea autofac. correct disposal of other component instances isn't guaranteed.
in general should avoided - wcf example has known , long-standing usability problem because connections throw during disposal. basic antipattern dispose()
called because exception propagating. throwing further exception masks original 1
edit:
as thought experiment - let's supported using try/catch magic in autofac. happens if onrelease()
throws 2 different components? can't propagate both exceptions. further - once exception has bubbled out of autofac, can catch it? of components servicing request have been released.
hope helps, nick.
Comments
Post a Comment