【问题标题】:How to call static async function in the below code snippet如何在下面的代码片段中调用静态异步函数
【发布时间】:2017-07-22 10:37:08
【问题描述】:
public static async Task InvokeRequestResponseService( string pstrRequest)
        {
            ServiceConnect objServiceConnect = new ServiceConnect();
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary<string, InputOutputTable>() {
                        {
                            "input1",
                            new InputOutputTable()
                            {
                                ColumnNames = new string[] {"Assignment group", "Short description"},
                                Values = new string[,] {  { "", pstrRequest },  { "", "" },  }
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>()
                    {
                    }
                };
                const string apiKey = "Some API Key"; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                client.BaseAddress = new Uri("Some Uri");

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)

                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);
}

// 我是这样调用函数的

ServiceConnect.InvokeRequestResponseService("xyz").Wait();

这里 Wait() 返回 viod 所以我不能从这里返回任何东西。但我必须添加等待才能完成任务。 谁能指导我如何获得功能响应或获得响应的其他方式。

【问题讨论】:

    标签: c# async-await


    【解决方案1】:

    获取函数的响应,将返回类型更改为Task&lt;HttpResponseMessage&gt;并返回响应。

    鉴于响应是唯一需要等待的东西,您可以删除 async 关键工作并返回响应任务

    public static Task<HttpResponseMessage> InvokeRequestResponseService( string pstrRequest)
    {
        ServiceConnect objServiceConnect = new ServiceConnect();
        using (var client = new HttpClient())
        {
            var scoreRequest = new
            {
                Inputs = new Dictionary<string, InputOutputTable>() {
                    {
                        "input1",
                        new InputOutputTable()
                        {
                            ColumnNames = new string[] {"Assignment group", "Short description"},
                            Values = new string[,] {  { "", pstrRequest },  { "", "" },  }
                        }
                    },
                },
                GlobalParameters = new Dictionary<string, string>()
                {
                }
            };
            const string apiKey = "Some API Key"; // Replace this with the API key for the web service
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
    
            client.BaseAddress = new Uri("Some Uri");
    
            // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
            // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
            // For instance, replace code such as:
            //      result = await DoSomeTask()
            // with the following:
            //      result = await DoSomeTask().ConfigureAwait(false)
    
            return client.PostAsJsonAsync("", scoreRequest);
        }
    }
    

    这样就可以调用函数了

    HttpResponseMessage response = await ServiceConnect.InvokeRequestResponseService("xyz");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-16
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多