我正在研究一个系统,其中多个客户端对象需要通过接口实现特定的功能,我希望该函数与continuation异步运行(我希望这些实现是I/O绑定的并且想要确保所有客户端对象尽快完成此功能).我正在使用Visual Studio异步CTP刷新SP1,C#"5.0".
在抽象类的子对象中强制执行异步行为的推荐做法是什么(见下文)?我不能(显然)使用虚方法方法强制使用'异步'方法.我只能要求'任务'返回类型.这是否意味着我不应该尝试在子对象中完全要求异步行为?在这种情况下,返回类型应该简单地"无效"吗?
公共接口现在是系统设计的一个不幸后果,但这是一个单独的问题.显然,我无法限制任何人绕过'BaseFoo'并实现'IFoo'接口的异步.
这是代码:
public interface IFoo
{
void Bar(); //NOTE: Cannot use 'async' on methods without bodies.
}
public abstract class BaseFoo : IFoo
{
public async void Bar()
{
await OnBar(); //QUESTION: What is the right "async delegation" pattern?
}
protected virtual async Task OnBar()
{
await TaskEx.Yield();
}
}
public class RealFoo : BaseFoo //NOTE: May be implemented by 3rd party
{
protected override async Task OnBar()
{
//CLIENT: Do work, potentially awaiting async calls
await …Run Code Online (Sandbox Code Playgroud) 我的服务器代码中有很多异步方法,但我怀疑我有没有等待的调用者.
是否有一种简单的方法来扫描缺少await的调用的代码?
public async Task DomeSomethingAsync()
{
var result = await GetResult();
await StoreResult(result);
}
Run Code Online (Sandbox Code Playgroud)
然后某个地方我忘了使用等待;
public async Task SomeBuggyCode()
{
await Initialize();
DoSomethingAsync(); // DOH - Forgot await
}
Run Code Online (Sandbox Code Playgroud)
我希望有一种聪明的方法可以识别这些错误的调用.