c# - WebAPI : 'IHttpActionResult' does not contain a definition for 'GetAwaiter'

标签 c# generics asynchronous asp.net-web-api lambda

早上好,这是我在社区中的第一个问题。我正在使用 C# WebAPI 框架,我正在尝试将所有 Controller 的方法更改为异步方法。我的 Controller 从 GenericController 扩展而来,它有一个从 DLL 调用方法的方法 (CallWF),它还处理所有类型的异常。

Layer1/Controllers <===> Layer2/GenericController.CallWF <===> Layer3/DLL Methods

这是我的 GenericController.CallWF 代码:

protected IHttpActionResult CallWF<T>(Func<T> action)
{
    try
    {
        return Ok(action.Invoke());
    }
    catch (Exception e1)
    {
        return( BadRequest( GetMyExceptionMessage(e1)) );
    }
}

protected IHttpActionResult CallWF(Action action)
{
    try
    {
        action.Invoke();
        return Ok();
    }
    catch (Exception e1)
    {
        return( BadRequest( GetMyExceptionMessage(e1)) );
    }
}

这里是一个 Controller 方法的例子。

[ResponseType(typeof(string))]
[Route("MyMethodA")]
public IHttpActionResult MyMethodA(int arg1)
{
    return CallWF<string>(
    () => {
        return repositoryA.itsMethodA(arg1);
    });
}

如您所见,该方法是同步的。现在我想把它变成异步的。在阅读了一些解释如何制作异步函数的网站后,这就是我认为的解决方案。

[ResponseType(typeof(string))]
[Route("MyMethodA")]
public async Task<IHttpActionResult> MyMethodA(int arg1)
{
    return await CallWF<string>(
    () => {
        return repositoryA.itsMethodA(arg1);
    });
}

但是这样做,会出现如下错误:

CS1061 'IHttpActionResult' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IHttpActionResult' could be found (are you missing a using directive or an assembly reference?)

尝试另一种方法,我尝试为异步创建一个新的 CallWF 函数。 这是制定的方法。

protected async Task<IHttpActionResult> CallWFAsync<T>(Func<T> action)
{
    try
    {
        IHttpActionResult res = Ok( action.Invoke());
        return await Ok( action.Invoke() );
    }
    catch (Exception e1)
    {
       return ( BadRequest(GetMyExceptionMessage(e1)) );
    }
}

这样做,它给了我标题错误。

CS1061 'OkNegotiatedContentResult' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'OkNegotiatedContentResult' could be found (are you missing a using directive or an assembly reference?)

有解决这个问题的想法吗?希望有人能帮助我。

最佳答案

异常消息说明您正在尝试返回不可等待的内容。不需要 await 只需返回 Task。除非方法中确实需要异步功能,否则您可以使用 Task.FromResult

简单地返回 Task

引用本文:http://blog.stephencleary.com/2012/02/async-and-await.html

Tip: If you have a very simple asynchronous method, you may be able to write it without using the await keyword (e.g., using Task.FromResult). If you can write it without await, then you should write it without await, and remove the async keyword from the method. A non-async method returning Task.FromResult is more efficient than an async method returning a value.

使用您的第一次尝试,它可以重构为...

[ResponseType(typeof(string))]
[Route("MyMethodA")]
public Task<IHttpActionResult> MyMethodA(int arg1) {
    var result = CallWF<string>(
    () => {
        return repositoryA.itsMethodA(arg1);
    });

    return Task.FromResult(result);
}

在你的第二种方法中,如果你想转换CallWF方法,你可以使用相同的方法

protected Task<IHttpActionResult> CallWFAsync<T>(Func<T> action) {
    IHttpActionResult result = null;
    try
    {
        result = Ok(action.Invoke());
    } catch (Exception e1) {
        result = BadRequest(GetMyExceptionMessage(e1));
    }
    return Task.FromResult(result);
}

现在这将允许您通过调用 CallWFAsync 方法来执行第一个示例中尝试执行的操作

[ResponseType(typeof(string))]
[Route("MyMethodA")]
public async Task<IHttpActionResult> MyMethodA(int arg1)
{
    return await CallWFAsync<string>(
    () => {
        return repositoryA.itsMethodA(arg1);
    });
}

关于c# - WebAPI : 'IHttpActionResult' does not contain a definition for 'GetAwaiter' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849321/

相关文章:

c# - 多对多父子转发器,数据关系c#

C#包结构系统

Java/泛型/数组问题

javascript - 在 TypeScript 中使用 async/await 进行方法链接

c# - 如何在 C# 中实现长轮询客户端?

C# Windows 窗体应用程序 - 在安装过程中保存配置/使用外部配置

c# - 通用扩展方法 : Type argument cannot be inferred from the usage

java - 类型参数不在其范围内

javascript - 回调函数中的 for 循环中的回调函数

c# - WinRT 为什么我会收到 UnauthorizedAccessException?