Suppress warning CS1998: This async method lacks &

2019-01-13 07:03发布

I've got an interface with some async functions. Some of the classes that implements the interface does not have anything to await, and some might just throw. It's a bit annoying with all the warnings.

When not using await in a async function.

Is it possible to suppress the message?

public async Task<object> test()
{
    throw new NotImplementedException();
}

warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

13条回答
2楼-- · 2019-01-13 07:32

I know this is an old thread, and perhaps this won't have the right effect for all usages, but the following is as close as I can get to being able to simply throw a NotImplementedException when I haven't yet implemented a method, without altering the method signature. If it's problematic I'd be happy to know about it, but it barely matters to me: I only use this while in development anyway, so how it performs isn't all that important. Still, I'd be happy to hear about why it's a bad idea, if it is.

public async Task<object> test()
{
    throw await new AwaitableNotImplementedException<object>();
}

Here's the type I added to make that possible.

public class AwaitableNotImplementedException<TResult> : NotImplementedException
{
    public AwaitableNotImplementedException() { }

    public AwaitableNotImplementedException(string message) : base(message) { }

    // This method makes the constructor awaitable.
    public TaskAwaiter<AwaitableNotImplementedException<TResult>> GetAwaiter()
    {
        throw this;
    }
}
查看更多
Summer. ? 凉城
3楼-- · 2019-01-13 07:32

Here is some alternatives depending on your method signature.

    public async Task Test1()
    {
        await Task.CompletedTask;
    }

    public async Task<object> Test2()
    {
        return await Task.FromResult<object>(null);
    }

    public async Task<object> Test3()
    {
        return await Task.FromException<object>(new NotImplementedException());
    }
查看更多
Deceive 欺骗
4楼-- · 2019-01-13 07:33
// This is to get rid of warning CS1998, please remove when implementing this method.
await new Task(() => { }).ConfigureAwait(false);
throw new NotImplementedException();
查看更多
劫难
5楼-- · 2019-01-13 07:35

If you don't have anything to await then return Task.FromResult

public Task<int> Success() // note: no "async"
{
  ... // Do not have await code
  var result = ...;
  return Task.FromResult(result);
}
查看更多
叛逆
6楼-- · 2019-01-13 07:36

Just as an update to Stephen's Answer, you no longer need to write the TaskConstants class as there is a new helper method:

return Task.FromException(new NotImplementedException());
查看更多
叛逆
7楼-- · 2019-01-13 07:38

Try this:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Await.Warning", "CS1998:Await.Warning")]

See: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.suppressmessageattribute?view=netframework-4.7.2

查看更多
登录 后发表回答