【问题标题】:'await' operator can only be used within an async method with c# exception?'await' 运算符只能在带有 c# 异常的异步方法中使用?
【发布时间】:2017-11-16 02:03:40
【问题描述】:

我正在使用 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; }
    }

谁能告诉我如何调用它或如何调试它???

【问题讨论】:

  • 该签名最近是否发生了变化 - 你能尝试一个干净的构建吗?
  • @DanielA.White 我已经重建了..但没有工作......所以我想发布它..
  • @DanielA.White 最近签名更改是什么意思...???

标签: c# asp.net .net


【解决方案1】:

信息很清楚。此行必须在标有async 的方法内:

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

【讨论】:

    【解决方案2】:

    你应该像这样将这个块包装在异步方法中

    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;
    
    }
    

    【讨论】:

    • 如果我想要一些返回类型,我应该如何在 main 方法中调用。我该怎么做你能解释一下吗??
    【解决方案3】:

    您只能在异步方法中使用 await。

    这是同一问题的另一个问题await operator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2016-02-08
      相关资源
      最近更新 更多