4

I have a custom event system handler that sends an email when a publish transaction fails. The event is registered like this:

this.EventSystem.Subscribe<PublishTransaction, SaveEventArgs>    
    (this.SendNotificationEmail, EventPhases.TransactionCommitted);

The SendNotificationEmail method looks like this:

private void SendNotificationEmail(PublishTransaction transaction, 
    SaveEventArgs args, EventPhases phases)
{
    if (transaction.Instruction.ResolveInstruction.Purpose == ResolvePurpose.Publish && 
        transaction.State == PublishTransactionState.Failed)
    {
        // Send email
    }        
}

The issue I'm having is that the SendNotificationEmail method is called twice per publication transaction. This leads to multiple emails per failed transaction.

Any ideas why the event would be triggered more than once?

2
  • Are you publishing to multiple publishing targets? Apr 22, 2015 at 17:30
  • When I publish to multiple targets, the event is triggered twice per target. Apr 22, 2015 at 17:44

2 Answers 2

3

In general, the Save event is called for any save (change) of the PublishTransaction, not only for state changes. I'm not sure why a second save happens when the state is Failed (perhaps additional info is received from CD transport/deployment).

Anyways, your implementatiom should not rely on a single Save event, but detect the state change. You could do that by hooking into the Initiated phase of the save and, if the state is Failed, load another instance of the PublishTransaction to test if the state before the save event occurred was already Failed or not.

1
  • This makes sense. I was assuming (without any basis) that each event would only be called once per event phase. To solve my issue, I ended up caching each transaction id, and only sending the email the first time the event is triggered. Apr 22, 2015 at 17:51
1

While publishing an item, each publishing target results in a transaction. In your code you are subscribing SendNotificationEmail method per transaction.

So If you publish to a multiple publishing targets, the "SendNotificationEmail" method will be called for each target(or transaction) and if it fails, the email will be send per target(or transaction) and will result in multiple emails.

2
  • I'm getting two emails per target, though. If I publish to one target, I get 2 emails; if I publish to two targets, I get 4 emails, etc. Apr 22, 2015 at 17:42
  • If you publish to multiple targets, you will get multiple Publish Transactions (one per target). Apr 22, 2015 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.