我想了解c#中的异步方法 我已经创建了一个延迟10秒的wcf服务。我正在从控制台应用程序调用wcf服务,值为asyncValue从不设置。它只是关闭控制台应用程序。异步任务等待
为什么asyncValue从未设置为Ok?
WCF服务
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AsyncService
{
[OperationContract]
public async Task<string> TimeAsync()
{
await Task.Delay(10000);
return "Ok";
}
}
控制台应用程序
static void Main(string[] args)
{
Task<string> asyncValue = GetAsyncTimeFromWcf();
}
private static async Task<string> GetAsyncTimeFromWcf()
{
AsyncService.AsyncServiceClient client = new AsyncService.AsyncServiceClient();
Task<string> asyncTime = client.TimeAsync();
return await asyncTime;
}
等待'asyncValue' –
@ L.B你不能'等待asyncValue'的'main'方法中,因为没有被标记为'async',你不能标记主要是异步。 – Alberto
'asyncValue.Result' –