简体   繁体   English

OWIN 身份验证:异步方法缺少“等待”运算符

[英]OWIN Authentication:async method lacks 'await' operators

I have implemented OWIN authentication in my project.But when I try to build the project, it is showing some errors.我已经在我的项目中实现了 OWIN 身份验证。但是当我尝试构建项目时,它显示了一些错误。

The methods which are showing errors :显示错误的方法:

 public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
    //code    
   }
  public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
  //code 
    }

The showing error is显示错误是

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.考虑使用 'await' 运算符来等待非阻塞 API 调用,或使用 'await Task.Run(...)' 在后台线程上执行 CPU 密集型工作。

How can I fix this issue?我该如何解决这个问题?

If you are not using an await operator in the implementation of the function then you can remove the async modifier.如果您没有在函数的实现中使用await运算符,那么您可以删除async修饰符。

However, once you remove it, the compiler will expect you to provide a return value for Task .但是,一旦您删除它,编译器将期望您为Task提供返回值。 You can obviously create a Task however you choose, but in this instance you might want to just return the following (which is discussed here ):您显然可以根据自己的选择创建一个Task ,但在这种情况下,您可能只想返回以下内容( 此处讨论):

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    return Task.FromResult<object>(null);
}

async is not part of the signature. async不是签名的一部分。 Just because you're overriding an async method doesn't mean that you have to mark your override as async .仅仅因为您覆盖async方法并不意味着您必须将覆盖标记为async

So, if you don't have any await s in your code, just remove the async mark.因此,如果您的代码中没有任何await ,只需删除async标记。

public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
//code    
}
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//code 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此异步方法缺少“等待”运算符,将同步运行 - This async method lacks 'await' operators and will run synchronously 异步方法缺少等待 - The async method lacks await 异步方法中的警告消息说它缺少等待运算符 - Warning message in async method saying that it lacks await operators 当等待在传递给异步lambda表达的函数中时,异步方法缺少等待操作员警告 - Async method lacks await operators warning when the await is in a function passed to an async lambda expresion c# 异步等待奇怪的警告 CS1998:此异步方法缺少“等待”运算符 - c# async await strange warning CS1998: This async method lacks 'await' operators 异步无效方法缺少等待 - async void method lacks await 当我从它调用另一个异步方法时,异步方法缺少等待运算符消息? - async method lacks await operators message when I call another async method from it? 如何删除StyleCop警告“此异步方法缺少&#39;await&#39;运算符,将同步运行”,而不会从签名中删除异步 - How to remove StyleCop warning “This async method lacks 'await' operators and will run synchronously” without removing async from signature Task.WaitAll 的使用没有任何“等待”运算符导致警告 CS1998 此异步方法缺少“等待”运算符,将同步运行 - Usage of Task.WaitAll without any 'await' operators causing warning CS1998 This async method lacks 'await' operators and will run synchronously 我是否应该担心“此异步方法缺少 &#39;await&#39; 运算符并将同步运行”警告 - Should I worry about "This async method lacks 'await' operators and will run synchronously" warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM