简体   繁体   English

'await'运算符只能在具有c#异常的异步方法中使用吗?

[英]'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 我正在使用hellosign C#API,并且在使用以下代码调用帐户信息功能时

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 下面是GetAsync方法

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 : 该行必须在标有async的方法内:

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

you should wrap this block in Async Method like this 您应该像这样将块包装在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;

}

You can only use await in an async method. 您只能在异步方法中使用await。

Here's another question to the same problem await operator 下面是同样的问题,另外一个问题的await操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# await 运算符只能在异步方法中使用,但方法是异步的 - C# The await operator can only be used within an async method but method is async await运算符只能在async方法中使用-但方法是异步的 - await operator can only be used within an async method - but method is asynchron “ await”运算符只能在异步方法中使用[MySQL连接器] - The 'await' operator can only be used within an async method [MySQL connector] &#39;await&#39;运算符只能在async&gt;方法中使用 - The 'await' operator can only be used within an async > method await 运算符只能在异步方法中使用 - The await operator can only be used within an async method &#39;await&#39;运算符只能在异步lambda表达式错误中使用 - The 'await' operator can only be used within an async lambda expression error 'await' 运算符只能在异步 lambda 表达式中使用 - The 'await' operator can only be used within an async lambda expression Visual Studio App Center:“ await”运算符只能在异步方法中使用 - Visual Studio App Center: The 'await' operator can only be used within an async method 等待操作符只能在标有&#39;async&#39;修饰符的方法或lambda中使用 - The await operator can only be used in a method or lambda marked with the 'async' modifier “await”运算符只能在异步方法中使用。 考虑使用“async”修饰符标记此方法并更改其返回类型 - The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM