【发布时间】: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