Adding object to consumer thread state

19 views
Skip to first unread message

Brian Colwell

unread,
Jul 18, 2017, 1:16:59 PM7/18/17
to masstransit-discuss
I am attempting to add an NHibernate ISession object to a Consumer thread.  I am not interested in injecting ISessionFactory or ISession to the Consumer class constructor.  I would like to open the ISession before consume and dispose it after consume.  I thought I had a solution with IConsumeMessageObserver but the async nature of MT causes the PreConsume to happen on a different thread than Consume.  I'd like to do something similar to web api pipeline configuration like ActionFilterAttribute OnActionExecuting and OnActionExecuted which use the same thread before, during, and after execution.  I also looked at using a transaction which seems to support storage across threads but this doesn't seem to fit with my use case.

I'm sure this has been answered before but I have not been able to find what I'm looking for.  Thanks in advance.

Chris Patterson

unread,
Jul 18, 2017, 6:33:50 PM7/18/17
to masstrans...@googlegroups.com
you should look at adding payloads to the ConsumeContext, so they are available to the consumer. Using thread static elements is always going to fail going forward as more async code becomes common.

Take a look at this to get started:


On Tue, Jul 18, 2017 at 10:16 AM, Brian Colwell <bst...@gmail.com> wrote:
I am attempting to add an NHibernate ISession object to a Consumer thread.  I am not interested in injecting ISessionFactory or ISession to the Consumer class constructor.  I would like to open the ISession before consume and dispose it after consume.  I thought I had a solution with IConsumeMessageObserver but the async nature of MT causes the PreConsume to happen on a different thread than Consume.  I'd like to do something similar to web api pipeline configuration like ActionFilterAttribute OnActionExecuting and OnActionExecuted which use the same thread before, during, and after execution.  I also looked at using a transaction which seems to support storage across threads but this doesn't seem to fit with my use case.

I'm sure this has been answered before but I have not been able to find what I'm looking for.  Thanks in advance.

--
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 masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/3d23c518-b539-4a18-83ce-e05fa7b1c70f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Colwell

unread,
Jul 19, 2017, 5:23:58 PM7/19/17
to masstransit-discuss
Thanks Chris.  If I understand correctly, the fundamental design of NH (session per thread) and the move away from "static elements" in MT make my goal unreachable.  TransactionContext is a nice storage mechanism but not a thread static one.  I will move forward in a different direction.


On Tuesday, July 18, 2017 at 6:33:50 PM UTC-4, Chris Patterson wrote:
you should look at adding payloads to the ConsumeContext, so they are available to the consumer. Using thread static elements is always going to fail going forward as more async code becomes common.

Take a look at this to get started:

On Tue, Jul 18, 2017 at 10:16 AM, Brian Colwell <bst...@gmail.com> wrote:
I am attempting to add an NHibernate ISession object to a Consumer thread.  I am not interested in injecting ISessionFactory or ISession to the Consumer class constructor.  I would like to open the ISession before consume and dispose it after consume.  I thought I had a solution with IConsumeMessageObserver but the async nature of MT causes the PreConsume to happen on a different thread than Consume.  I'd like to do something similar to web api pipeline configuration like ActionFilterAttribute OnActionExecuting and OnActionExecuted which use the same thread before, during, and after execution.  I also looked at using a transaction which seems to support storage across threads but this doesn't seem to fit with my use case.

I'm sure this has been answered before but I have not been able to find what I'm looking for.  Thanks in advance.

--
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.

Alexey Zimarev

unread,
Jul 20, 2017, 2:04:31 PM7/20/17
to masstransit-discuss
Injecting ISession to the consumer class is the valid option, an alternative described by Chris also works - use custom middleware and store session in the context. We use it to keep RavenDb session and it works just fine.
Reply all
Reply to author
Forward
0 new messages