& # 39;等待# 39;Operator只能在async方法中使用,c#例外
本文关键字:例外 方法 Operator 等待 async | 更新日期: 2023-09-27 17:51:23
我正在使用hellosign c# api,当我使用下面的代码调用帐户信息函数
var helloSign = new HelloSignClient("username", "password");
Account account = await helloSign.Account.GetAsync();
Console.WriteLine("Your current callback: " + account.CallbackUrl);
我得到以下错误。
Error 2 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
下面是GetAsync方法
public async Task<Account> GetAsync()
{
AccountWrapper accountWrapper = await helloSignService.MakeRequestAsync<AccountWrapper>(settings.HelloSignSettings.Endpoints.Account.Get);
return accountWrapper.Account;
}
这是帐户类的类型
public class Account : AccountCondensedWithRole
{
[JsonProperty("callback_url")]
public string CallbackUrl { get; internal set; }
}
谁能告诉我如何调用这个或者如何调试这个??
你应该像这样用Async方法包装这个块
public async Task<Type> MethodAsync(){
// other code if needed ....
var helloSign = new HelloSignClient("username", "password");
Account account = await helloSign.Account.GetAsync();
Console.WriteLine("Your current callback: " + account.CallbackUrl);
return type;
}
传达的信息非常清楚。这一行必须位于标记为async
:
Account account = await helloSign.Account.GetAsync();
只能在异步方法中使用await。
下面是同样问题的另一个问题等待操作符