none
Using Workflow Extensions (context.GetExtension) in IIS-hosted workflows

    Question

  • I must be missing something simple, but how is one supposed to set default extension providers for IIS hosted workflows (eg. XAMLX)?

    I want to inject a service and use it in many activities like:

    IFoo aFoo = context.GetExtension<IFoo>();

    If I were hosting it myself I would have access to a WorkflowServiceHost and at startup would do something like:

    host.WorkflowExtensions.Add<IFoo>(() => new FooImp());

    but how would I set an IFoo for my XAMLX instances to use?

    Thanks!

    Wednesday, March 24, 2010 7:22 PM

Answers

  • Basically you use an IServiceBehavior to add the extension and a BehaviorExtensionElement to add the IServiceBehavior.

     

    public class StringWriterBehavior : IServiceBehavior
    {
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            var host = (WorkflowServiceHost)serviceHostBase;
            host.WorkflowExtensions.Add<TextWriter>(() => new StringWriter());
        }
    }
    

    public class StringWriterElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(StringWriterBehavior); }
        }
    
        protected override object CreateBehavior()
        {
            return new StringWriterBehavior();
        }
    }
    

    And the config file:

    <system.serviceModel>
      <extensions>
        <behaviorExtensions>
          <add name=“stringWriter“
               type="OrderService.StringWriterElement, MyWorkflowService"/>
        </behaviorExtensions>
      </extensions>
      <services>
        <service name="OrderWorkflow“
                 behaviorConfiguration="OrderWorkflowBehavior">
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="OrderWorkflowBehavior">
            <serviceMetadata httpGetEnabled="True"/>
            <stringWriter />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    
    Maurice

    Wednesday, March 24, 2010 7:42 PM

All replies

  • Basically you use an IServiceBehavior to add the extension and a BehaviorExtensionElement to add the IServiceBehavior.

     

    public class StringWriterBehavior : IServiceBehavior
    {
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            var host = (WorkflowServiceHost)serviceHostBase;
            host.WorkflowExtensions.Add<TextWriter>(() => new StringWriter());
        }
    }
    

    public class StringWriterElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(StringWriterBehavior); }
        }
    
        protected override object CreateBehavior()
        {
            return new StringWriterBehavior();
        }
    }
    

    And the config file:

    <system.serviceModel>
      <extensions>
        <behaviorExtensions>
          <add name=“stringWriter“
               type="OrderService.StringWriterElement, MyWorkflowService"/>
        </behaviorExtensions>
      </extensions>
      <services>
        <service name="OrderWorkflow“
                 behaviorConfiguration="OrderWorkflowBehavior">
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="OrderWorkflowBehavior">
            <serviceMetadata httpGetEnabled="True"/>
            <stringWriter />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    
    Maurice

    Wednesday, March 24, 2010 7:42 PM
  • But there seems to be no way to access/load the extension from within a XAMLX definition.  Am I wrong?    If I'm correct, then that would make loading an extension via a service behavior pretty pointless.
    Thursday, May 15, 2014 9:55 PM
  • Another way to add an extension into the workflow instance is via a custom activity. The following code snippet shows this technique:

    public class AddFooExtension : NativeActivity
    {
      protected override void CacheMetadata(NativeActivityMetadata metadata)
      {
        metadata.AddDefaultExtensionProvider<IFoo>(() => new FooImpl());
      }
    
      protected override void Execute(NativeActivityContext context)
      {
        Trace.WriteLine("The 'Foo' extension has been added");
      }
    }
    Note, that only one extension can be added into the workflow.

    Thanks

    Roman



    • Edited by Roman Kiss Friday, May 16, 2014 4:33 PM
    Friday, May 16, 2014 4:31 PM