WorkflowCompensationBehavior.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Statements / WorkflowCompensationBehavior.cs / 1305376 / WorkflowCompensationBehavior.cs

                            //---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------
namespace System.Activities.Statements
{ 
    using System;
    using System.Collections.Generic; 
    using System.Runtime; 
    using System.Threading;
    using System.Collections.ObjectModel; 

    sealed class WorkflowCompensationBehavior : NativeActivity
    {
        Variable currentCompensationToken; 

        public WorkflowCompensationBehavior() 
            : base() 
        {
            currentCompensationToken = new Variable 
                {
                    Name = "currentCompensationToken",
                };
 
            DefaultCompensation = new DefaultCompensation()
                { 
                    Target = new InArgument(this.currentCompensationToken), 
                };
 
            DefaultConfirmation = new DefaultConfirmation()
                {
                    Target = new InArgument(this.currentCompensationToken),
                }; 
        }
 
        Activity DefaultCompensation 
        {
            get; 
            set;
        }

        Activity DefaultConfirmation 
        {
            get; 
            set; 
        }
 
        protected override bool CanInduceIdle
        {
            get
            { 
                return true;
            } 
        } 

        protected override void CacheMetadata(NativeActivityMetadata metadata) 
        {
            Fx.Assert(this.DefaultCompensation != null, "DefaultCompensation must be valid");
            Fx.Assert(this.DefaultConfirmation != null, "DefaultConfirmation must be valid");
            metadata.SetImplementationChildrenCollection( 
                new Collection
                { 
                    this.DefaultCompensation, 
                    this.DefaultConfirmation
                }); 

            metadata.SetImplementationVariablesCollection(new Collection { this.currentCompensationToken });
        }
 
        protected override void Execute(NativeActivityContext context)
        { 
            Bookmark mainRootCompleteBookmark = context.CreateBookmark(OnMainRootComplete, BookmarkOptions.NonBlocking); 
            context.RegisterMainRootCompleteCallback(mainRootCompleteBookmark);
 
            CompensationExtension compensationExtension = context.GetExtension();
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid");

            compensationExtension.WorkflowCompensation = context.CreateBookmark(new BookmarkCallback(OnCompensate)); 
            compensationExtension.WorkflowConfirmation = context.CreateBookmark(new BookmarkCallback(OnConfirm));
 
            Fx.Assert(compensationExtension.WorkflowCompensationScheduled != null, "compensationExtension.WorkflowCompensationScheduled bookmark must be setup by now"); 
            context.ResumeBookmark(compensationExtension.WorkflowCompensationScheduled, null);
        } 

        protected override void Cancel(NativeActivityContext context)
        {
            context.CancelChildren(); 
        }
 
        void OnMainRootComplete(NativeActivityContext context, Bookmark bookmark, object value) 
        {
            CompensationExtension compensationExtension = context.GetExtension(); 
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid");

            CompensationTokenData rootHandle = compensationExtension.Get(CompensationToken.RootCompensationId);
            Fx.Assert(rootHandle != null, "rootToken must be valid"); 

            ActivityInstanceState completionState = (ActivityInstanceState)value; 
 
            if (completionState == ActivityInstanceState.Closed)
            { 
                context.ResumeBookmark(compensationExtension.WorkflowConfirmation, new CompensationToken(rootHandle));
            }
            else if (completionState == ActivityInstanceState.Canceled)
            { 
                context.ResumeBookmark(compensationExtension.WorkflowCompensation, new CompensationToken(rootHandle));
            } 
            else if (completionState == ActivityInstanceState.Faulted) 
            {
                // Do nothing. Neither Compensate nor Confirm. 
                // Remove the bookmark to complete the WorkflowCompensationBehavior execution.
               context.RemoveBookmark(compensationExtension.WorkflowConfirmation);
               context.RemoveBookmark(compensationExtension.WorkflowCompensation);
            } 
        }
 
        void OnCompensate(NativeActivityContext context, Bookmark bookmark, object value) 
        {
            CompensationExtension compensationExtension = context.GetExtension(); 
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid");

            CompensationToken rootToken = (CompensationToken)value;
            Fx.Assert(rootToken != null, "rootToken must be passed"); 

            this.currentCompensationToken.Set(context, rootToken); 
 
            CompensationTokenData rootTokenData = compensationExtension.Get(rootToken.CompensationId);
            if (rootTokenData.ExecutionTracker.Count > 0) 
            {
                context.ScheduleActivity(DefaultCompensation, new CompletionCallback(OnCompensationComplete));
            }
            else 
            {
                OnCompensationComplete(context, null); 
            } 
        }
 
        void OnCompensationComplete(NativeActivityContext context, ActivityInstance completedInstance)
        {
            // Remove bookmark.... have a cleanup book mark method...
            CompensationExtension compensationExtension = context.GetExtension(); 
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid");
 
            context.RemoveBookmark(compensationExtension.WorkflowConfirmation); 
        }
 
        void OnConfirm(NativeActivityContext context, Bookmark bookmark, object value)
        {
            CompensationExtension compensationExtension = context.GetExtension();
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid"); 

            CompensationToken rootToken = (CompensationToken)value; 
            Fx.Assert(rootToken != null, "rootToken must be passed"); 

            this.currentCompensationToken.Set(context, rootToken); 

            CompensationTokenData rootTokenData = compensationExtension.Get(rootToken.CompensationId);
            if (rootTokenData.ExecutionTracker.Count > 0)
            { 
                context.ScheduleActivity(DefaultConfirmation, new CompletionCallback(OnConfirmationComplete));
            } 
            else 
            {
                OnConfirmationComplete(context, null); 
            }
        }

        void OnConfirmationComplete(NativeActivityContext context, ActivityInstance completedInstance) 
        {
            CompensationExtension compensationExtension = context.GetExtension(); 
            Fx.Assert(compensationExtension != null, "CompensationExtension must be valid"); 

            context.RemoveBookmark(compensationExtension.WorkflowCompensation); 
        }
    }
}
 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK