【问题标题】:Switching from sync to async aps.net API controller从同步切换到异步 aps.net API 控制器
【发布时间】:2019-07-17 10:27:10
【问题描述】:

我有这个 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# api async-await


    【解决方案1】:

    我对这个堆栈不是很熟悉,所以我不记得你是否可以返回 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 2016-08-02
      • 2017-11-20
      • 2012-10-14
      • 2017-05-19
      • 2022-11-10
      • 2013-12-04
      • 1970-01-01
      相关资源
      最近更新 更多