Implementing Windsor Scoped Lifestyle In Masstransit

1,039 views
Skip to first unread message

Mick Delaney

unread,
May 14, 2012, 9:43:56 AM5/14/12
to masstrans...@googlegroups.com
I'm trying to use Windsor Scoped lifestyles with MT. 

I'm using the ServiceBusConfigurator something like this:

sbc.BeforeConsumingMessage(() =>
{
//BeginScope container

var sessionFactory = container.Resolve<ISessionFactory>();
LazySessionContext.Bind(new Lazy<ISession>(() => BeginSession(sessionFactory)), sessionFactory);
});
 
sbc.AfterConsumingMessage(() =>
{
var sessionFactory = container.Resolve<ISessionFactory>();
var events = container.Resolve<IDispatchEvents>();
 
var session = LazySessionContext.UnBind(sessionFactory);
if (session == null)
{
return;
}
EndSession(session);
events.Dispatch();

//End Scope container
});

sbc.Subscribe(subs => subs.LoadFrom(container));


In the windsor docs it shows the following example:

Container.Register(Component.For<A>().LifestyleScoped());
 
using (Container.BeginScope())
{
    var a1 = Container.Resolve<A>();
    var a2 = Container.Resolve<A>();
    Assert.AreSame(a1, a2);
}


But I'm wondering where to put the Using statement in the ServiceBusConfigurator calls (above). 

I was thinking of storing it in CallContext or something. 

Is there a recommended way to do this sort of thing in MT??? 

Dru Sellers

unread,
May 14, 2012, 9:51:11 AM5/14/12
to masstrans...@googlegroups.com
it looks like we may not expose a great way for you to use the 'using' statement as the windsor docs would show.
We do have some 'NestedContainer' support for StructureMap, you could try looking there.


-d



--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/I2sU0vow0bMJ.
To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/masstransit-discuss?hl=en.

Mick Delaney

unread,
May 14, 2012, 9:53:46 AM5/14/12
to masstrans...@googlegroups.com
just noticed IConsumerFactory<TConsumer>

so thats the road to go down then, a Custom IConsumerFactory<TConsumer>, i can start with the existing Windsor one... 

cheers 
To post to this group, send email to masstransit-discuss@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.

Dru Sellers

unread,
May 14, 2012, 9:56:56 AM5/14/12
to masstrans...@googlegroups.com
yup. seems like it.
let me know how it goes.

-d

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/f087pIxNICMJ.

To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.

Mick Delaney

unread,
May 14, 2012, 2:26:49 PM5/14/12
to masstrans...@googlegroups.com
the custom IConsumerFactory is working nicely now... 

cheers for the quick response.... 
-d

To unsubscribe from this group, send email to masstransit-discuss+unsubscribe...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/masstransit-discuss?hl=en.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.

Dru Sellers

unread,
May 14, 2012, 2:56:45 PM5/14/12
to masstrans...@googlegroups.com
Excellent, happy to hear about that. :)
-d

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/JnoDmVLU95gJ.

To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.

Chris Patterson

unread,
May 15, 2012, 12:37:07 PM5/15/12
to masstrans...@googlegroups.com
Excellent, that's why the consumer factory design was applied.

Perhaps you could write up a quick doc that shows how to set it up (I'm sure others are interested in managing the lifecycle of their sessions, etc.) and we could add it to the container documentation?

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/JnoDmVLU95gJ.

To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.

Dru Sellers

unread,
May 15, 2012, 3:04:51 PM5/15/12
to masstrans...@googlegroups.com
maybe even add it as a consumer factory option???

-d

Mick Delaney

unread,
May 16, 2012, 4:41:10 AM5/16/12
to masstrans...@googlegroups.com
thats what i was thinking, i could extend the windsor package, & document of course. 
right now i'm just testing/fleshing it out... should be in production on monday, 
i'll then set aside some time to fit it into the windsor package..
-d

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/JnoDmVLU95gJ.

To post to this group, send email to masstransit-discuss@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/masstransit-discuss?hl=en.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.

Dru Sellers

unread,
May 16, 2012, 8:15:40 AM5/16/12
to masstrans...@googlegroups.com
If you have any questions let me know. :)

-d

To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/J5bOmOxI7U0J.
To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.

Dru Sellers

unread,
Jun 15, 2012, 8:15:30 AM6/15/12
to masstrans...@googlegroups.com
Can you do a perf analysis of the change? I would be curious to know.

Also, maybe you could put together a pull request for us?

I have no reason not to.

-d

On Fri, Jun 15, 2012 at 3:54 AM, Paul Cox <paul...@icompile.co.uk> wrote:
Is there any disadvantage to just amending the existing WindsorConsumerFactory so that this is the same as the StructureMapConsumerFactory?

We've implemented this in our project and essentially the only change is to wrap the current code in GetConsumer with a using block to begin a new scope.

using (_container.BeginScope())
{
    // Existing code
}

Paul.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.

Gian Marco Gherardi

unread,
Jan 18, 2013, 4:08:00 AM1/18/13
to masstrans...@googlegroups.com
After some experiment, i ended up creating a new custom scope (called "MessageScope"):

public class MessageScope : IScopeAccessor
{
[ThreadStatic] private static ILifetimeScope lifetimeScope;

public static void BeginScope()
{
if (lifetimeScope != null)
{
throw new InvalidOperationException("already in scope");
}
lifetimeScope = new DefaultLifetimeScope();
}

public static void EndScope()
{
if (lifetimeScope == null)
{
throw new InvalidOperationException("not in scope");
}
lifetimeScope.Dispose();
lifetimeScope = null;
}

public void Dispose()
{
if (lifetimeScope != null)
{
lifetimeScope.Dispose();
lifetimeScope = null;
}
}

public ILifetimeScope GetScope(CreationContext context)
{
if (lifetimeScope == null)
{
throw new InvalidOperationException("Scope was not available. Did you forget to call MessageScope.BeginScope()?");
}
return lifetimeScope;
}
}

To enable MessageScope, ServiceBus must be configured, eg:

ServiceBusFactory.New(cfg => {
// ....
cfg.BeforeConsumingMessage(MessageScope.BeginScope);
cfg.AfterConsumingMessage(MessageScope.EndScope);
// ....
});

And component must be defined with LifestyleScoped=MessageScope, eg:

container.Register(Component.For<MyComponent>().LifestyleScoped<MessageScope>());

Hope this help

On Friday, June 15, 2012 2:15:30 PM UTC+2, Dru wrote:
Can you do a perf analysis of the change? I would be curious to know.

Also, maybe you could put together a pull request for us?

I have no reason not to.

-d

On Fri, Jun 15, 2012 at 3:54 AM, Paul Cox <paul...@icompile.co.uk> wrote:
Is there any disadvantage to just amending the existing WindsorConsumerFactory so that this is the same as the StructureMapConsumerFactory?

We've implemented this in our project and essentially the only change is to wrap the current code in GetConsumer with a using block to begin a new scope.

using (_container.BeginScope())
{
    // Existing code
}

Paul.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/tx6UMHAB41IJ.

To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.

Chris Patterson

unread,
Jan 22, 2013, 11:13:06 AM1/22/13
to masstrans...@googlegroups.com
There is also the possibility to intercept a consumer registration, so that you can delegate pre-post handling of the consumer for the purpose of wrapping up unit-of-work or other scope concerns (note that in this case, the factory method for the consumer is called before the delegate, so you would not have the same level of control of the consumer construction, however.




To unsubscribe from this group, send email to masstransit-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/QSfejLD5llAJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Chris Patterson

unread,
Jan 22, 2013, 1:21:34 PM1/22/13
to masstrans...@googlegroups.com
Support for this in Windsor has just been added to the /develop branch of MassTransit. Use the .EnableMessageScope() extension method on the service bus configurator, and register your components as scoped using <MessageScope>() and you should be all set.

Gian Marco Gherardi

unread,
Jan 22, 2013, 2:01:43 PM1/22/13
to masstrans...@googlegroups.com
Wonderful, thanks Chris!

Gian Marco Gherardi
http://gianmarco.gherardi.me

Ran Trifon

unread,
Mar 13, 2013, 11:53:43 AM3/13/13
to masstrans...@googlegroups.com

Hi Chris,

Is there any plans to merge the MessageScope bits to the master branch and publish it to NuGet.
I really need this feature, and I prefer to work on official release and not compile it by my self.

Thanks again for the great work.

Ran
Wonderful, thanks Chris!
-d


To unsubscribe from this group, send email to masstransit-discuss+unsubscribe...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/masstransit-discuss?hl=en.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/masstransit-discuss/-/QSfejLD5llAJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To post to this group, send email to masstrans...@googlegroups.com.
To unsubscribe from this group, send email to masstransit-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ran Trifon

unread,
Mar 13, 2013, 12:47:17 PM3/13/13
to masstrans...@googlegroups.com

Sorry for my last post, it is already the MessageScope is already in.
Don't know how i miss it.
Reply all
Reply to author
Forward
0 new messages