[WF4]Using WF4 Customized Extension

Here is a simple WF4 extension:

   public class SimpleExtension : IWorkflowInstanceExtension {

        private WorkflowInstanceProxy instance;

        public IEnumerable<object> GetAdditionalExtensions() {

            return null;

        }

        public void SetInstance(WorkflowInstanceProxy instance) {

            this.instance = instance;

        }

        public void DoSomething() {

            Console.WriteLine("Extension is doing something...");

        }

    }

1. Adding extension in host level
    a. An activity which will use this extension:      

    public class MyActivityWithExtension : NativeActivity {

        protected override void Execute(NativeActivityContext context) {

            var extension = context.GetExtension<SimpleExtension>();

            extension.DoSomething();

        }

    }

 

     b. Host         

    public class WF4Extesions {

        public void Run() {

            AutoResetEvent waithandler = new AutoResetEvent(false);

            WorkflowApplication wfApp = new WorkflowApplication(

                new Sequence() {

                    Activities = {

                        new WriteLine(){Text="Workflow Start"},

                        new MyActivityWithExtension(),

                        new WriteLine(){Text="Workflow End"}

                    }

                }

            );

            wfApp.Extensions.Add(new SimpleExtension());

            wfApp.Unloaded = (e) => {

                waithandler.Set();

            };

            wfApp.Run();

            waithandler.WaitOne();

        }

    }

 

2. Adding extension in activity level

 

   a. An activity which will add extension into workflow.       

    public class AddExtension : NativeActivity {

        protected override void CacheMetadata(NativeActivityMetadata metadata) {

            metadata.AddDefaultExtensionProvider<SimpleExtension>(() => new SimpleExtension());

        }

        protected override void Execute(NativeActivityContext context) {

            Console.WriteLine("Extension is added");

        }

    }

    b. An activity which will use extension(the same activity will used in section 1):
        public class MyActivityWithExtension : NativeActivity {

        protected override void Execute(NativeActivityContext context) {

            var extension = context.GetExtension<SimpleExtension>();

            extension.DoSomething();

        }

    }

     c.Host:      

    public class WF4Extesions {

        public void Run() {

            AutoResetEvent waithandler = new AutoResetEvent(false);

            WorkflowApplication wfApp = new WorkflowApplication(

                new Sequence() {

                    Activities = {

                        new WriteLine(){Text="Workflow Start"},

                        new AddExtension(),

                        new MyActivityWithExtension(),

                        new WriteLine(){Text="Workflow End"}

                    }

                }

            );

            wfApp.Unloaded = (e) => {

                waithandler.Set();

            };

            wfApp.Run();

            waithandler.WaitOne();

        }

    }

When we have a xamlx worklfow service , we can drop such a workflow service into IIS virtual directory directly ,and it seems not easy to add extension in the host level. however, we can use an AddExtension activity to add a extension to a workflow instance. Please note that you can only add one extension instence once.  

    public class MyExtension:IWorkflowInstanceExtension {
        private WorkflowInstanceProxy instance;
        public IEnumerable<object> GetAdditionalExtensions() {
            return null;
        }

        public void SetInstance(WorkflowInstanceProxy instance) {
            this.instance = instance;
        }

        public void WaitSome(Bookmark bookmark) {
            ThreadPool.QueueUserWorkItem(state => {
                Thread.Sleep(5000);
                var iss = 
instance.BeginResumeBookmark(bookmark, 42, nullnull);
                var result = instance.EndResumeBookmark(iss);
                Console.WriteLine("BookmarkResumptionResult:" + result);
            });
        }
    }
    public class MyBookmarkedActivity : NativeActivity {
        protected override bool CanInduceIdle {
            get {
                return true;
            }
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata) {
            metadata.AddDefaultExtensionProvider<MyExtension>(() => 
new MyExtension());
        }

        protected override void Execute(NativeActivityContext context) {
            Console.WriteLine("execution start");
            var bookmark = 
context.CreateBookmark("MyBookmark", BookmarkResumed);
            var extension = context.GetExtension<MyExtension>();
            extension.WaitSome(bookmark);
            Console.WriteLine("Exection end;");
        }

        private void BookmarkResumed(NativeActivityContext context, 
               Bookmark bookmark, 
               object value) {
            Console.WriteLine("Bookmark resumed with '{0}'.", value);
        }
    }

 

blog comments powered by Disqus