简体   繁体   中英

'await' operator can only be used within an async method with c# exception?

I am using hellosign c# api and when I am calling function for account info using below code

var helloSign = new HelloSignClient("username", "password");
    Account account = await helloSign.Account.GetAsync();
    Console.WriteLine("Your current callback: " + account.CallbackUrl);

I am getting below error.

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'.

below is GetAsync method

public async Task<Account> GetAsync()
        {
            AccountWrapper accountWrapper = await helloSignService.MakeRequestAsync<AccountWrapper>(settings.HelloSignSettings.Endpoints.Account.Get);
            return accountWrapper.Account;
        }

this is type of account class

public class Account : AccountCondensedWithRole
    {
        [JsonProperty("callback_url")]
        public string CallbackUrl { get; internal set; }
    }

Can some one tell me how to call this or how to debug this ???

The message is pretty clear. This line must be inside a method marked with async :

Account account = await helloSign.Account.GetAsync();

you should wrap this block in Async Method like this

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;

}

You can only use await in an async method.

Here's another question to the same problem await operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM