AsyncTask的codeActivity并等待后丢失的上下文 [英] AsyncTaskCodeActivity and lost context after await

查看:257
本文介绍了AsyncTask的codeActivity并等待后丢失的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的AsyncTask codeActivity 的当执行第一AWAIT之后被访问的上下文参数失败。例如:

AsyncTaskCodeActivity fails when the context parameter is accessed after the first await is performed. For example:

public class TestAsyncTaskCodeActivity : AsyncTaskCodeActivity<int>
{
    protected async override Task<int> ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
    {
        await Task.Delay(50);
        // context has already been disposed and the next line throws
        // ObjectDisposedException with the message:
        // An ActivityContext can only be accessed within the scope of the function it was passed into.
        context.Track(new CustomTrackingRecord("test"));
        // more awaits can happen here
        return 3;
    }
}

有没有简单的方法preserve的背景下,因此它可以也正在等待事情后使用?

Is there any simple way to preserve the context so it can be used also after awaiting something?

推荐答案

啊。

当我写了的AsyncTask codeActivity&LT; T&GT; ,我假定异步codeActivityContext 实际上将是在开始和异步方法的端部相同的实例,且可一路通过。这是不是这样的(这是一个有点奇怪 - 不知道为什么WF团队所做的决定)。

When I wrote AsyncTaskCodeActivity<T>, I assumed that the AsyncCodeActivityContext was in fact going to be the same instance at the beginning and end of the asynchronous method, and be available all the way through. This is not the case (which is a bit odd - not sure why the WF team made that decision).

相反,异步codeActivityContext 可以的只有的可以在活动的开始和结束访问。尴尬的,确实如此。

Instead, the AsyncCodeActivityContext can only be accessed at the beginning and end of the activity. Awkward, indeed.

更新code以下将让您在开始访问上下文(例如,读取变量),然后在年底再次访问上下文。我还引入了一个可选的 TSTATE ,可用于存储活动状态(该活动的可以的整个执行权限)。让我知道这是否符合您的需要;我没有测试它。

The updated code below will allow you to access the context at the beginning (e.g., reading In variables) and then access the context again at the end. I also introduce an optional TState, which can be used for storing activity state (which the activity can access throughout its execution). Let me know if this fits your needs; I haven't tested it.

public abstract class AsyncTaskCodeActivity<T, TState> : AsyncCodeActivity<T>
{
  protected sealed override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
  {
    TState activityState = PreExecute(context);
    context.UserState = activityState;
    var task = ExecuteAsync(activityState);
    return AsyncFactory<T>.ToBegin(task, callback, state);
  }

  protected sealed override T EndExecute(AsyncCodeActivityContext context, IAsyncResult asyncResult)
  {
    var result = AsyncFactory<T>.ToEnd(asyncResult);
    return PostExecute(context, (TState)context.UserState, result);
  }

  protected virtual TState PreExecute(AsyncCodeActivityContext context)
  {
    return default(TState);
  }
  protected abstract Task<T> ExecuteAsync(TState activityState);
  protected virtual T PostExecute(AsyncCodeActivityContext context, TState activityState, T result)
  {
    return result;
  }
}
public abstract class AsyncTaskCodeActivity<T> : AsyncTaskCodeActivity<T, object>
{
}

这篇关于AsyncTask的codeActivity并等待后丢失的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆