$count
//我正在调用这样的函数
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);
}
这里Wait()返回viod,所以我无法从这里返回任何内容。但是我必须添加Wait以完成任务。 任何人都可以指导我如何获得功能的响应或者获得响应的另一种方式。
答案 0 :(得分:0)
将函数更改返回类型的响应发送到Task<HttpResponseMessage>
并返回响应。
鉴于响应是唯一需要等待的事情,您可以删除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");