开发者

Activity - Workflow Instance Extension one-to-one relation

I am trying to implement a WF4 activity extension which to handle some long running data processing and to send information back to the "mother" activity from time to time.

I implemented a version of this but I have issues with it if I am using this activity in a parallel activity context. As it can be seen from the code below, the开发者_StackOverflow中文版 activity is creating a bookmark and after it makes a call to the MyActivityExtension object. I want that every instance of the MyActivity class to have a different instance of MyActivityExtension to communicate with. If two activities of this type are used sequentaly in one workflow definition everything works OK, and the calls to the extension class are made to different objects, but if one activity starts execution while another one is idled the second activity use the same extension instance like the idled one.

Below I have added a simplified sample of my code. Any idea how I can impose a one-to-one relation between activity instances and activity extension instances?

Thank you in advance, Alex

MyActivity code:

    protected override void Execute(NativeActivityContext context)
    {
        //Some data processing....

        //Obtain the activity extension
        MyActivityExtension extension = context.GetExtension<MyActivityExtension >();


        string bookmarkName = "MyActivity_" + Guid.NewGuid().ToString();
        var bookmark = context.CreateBookmark(bookmarkName, BookmarkResumed);

        extension.ProcessData(bookmarkName);
    }

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

 private void BookmarkResumed(NativeActivityContext context, Bookmark bookmark, object value)
    {
       //some data retrieving operations
    }

MyActivityExtension code:

public class MyActivityExtension : IWorkflowInstanceExtension
{
    private WorkflowInstanceProxy instance;
    private Guid id;

    public MyActivityExtension()
    {
         id = Guid.NewGuid();
    }

    internal void ProcessData(string bookmarkName)
    {
        Console.WriteLine("My activity extension Id: " + id.ToString());

        //Some data processing
    }

    public IEnumerable<object> GetAdditionalExtensions()
    {
        return null;
    }

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


The IWorkflowInstanceExtension helps with a one extension to one workflow relation but each activity, as you discovered will share the same extension. There is no out of the box way to create a new extension for each activity. What I would do is change the extension into some sort of extension factory with a Create() function that returns a new object.

So your code would look something like this:

   protected override void Execute(NativeActivityContext context)
    {
        //Some data processing....

        //Obtain the activity extension
        MyActivityExtensionFactory extensionFactory = context.GetExtension<MyActivityExtensionFactory >();
        MyActivityExtension extension = extensionFactory.Create();

        string bookmarkName = "MyActivity_" + Guid.NewGuid().ToString();
        var bookmark = context.CreateBookmark(bookmarkName, BookmarkResumed);

        extension.ProcessData(bookmarkName);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜