C# 如何在下面的代码段中调用静态异步函数
这里Wait()返回viod,因此我无法从这里返回任何内容。但我必须添加等待以完成任务。C# 如何在下面的代码段中调用静态异步函数,c#,async-await,C#,Async Await,这里Wait()返回viod,因此我无法从这里返回任何内容。但我必须添加等待以完成任务。 任何人都可以指导我如何获取函数的响应,或者获取响应的其他方法是什么。要获取函数的响应,请将返回类型更改为任务,并返回响应 鉴于响应是唯一需要等待的东西,您可以删除async键,然后返回响应任务 ServiceConnect.InvokeRequestResponseService("xyz").Wait(); ServiceConnect.InvokeRequestResponseService("xyz
任何人都可以指导我如何获取函数的响应,或者获取响应的其他方法是什么。要获取函数的响应,请将返回类型更改为
任务
,并返回响应
鉴于响应是唯一需要等待的东西,您可以删除async
键,然后返回响应任务
ServiceConnect.InvokeRequestResponseService("xyz").Wait();
ServiceConnect.InvokeRequestResponseService("xyz").Wait();
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");