Hello, We are upgrading from NSB5 to NSB7. And I am a bit confused about Unit Of Work behavior.
Are behaviors singletons or container scoped?
How do I get access to Sql Persistence connection (outbox is enabled) in a “public class UnitOfWorkBehavior : Behavior”?
How do I get access to my nested container instance so that I can setup my NHibernate session correctly for rest of the services in the above Unit of Work?
The builder you get on the context is the child builder but it cannot be modified. If you need to inject ISession into your services you have to either explicitly pass it to the services over method injection starting from the handle method or you need to create a binding on a func on the root container that resolves the session on-demand from an AsyncLocal that is set in a behavior.
It is very similar to the guidance we wrote for IBus
and you can have a look at how it is done with the UniformSession extension
To add to the information of @danielmarbach is that it seems you want to create your own NHibernate session based on the existing connection accessible via the context. Unfortunately, by default that isn’t possible with NHibernate. NHibernate can join/enlist to an existing TransactionScope but as these are not used you only have access to an existing IDbConnection and IDbTransaction which are managed by the SqlPersistence session.
You cannot create an NHibernate session based on a IDbConnection that already has a started a transaction. This is a NHibernate limitation. Maybe this can be resolved by using a custom factory to resolve the existing IDbTransaction but I’m not aware of any.
Thank you Ramon for pointing this out. I guess if we take NH dependency then we must use NH persistence to keep everything in one tx vis-a-vis outbox etc. With sql persistence, we would have to use something else or just plain ADO.NET.