如何获取异步Task< string>的返回值;方法名称()?
我正在尝试获取方法的返回字符串,但是问题是我不知道如何从public async Task<string> Login(string username, string password, string site)
获取返回值.
I'm trying to get the return string of my method but the problem is I don't know how can I get the return value from public async Task<string> Login(string username, string password, string site)
.
这是我来自Program.cs的代码
This is my codes from Program.cs
static void Main(string[] args)
{
var username = "Leonel.Sarmiento";
var password = "welcome";
var site = "QADBSite";
var url = "na1.sabacloud.com";
ConsoleCustomizer.Spinner Spinner = new ConsoleCustomizer.Spinner("+", "x", "+", "x");
ConsoleCustomizer.TypeWriter TypeWriter = new ConsoleCustomizer.TypeWriter(15, 150);
ConsoleCustomizer.Alerts Alerts = new ConsoleCustomizer.Alerts();
Alerts.Write("Information", "HOST URL:", null);
TypeWriter.WriteLine(@"http:\\"+url);
Alerts.Write("Information", "USERNAME:", null);
TypeWriter.WriteLine(username);
Alerts.Write("Information", "PASSWORD:", null);
for (var i = 0; i < password.Length; i++)
{
TypeWriter.Write("*");
}
Console.WriteLine("");
SabaController saba = new SabaController(url);
//var certificate = saba.Login(username, password, site).Wait();
saba.Login(username, password, site).Wait();
Console.Read();
}
这是我来自Saba Controller.cs的代码
This is my codes from Saba Controller.cs
public async Task<string> Login(string username, string password, string site)
{
using(var client = new HttpClient())
{
client.BaseAddress = new Uri("https://" + HostURL + "/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("user", username);
client.DefaultRequestHeaders.Add("password", password);
client.DefaultRequestHeaders.Add("site", site);
//HTTP GET: saba/api/login
HttpResponseMessage response = await client.GetAsync("Saba/api/login");
if (response.IsSuccessStatusCode)
{
SabaModel saba = await response.Content.ReadAsAsync<SabaModel>();
SabaCertificate = saba.Certificate;
}
}
return SabaCertificate;
}
当前,您只是在调用Wait()
-这将阻塞直到任务完成,但不会给您返回值.如果改为使用Result
属性,则会阻止该属性,然后为您提供结果:
Currently you're just calling Wait()
- that will block until the task completes, but won't give you the return value. If you use the Result
property instead, that will block and then give you the result:
string certificate = saba.Login(username, password, site).Result;
现在,它将在 console 应用程序中工作,因为没有SynchronizationContext
...,这意味着async方法的继续将在线程池线程上执行.如果您使用WinForms UI线程中的相同代码(例如),那么您将陷入死锁-UI线程将等待任务完成,但是直到将任务放到UI上之前,任务无法完成线程来执行更多代码.
Now, that will work in a console app because there's no SynchronizationContext
... which means continuations in the async method will be executed on a thread pool thread. If you use the same code from a WinForms UI thread (for example) then you'd end up with a deadlock - the UI thread would be waiting for the task to complete, but the task couldn't complete until it got onto the UI thread to execute some more code.
顺便说一句,这似乎是将SabaCertificate
和SabaModel
存储在SabaController
中,但是应该这样做似乎并不明显.
As an aside, this appears to be storing SabaCertificate
and SabaModel
in the SabaController
, but it's not obvious that it should be doing that.