Solutions    |    Downloads    |    License    |    Documentation    |    Training    |    Support    |    Customers    |    About Us

NServiceBus Support For Child Containers

Last Updated: Jan 23, 2013 08:39AM IST
NServiceBus Support For Child Containers

Child containers are essentially a snapshot of the main container where transient instances are treated as singleton within the scope of the child container.
 

This is useful when you’d want to scope instances for the duration of a web request or the handling of a message in NServiceBus. While this was possible before, child containers brings one more important feature to the table.


Be aware that child containers are not supported by spring.net so if you plan to take advantage of this you’d have to use one of the other containers supported by NServiceBus.

Deterministic disposal


Instance lifetime is usually not tracked by the container (Windsor is doing it though) and that means that you’d have to manually call dispose on any instance that needs deterministic disposal. Child containers solves this issue by automatically disposing all transient objects created within each specific child container.
This is very handy when it comes to manage things like the NHibernate session. A more in depth description of child containers can be found here.
 

NServiceBus creates a child container for each transport message that is received, remember that transport messages can contain multiple “user defined messages”.
This means that all transient instances that are created during that message processing is in scoped as singletons within the child container.
This allows you to easily share for example the NHibernate session between repositories without mucking around with thread static caching.
 

When the message has finished processing the child container and all transient instances are disposed. So if you need deterministic disposal just implement IDisposable.
 

Beginning NServiceBus V3, you can get a “session per transport message” by configuring the session as transient. The example uses StructureMap.
 

var container = new Container(x =>
{
    x.ForSingletonOf<ISessionFactory>().Use(ConfigureSessionFactory());
    x.For<ISession>().Use(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());
});


This will allow you to inject your session to all components involved in processing each message


public class NHibernateMessageHandler:IHandleMessages<NHibernateMessage>
{
    readonly ISession session;

    public NHibernateMessageHandler(ISession session)
    {
        this.session = session;
    }

    public void Handle(NHibernateMessage message)
    {
        session.Save(new PersistentEntity
            {
                Data = "Whatever " + DateTime.Now.ToShortTimeString()
            });
    }
}


And when the message is processed the session will be disposed and all resources like database connections etc are released.


Child containers are a powerful feature that can simplify your code and should definitely be in your toolbox.


If you configure your components using the NServiceBus configure API, it's possible to configure instance lifecyle to be per unit of work using:

	Configure.Instance.Configurer.ConfigureComponent<MyComponent>(DependencyLifecycle
.InstancePerUnitOfWork);
About NServiceBus    |    Contact Us    |    Privacy    |    Follow us on:   
Copyright 2010-2013 NServiceBus. All rights reserved
NSB_Y@yahoo.com
http://assets1.desk.com/r1046ffeaa2233e531563a32d7edef6677d8a78b5/javascripts/
nservicebus
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise