您如何处理FSM中缺少WhenAsync?

时间:2018-07-02 08:55:17

标签: async-await akka.net

考虑以下演员:

class FooActor : ReceiveActor
{
    public FooActor()
    {
        Receive<Bar>(message => HandleBar(message));
    }

    void HandleBar(Bar message)
    {
        using (var temporaryDirectory = new TemporaryDirectory())
        {
            var apiClient = new ApiClient();
            var bazStream = apiClient.GetBazStream(message.BazId);

            using (var zipArchive = new ZipArchive(bazStream))
            {
                zipArchive.ExtractToDirectory(temporaryDirectory.Path);
            }

            foreach (var file in Directory.EnumerateFiles(temporaryDirectory.Path))
            {
                /* do work */
                apiClient.MarkFileAsProcessed(file);
            }
        }
    }
}

但也许ApiClient仅提供异步方法。容易,只需使用ReceiveAsync,代码几乎相同:

class AsyncFooActor : ReceiveActor
{
    public AsyncFooActor()
    {
        ReceiveAsync<Bar>(message => HandleBar(message));
    }

    async Task HandleBar(Bar message)
    {
        using (var temporaryDirectory = new TemporaryDirectory())
        {
            var apiClient = new ApiClient();
            var bazStream = await apiClient.GetBazStreamAsync(message.BazId);

            using (var zipArchive = new ZipArchive(bazStream))
            {
                zipArchive.ExtractToDirectory(temporaryDirectory.Path);
            }

            foreach (var file in Directory.EnumerateFiles(temporaryDirectory.Path))
            {
                /* do work */
                await apiClient.MarkFileAsProcessedAsync(file);
            }
        }
    }
}

现在,演员需要继续做其他事情,然后再进行其他操作,因此您想将其更改为FSM。但是没有WhenAsync方法可用于处理async / await。知道您拥有HandleBar资源并想让代码易于阅读和阅读,如何为FSM修改IDisposable

0 个答案:

没有答案