Scope Per Consumer

485 views
Skip to first unread message

Mick Delaney

unread,
Mar 18, 2016, 12:03:17 PM3/18/16
to masstransit-discuss
Hi,

In the process of upgrading from 2x to 3x and have a requirement to scope various objects to a consumer. 

By scope I mean a Castle Windsor Scoped Object

for example:

container.Register(Component.For<Context>().Scoped())

ReadModelHandler : IConsumer<CreatedEvent>
  Context

NotifyHandler : IConsumer<CreatedEvent>
  Context

Both consumers live in the same process/same endpoint. 

I want to apply a filter before which will manage a transaction & setup some other data on the Context. 

What I'm seeing is that the scope & as a result the Context is being shared across both consumers.

Is this the expected behaviour ?




Chris Patterson

unread,
Mar 18, 2016, 12:33:41 PM3/18/16
to masstrans...@googlegroups.com
How are you registering your consumer? If you're using the WindsorConsumerFactory(), it will create a scope for the consumer before resolving the consumer from the scope.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/e84575a5-9f15-4aa5-9313-e5c835aa39f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mick Delaney

unread,
Mar 18, 2016, 1:51:04 PM3/18/16
to masstransit-discuss
that was it, thanks chris!!!


On Friday, 18 March 2016 16:33:41 UTC, Chris Patterson wrote:
How are you registering your consumer? If you're using the WindsorConsumerFactory(), it will create a scope for the consumer before resolving the consumer from the scope.

On Fri, Mar 18, 2016 at 9:03 AM, Mick Delaney <mickd...@gmail.com> wrote:
Hi,

In the process of upgrading from 2x to 3x and have a requirement to scope various objects to a consumer. 

By scope I mean a Castle Windsor Scoped Object

for example:

container.Register(Component.For<Context>().Scoped())

ReadModelHandler : IConsumer<CreatedEvent>
  Context

NotifyHandler : IConsumer<CreatedEvent>
  Context

Both consumers live in the same process/same endpoint. 

I want to apply a filter before which will manage a transaction & setup some other data on the Context. 

What I'm seeing is that the scope & as a result the Context is being shared across both consumers.

Is this the expected behaviour ?




--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

Mick Delaney

unread,
Mar 18, 2016, 2:22:25 PM3/18/16
to masstransit-discuss
sorry chris, 
is there a way to apply a filter to each consumer that has access to that scope ??
regards

Chris Patterson

unread,
Mar 18, 2016, 2:30:52 PM3/18/16
to masstrans...@googlegroups.com
The scope is created for the consumer itself as part of the consumer factory. So if you had multiple consumers on the same receive endpoint for the same message, a separate scope would be created and disposed for each consumer.

There isn't currently a scope that is per-message that is shared by all downstream consumers, as in there is no filter with a shared named scope. The threaded nature of the TPL and the lack of a custom scope support in Windsor to make this work has made this hard to figure out. I tried several approaches and finally just gave up. The thread static methods are all not the way to do it. 

__
Chris Patterson




To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Chris Patterson

unread,
Mar 20, 2016, 7:14:15 PM3/20/16
to masstrans...@googlegroups.com
So if you add the "UseMessageScope()" to your receive endpoint configurator, it will inject a message scope that seems to work as you're expecting:


You can see that the dependency is scoped to the message scope, which is established by the filter created by UseMessageScope(). So the middleware filter should create and destroy it as expected.


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/e84575a5-9f15-4aa5-9313-e5c835aa39f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mick Delaney

unread,
Mar 21, 2016, 8:26:10 AM3/21/16
to masstransit-discuss
I think i've done a bad job explaining what i need. 
but thanks for being so responsive.

what i have basically is the same endpoint with 2 consumers of the same message, i want them both to have a separate scope. 
i was seeing the shared scope issue because I was using UseMessageScope & LoadFrom container. 
but if i just use LoadFrom then I get the separate scope & I can use my filters on that scope as expected. 

still interesting to know there's the potential to share resources across handlers, but i dont have a need for it yet. 


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

Mick Delaney

unread,
Mar 21, 2016, 9:30:12 AM3/21/16
to masstransit-discuss
ok, so i've created a gist to show you the issue: 


its based on the example you provided. 

the issue is, i dont see how i can get a scope available in a Filter.





On Sunday, 20 March 2016 23:14:15 UTC, Chris Patterson wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

Mick Delaney

unread,
Mar 21, 2016, 10:20:57 AM3/21/16
to masstransit-discuss
the only solution i can see now is to create my own ConsumerFactory, which is what I did in MT2x 
so am going to go with that otherwise i'll need to rewrite all my handlers. 

Chris Patterson

unread,
Mar 21, 2016, 12:03:41 PM3/21/16
to masstrans...@googlegroups.com
That was going to be my response, if you want to do more than the stock consumer factory, just create your own based on the one provided. I actually think you can provide filters that run within the scope of the consumer factory, but I can't remember how to create them right now. And it would be message-type specific, and I think you want something more generic.

You might be able to add a ConsumerConsumeContext type of filter, but I'd have to go back and look closer.

The consumer factory is the easiest option, since you're adding to the base behavior. I'll work on making middleware pluggable at that level more easily.


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Mick Delaney

unread,
Mar 21, 2016, 1:11:17 PM3/21/16
to masstransit-discuss
Thanks Chris, i think it'll be pretty useful to add filters at that level but for now I'm happy to use the ConsumerFactory. 
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages