0

In my web.api application I use EntityFramework 6 (EF6), MediatR, and Autofac. I want to Create EF6 DbContext with a lifetime matching MediatR RequestHandlers so that I obtain a unit of work per RequestHandler invocation.I've been looking into Autofac concepts like InstancePerMatchingLifteTimeScope, Owned< Dependency > and also MediatR Handler Decoration using MediatR Pipeline.

I haven't found any solution yet. It seems I need to wrap both Handler Creation and handler invocation inside a AutofacLifeTimeScope tagged with a wellknown-object, so I can register IDBContext with InstancePerMatchingLifeTimeScope(wellknown-object).

Maybe You can propose another solution ?

  • 1
    Isn't per handler the same thing as per HTTP request? Do you have several handlers running per request? – Mickaël Derriey Mar 02 '17 at 17:23
  • I will have only one command handler ,but might have several EventHandlers kicking in. So in effect I will have more than one handler pr request yes. I know one should normally only have one transaction pr request. The best lead i have found until now is [this post](http://stackoverflow.com/questions/31230305/autofac-lifetime-scope-decorator) – Christian Johansen Mar 03 '17 at 23:31
  • So during the HTTP request, one command will be sent. It will be handled by one command handler. That command handler might publish several events, and you wants the handlers of each of these events to be in their own lifetime scope - i.e. if you have 6 events, you want to create 6 lifetime scopes? – Mickaël Derriey Mar 12 '17 at 22:27
  • yes that's right. – Christian Johansen Mar 14 '17 at 13:01
  • I thought I had as solution but the question is still valid. The tricky thing is to resolve a concrete EventHandler from the Decorator. The Decorator needs type information. – Christian Johansen May 30 '17 at 13:29

0 Answers0