从同步切换到异步 APS.NET API 控制器

问题描述 投票:0回答:1

我有这个 C# 控制器,它通过接收字符串作为输入来执行不同的 SQL 函数。

public HttpResponseMessage GetFunction(string SQLstring)
{
    HttpResponseMessage response = new HttpResponseMessage()
    {
        Content = new StringContent(SQLFunctions.SQLsyncFunctionGet(SQLstring), System.Text.Encoding.UTF8, "application/json")
    };

    return response;
}

我正在尝试用异步方法重建它:

首先我将 SQL 同步功能更改为异步,没有任何问题:

public async Task<string> SQLasyncFunctionGET(string SQLString)

如何更改

GetFunction
类以在我构建的 Web API 中激活它?

public async Task<IHttpActionResult> GetFunction(string SQLString)
{
    var content = await ???????????????
    return ok(content);
}
c# asp.net .net async-await
1个回答
0
投票

我对这个堆栈不太熟悉,所以我不记得你是否可以返回Task。但如果可以的话,这应该可行:

public async Task<HttpResponseMessage> GetFunction(string SQLstring)
{
        HttpResponseMessage response = new HttpResponseMessage()
        {
            Content = new StringContent(await SQLFunctions.SQLsyncFunctionGet(SQLstring), System.Text.Encoding.UTF8, "application/json")
        };

        return response;
}
© www.soinside.com 2019 - 2024. All rights reserved.