错误:“不能在没有主体的方法上使用‘异步’".如何强制异步子覆盖? [英] Error: "Cannot use 'async' on methods without bodies". How to force async child overrides?

查看:26
本文介绍了错误:“不能在没有主体的方法上使用‘异步’".如何强制异步子覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个系统,在该系统中,多个客户端对象需要通过接口实现特定功能,并且我希望该功能与延续异步运行(我希望实现是 I/O 绑定的并希望确保所有客户端对象尽快完成此功能).我正在使用 Visual Studio Async CTP Refresh for SP1,使用 C#5.0".

I'm working on a system in which multiple client objects are expected to implement a particular function via an interface, and I want that function to run asynchronously with continuations (I'm expecting the implementations to be I/O-bound and want to ensure that all the client objects complete this function as soon as possible). I'm using the Visual Studio Async CTP Refresh for SP1, with C# "5.0".

在我的抽象类的子对象中强制执行异步行为的推荐做法是什么(见下文)?我不能(显然)使用虚拟方法方法强制使用异步"方法.我只能要求任务"返回类型.这是否意味着我不应该尝试在子对象中要求异步行为?在这种情况下,返回类型是否应该只是void"?

What is the recommended practice for enforcing asynchronous behavior in child objects of my abstract class (see below)? I can't (apparently) enforce use of 'async' methods using the virtual method approach. I can only require a 'Task' return type. Does this mean I should not try to require asynchronous behavior at all in child objects? In that case, should the return type be simply 'void'?

公共接口是目前系统设计的一个不幸结果,但这是一个单独的问题.显然,我无法限制任何绕过BaseFoo"而仅实现IFoo"接口的异步操作.

The public interface is an unfortunate consequence of the system design right now, but that's a separate issue. Obviously, I couldn't constrain anyone to be asynchronous who bypasses 'BaseFoo' and just implements the 'IFoo' interface.

代码如下:

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 TaskEx.Yield(); //SECONDARY QUESTION: Is there a way to avoid this if there are no 'awaits' in the client's work?
    }
}

推荐答案

一个方法是否使用async/await 实现是一个实现em> 细节.方法应该如何表现是一个契约细节,应该以正常方式指定.

Whether a method is implemented using async/await or not is an implementation detail. How the method should behave is a contract detail, which should be specified in the normal way.

请注意,如果您让方法返回 TaskTask,则更明显的是它是异步的,并且可能很难实现没有异步.

Note that if you make the method return a Task or a Task<T>, it's more obvious that it's meant to be asynchronous, and will probably be hard to implement without being asynchronous.

另一方面,如果有一个实现(例如用于测试目的),其中 await 表达式永远 是不完整的,你为什么要强迫某人编写一个没有 await 调用的异步方法?您期望实现是 IO 绑定的,但可能会有一些特殊情况,实现需要使用硬编码数据等.

On the other hand, if there's an implementation (e.g. for test purposes) where the await expressions would never be incomplete, why would you want to force someone to write an async method with no await calls in anyway? You're expecting implementations to be IO-bound, but maybe there will be special cases where implementations want to use hard-coded data etc.

基本上,您必须在该方法的文档中处理此问题 - 如果您不相信实现者会阅读该内容,那么无论如何您都没有机会:(

Basically you've got to handle this in the documentation for the method - if you can't trust implementers to read that, you've got no chance anyway :(

这篇关于错误:“不能在没有主体的方法上使用‘异步’".如何强制异步子覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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