Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c# - Error: "Cannot use 'async' on methods without bodies". How to force async child overrides?

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".

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'?

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.

Here is the code:

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?
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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 :(


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...