locked
Extensions RRS feed

  • Question

  • I adding Extension to my WorkflowApplication object:

    wfApp.Extensions.Add(new TransParamsExtension(TransParams));

    And try to use it in my custom activity:

    TransParamsExtension extension = context.GetExtension<TransParamsExtension>(); 
    
    But after this string "extension" still be null. Why?

    My extension class:

     public class TransParamsExtension : IWorkflowInstanceExtension
    
     {
    
      private WorkflowInstanceProxy _Instance;
      public object TransParams { get; set; }
      public TransParamsExtension(object TransParams)
      {
       this.TransParams = TransParams;
      }  
      public IEnumerable<object> GetAdditionalExtensions()
      {
       yield break;
      }
      public void SetInstance(WorkflowInstanceProxy instance)
      {
       _Instance = instance;
      }
     }
     

     

    Tuesday, August 10, 2010 6:57 AM

Answers

  • Hi,

    The extension works fine in my computer. Here is my test code:
    You extension code(I add a DoSomething method):
        public class TransParamsExtension : IWorkflowInstanceExtension {
            private WorkflowInstanceProxy _Instance;
            public object TransParams { get; set; }
            public TransParamsExtension(object TransParams) {
                this.TransParams = TransParams;
            }
            public IEnumerable<object> GetAdditionalExtensions() {
                yield break;
            }
            public void SetInstance(WorkflowInstanceProxy instance) {
                _Instance = instance;
            }
            public void DoSomething() {
                Console.WriteLine(TransParams==null);
            }
        }
    Custom activity code:
        public class MyActivityWithExtension : NativeActivity {
            protected override void Execute(NativeActivityContext context) {
                Console.WriteLine("Start");
                var extension = context.GetExtension<TransParamsExtension>();
                extension.DoSomething();
                Console.WriteLine("end");
            }
        }
    Workflow host code:
         public static void RunWorkflowWithCustomizedExtension(){
                AutoResetEvent waitHandler = new AutoResetEvent(false);
                WorkflowApplication wfApp = new WorkflowApplication(new Sequence() {
                    Activities = {
                        new MyActivityWithExtension()
                    }
                });
                wfApp.Extensions.Add(new TransParamsExtension(new object()));
                wfApp.Unloaded = (arg) => {
                    waitHandler.Set();
                };
                wfApp.Run();
                waitHandler.WaitOne();
         }

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com
    • Marked as answer by Andrew_Zhu Tuesday, August 17, 2010 7:33 AM
    Thursday, August 12, 2010 8:18 AM

All replies

  • Is your SetInstance() method getting called?
    I had a quick look, I think it is probably not, because you are using the singleton version of Add, Add(object), rather than the per-instance version of Add, Add(Func<T>);

    Tim

    Tuesday, August 10, 2010 5:49 PM
  • Thank you for your response.

          WorkflowApplication wfApp = new WorkflowApplication(ActivityXamlServices.Load(name));
          Func<object, TransParamsExtension> transparamsextfunc = (object TransP) => { return new TransParamsExtension(TransP); };
          wfApp.Extensions.Add(transparamsextfunc(TransParams));

    I changed my code:

    But nothing changes.

    Something interest: I see my extension in extension list of my WorkflowApplication object. But why can't I get it from NativeActivityContext of my activity?

    Wednesday, August 11, 2010 4:03 AM
  • This variant doesn't work too:

       WorkflowApplication wfApp = new WorkflowApplication(ActivityXamlServices.Load(name));
       Func<TransParamsExtension> transparamsextfunc = () => { return new TransParamsExtension(TransParams); };
       wfApp.Extensions.Add<TransParamsExtension>(transparamsextfunc);
    

    My extension is no more singleton, really. But there is still null, when I try to get my extension from context in activity.

     

    Wednesday, August 11, 2010 4:30 AM
  • Hi,

    The extension works fine in my computer. Here is my test code:
    You extension code(I add a DoSomething method):
        public class TransParamsExtension : IWorkflowInstanceExtension {
            private WorkflowInstanceProxy _Instance;
            public object TransParams { get; set; }
            public TransParamsExtension(object TransParams) {
                this.TransParams = TransParams;
            }
            public IEnumerable<object> GetAdditionalExtensions() {
                yield break;
            }
            public void SetInstance(WorkflowInstanceProxy instance) {
                _Instance = instance;
            }
            public void DoSomething() {
                Console.WriteLine(TransParams==null);
            }
        }
    Custom activity code:
        public class MyActivityWithExtension : NativeActivity {
            protected override void Execute(NativeActivityContext context) {
                Console.WriteLine("Start");
                var extension = context.GetExtension<TransParamsExtension>();
                extension.DoSomething();
                Console.WriteLine("end");
            }
        }
    Workflow host code:
         public static void RunWorkflowWithCustomizedExtension(){
                AutoResetEvent waitHandler = new AutoResetEvent(false);
                WorkflowApplication wfApp = new WorkflowApplication(new Sequence() {
                    Activities = {
                        new MyActivityWithExtension()
                    }
                });
                wfApp.Extensions.Add(new TransParamsExtension(new object()));
                wfApp.Unloaded = (arg) => {
                    waitHandler.Set();
                };
                wfApp.Run();
                waitHandler.WaitOne();
         }

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com
    • Marked as answer by Andrew_Zhu Tuesday, August 17, 2010 7:33 AM
    Thursday, August 12, 2010 8:18 AM
  • Actually, my problem was different. I tryed to use extensions in my app, but my app is a plugin for the big software complex. And problem is in this complex, because as in independent application my code works fine.
    Thursday, August 19, 2010 4:35 AM