Home > WF4

[WF4]Using WF4 Customized Extension

6. September 2010

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);
        }
    }

 

WF4

Comments

10/4/2010 6:50:16 AM #
I have a few extra questions . Would you mind answering them if I ask here or shall I ask on email? Looking forward to more great content, thanks!
10/4/2010 6:38:43 PM #
you can ask it here
12/11/2010 3:45:11 AM #
Nice post, post more.
BuilderSJR
BuilderSJR
9/17/2011 5:04:42 AM #
This really answered my problem, thank you!
10/12/2011 5:25:40 AM #
Nice post
10/13/2011 1:34:26 AM #
just visit your blog at the first time, cool blog ))
psycholog warszawa
psycholog warszawa
10/23/2011 7:43:33 AM #
Hello There. I found your blog using msn. This is a very well written article. I’ll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I’ll definitely return.
11/18/2011 2:34:44 AM #
<p><a href="http://www.cheap-moncler-coats.com/">Cheap Moncler Coats Kids</a>In darkness the two dons crept to the window. <p><a href="www.cheap-moncler-coats.com/">Moncler Coats Women</a>The quad below was a kaleidoscope of dimly discernible faces.<p><a href="www.cheap-moncler-coats.com/">Moncler Coats Men</a>'There must be fifty of them at least,' said Mr Postlethwaite. 'If only they were all members of the College! Fifty of them at ten pounds each. Oh my!''It'll be more if they attack the Chapel,' said Mr Sniggs. 'Oh, please God, <p><a href="www.cheap-moncler-coats.com/">Fashion Moncler Jackets</a> make them attack the Chapel.<p><a href="http://www.brand-dress.com/">Moncler Sweater Men</a>'
12/8/2011 1:29:26 AM #
<p><a href="http://www.womens-ugg-boots.org/">Womens Ugg Boots</a> 'Indeed, indeed? Well, <p><a href="http://www.cheap-ugg-boot.net/">Discount Ugg Boots</a> I shall not ask for details.<p><ahref="http://www.womens-ugg-boots.org/">Discount Uggs Boots</a> <p><a href="http://www.cheap-ugg-boot.net/">Cheap Ugg Boot</a> I have been in the scholastic profession long enough to know that nobody enters it unless he has some very good reason which he is anxious to conceal. But, again to be practical, <p><a href="http://www.womens-ugg-boots.org/"> Ugg Outlet</a> Mr Pennyfeather, <p><a href="http://www.cheap-ugg-boot.net/"> Womens Winter Boots</a> I can hardly pay one hundred and twenty pounds to anyone who has been sent down for indecent behaviour. <p><a href="http://www.womens-ugg-boots.org/">Cheap Uggs Boots On Sale</a> Suppose that we fix your salary at ninety pounds a year to begin with? <p><a href="http://www.cheap-ugg-boot.net/">Sheepskin Boots On Sale</a> I have to return to Llanabba to night.<p><a href="http://www.brand-dress.com/">Brand Dress</a> There are six more weeks of
12/10/2011 5:47:45 PM #
<a href="www.genuineuggboots-au.com/"><strong>ugg boots sale</strong></a>  is well-known brands, and also the world's most famous <a href="www.genuineuggboots-au.net/"><strong>genuine ugg boots</strong></a> brand.
12/10/2011 6:21:11 PM #
add a comment site:http://xhinker.com/
1/14/2012 11:50:33 PM #
The post is written in very a good manner and it entails many useful information for me.Keep on sharing such an amazing posts.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading