protected override void Execute(NativeActivityContext context) { ExceptionPersistenceExtension extension = context.GetExtension <ExceptionPersistenceExtension>(); if ((extension != null) && !extension.PersistExceptions) { // We will need a NoPersistProperty if we catch an exception. NoPersistProperty noPersistProperty = context.Properties.FindAtCurrentScope(NoPersistProperty.Name) as NoPersistProperty; if (noPersistProperty == null) { noPersistProperty = new NoPersistProperty(context.CurrentExecutor); context.Properties.Add(NoPersistProperty.Name, noPersistProperty); } } this.state.Set(context, new TryCatchState()); if (this.Try != null) { context.ScheduleActivity(this.Try, new CompletionCallback(OnTryComplete), new FaultCallback(OnExceptionFromTry)); } else { OnTryComplete(context, null); } }
void OnExceptionFromTry(NativeActivityFaultContext context, Exception propagatedException, ActivityInstance propagatedFrom) { if (propagatedFrom.IsCancellationRequested) { if (TD.TryCatchExceptionDuringCancelationIsEnabled()) { TD.TryCatchExceptionDuringCancelation(this.DisplayName); } // The Try activity threw an exception during Cancel; abort the workflow context.Abort(propagatedException); context.HandleFault(); } else { Catch catchHandler = FindCatch(propagatedException); if (catchHandler != null) { if (TD.TryCatchExceptionFromTryIsEnabled()) { TD.TryCatchExceptionFromTry(this.DisplayName, propagatedException.GetType().ToString()); } context.CancelChild(propagatedFrom); TryCatchState state = this.state.Get(context); // If we are not supposed to persist exceptions, enter our noPersistScope ExceptionPersistenceExtension extension = context.GetExtension <ExceptionPersistenceExtension>(); if ((extension != null) && !extension.PersistExceptions) { NoPersistProperty noPersistProperty = (NoPersistProperty)context.Properties.FindAtCurrentScope(NoPersistProperty.Name); if (noPersistProperty != null) { // The property will be exited when the activity completes or aborts. noPersistProperty.Enter(); } } state.CaughtException = context.CreateFaultContext(); context.HandleFault(); } } }