Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 19, 2019 @ 08:25
    Matt Brailsford
    0

    Is there a way to run code on v8 ambient scope completion / abort?

    In v8 we have a ScopeProvider that provides the ability to create Scopes whenever you perform some DB transaction. I believe this is meant to be akin to the UoW pattern, however it seems to do a lot more "stuff".

    When you create a Scope, if no other scope exists, it sets the new scope as the ambient scope, then if you create more scopes (ie, you call more service methods etc that also create their own scopes) then those scopes become linked to the ambient scope and they all share the same DB transaction.

    In these child scopes, if any method should exit without calling Complete then the whole transaction rolls back.

    The thing I'm struggling to find is a way to know if the transaction completed or rolled back? What I'd like to do is create a series of event objects and register them with a custom (wrapped) scope, then only if the entire transaction succeeds, fire off the events.

    Anyone got any ideas how this can be achieved?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 19, 2019 @ 09:26
    Matt Brailsford
    100

    Ok, I think I've worked out a way.

    It appears Scope has an Events collection for which you can Dispatch an event from. From what I can tell, the default implementation of this Events property doesn't immediately dispatch those events (if it's not a cancelable event) instead it saves them and just executes them after the final transaction is complete (if it doesn't complete, they don't fire).

    I am kind of at the whim of the default umbraco implementation here though as if this queued event handler is ever changed it would break my implementation without me knowing. Unfortunately, the QueuedEventDispatcher is internal so I can't force this specific event dispatcher without duplicating it and creating my own.

Please Sign in or register to post replies

Write your reply to:

Draft