Search code examples
workflowworkflow-foundation-4workflow-activityworkflowservice

Calling Code Activity Within Code Activity


How do I execute a code activity from within a code activity?

public sealed class ApprovalRequired : CodeActivity
{
    EmailActivity ea = new EmailActivity() // this is a code activity
    ea.Sender = ...
    ea.Rec = ...
    ea.Subject = "Approved"
   // ea.Execute() -- there is no way to call the execute method..
}

Solution

  • The easiest approach would be to prepare a XAML-based activity that has a sequence activity with your ApprovalRequired activity somewhere in it. Something like this:

    enter image description here

    Edit: To actually have an 'inner' activity be executed from another activity, your ApprovalRequired class should inherit from the NativeActivity class first, and override the CacheMetadata method to let the workflow application know to expect a child activity will be executed. The ApprovalRequired activity would look like this:

    namespace WCA.Scratch
    {
        using System.Activities;
    
        public sealed class ApprovalRequired : NativeActivity
        {
            public ApprovalRequired()
            {
                this.Email = new Email();
            }
    
            public Email Email
            {
                get;
                set;
            }
    
            protected override void CacheMetadata(NativeActivityMetadata metadata)
            {
                metadata.AddImplementationChild(this.Email);
            }
    
            protected override void Execute(NativeActivityContext context)
            {
                // Some logic here...
                this.Email.Body = "My email body.";
                this.Email.From = "[email protected]";
                this.Email.To = "[email protected]";
                this.Email.Subject = "Approval Request";
                context.ScheduleActivity(this.Email);
                // Some other logic here...
            }
        }
    }
    

    Keep in mind that you'll need to manually register any activity arguments or activity variables in the ApprovalRequired's CacheMetadata method as well.