考虑以下演员:
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
?