Skip to content

ObjectDisposedException on Custom Workflow Activities

August 29, 2011

I was noticing some strange behaviour of output parameters of custom workflow activities when multiple instances of a workflow containing a particular custom activity were running at once.

The event was random, but sometimes one of the workflow would fail to set the output parameter. No exception, no error, the workflow would simply not do what the code said. While dismantling my code piece by piece and testing for differente race conditions I finally got an exception that shone some light:

System.ObjectDisposedException: An ActivityContext can only be accessed within the scope of the function it was passed into

A little cryptic, but I realised that the problem was caused by the way I was storing the CodeActivityContext for execution throughout my classes:

        [Output("Count")]
        public OutArgument Count { get; set; }
        //
        public CodeActivityContext ExecutionContext { get; set; }
        //
        protected override void Execute(CodeActivityContext executionContext)
        {
            this.ExecutionContext = executionContext;
            SomeOtherMethod();
            (...)
        }

        //
        private void SomeOtherMethod()
        {
            this.Count.Set(this.ExecutionContext, 1);
            (...)

As you can see above, I thought I could use CodeActivityContext like any other class, and I was storing it as a class property so I did not have to pass it from method to method. I still don’t know why, but I am reasonably sure that this does not work, and that the correct method is:

        [Output("Count")]
        public OutArgument Count { get; set; }
        //
        protected override void Execute(CodeActivityContext executionContext)
        {
            SomeOtherMethod(executionContext);
            (...)
        }

        //
        private void SomeOtherMethod(CodeActivityContext executionContext)
        {
            this.Count.Set(executionContext, 1);
            (...)

Which is consistent with the exception message: “An ActivityContext can only be accessed within the scope of the function it was passed into“. Indeed.

I suspect that this has to do with serialisation: everything works until the class is serialised and deserialised, then CodeActivityContext cannot be revived by deserialisation, and it becomes somewhat corrupted. Pity that the behaviour is not consistent, the exception is thrown sporadically and most of the times there are no errors.

If you can offer a logical explanation I’d love to hear about it. I’m not an expert in Microsoft WF and this might just be a beginner’s mistake.

Alberto “Took me a while to figure it out” Gemin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: