从同步切换到异步aps.net API控制器

时间:2017-08-30 16:11:14

标签: c# api async-await

我有这个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;
    }

我试图在Async方法中重建它:

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

public async Task<string> SQLasyncFunctionGET(string SQLString)

如何更改GetFunction类以在我构建的Web API中激活它?

public async Task<IHttpActionResult> GetFunction(string SQLString)
{
var content = await ???????????????
return ok(content);
}

欢迎提供帮助。

1 个答案:

答案 0 :(得分:0)

我对这个堆栈并不熟悉,所以我不记得你是否可以返回Task&lt; HttpResponseMessage&gt;或不。但如果可以,这应该有效:

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;
}