【问题标题】:Unable to call async and parallel method inside my asp.net console application无法在我的 asp.net 控制台应用程序中调用异步和并行方法
【发布时间】:2018-11-29 15:34:16
【问题描述】:

我有一个 asp.net 控制台应用程序,在这个控制台应用程序中,我想使用 WhenAll 以并行方式调用异步方法,这是我的控制台应用程序主方法:

static void Main(string[] args)
{
    Marketing ipfd = new Marketing();
    try
    {
        using (WebClient wc = new WebClient()) // call the PM API to get the account id
        {
            //code goes here...
        }
    }
    catch (Exception e)
    {
    }
    var tasks = ipfd.companies.Select(c => gettingCustomerInfo(c.properties.website.value)).ToList();
    var results = await Task.WhenAll(tasks);}
}

这是我正在调用的方法:-

class Program
{
    static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
    SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);
    int numberofrequests = int.Parse(ConfigurationManager.AppSettings["numberofrequests"].ToString());
    int waitduration = int.Parse(ConfigurationManager.AppSettings["waitdurationmilsc"].ToString());

    private async Task<ScanInfo> gettingCustomerInfo(string website)
    {
        await throttler.WaitAsync();
        ScanInfo si = new ScanInfo();
        var tasks = ipfd.companies.Select(c =>   gettingCustomerInfo(c.properties.website.value)).ToList();
        var results = await Task.WhenAll(tasks);

但我得到了这些例外:-

“await”运算符只能在异步方法中使用。考虑 用 'async' 修饰符标记这个方法并改变它的返回值 键入“任务”

非静态字段、方法或 属性 '***.Program.gettingCustomerInfo(string)'

那么任何人都可以就此提出建议吗?现在我知道第一个异常是关于 Main 方法本身不是异步的,但是如果我将 main 方法定义为异步,那么我会得到另一个异常,即程序不包含可以作为端点调用的 Main 方法?

【问题讨论】:

  • 你试过 Task mainTask = Task.WhenAll(tasks).Wait() 吗?
  • @LeBigCat 现在如果我定义以下var results = Task.WhenAll(tasks).Wait(); 我会得到这个错误Cannot assign void to an implicitly-typed local variable *****\Program.cs + 我仍然会得到我上面的第二个错误..
  • 是的,如果您的任务不返回 void,您还必须将类型添加到 mainTask(这是下面的第一个答案;))
  • @LeBigCat 不确定你到底是什么意思?
  • Task 或 var mainTask = Task.WhenAll(tasks).Wait() 也可以,你也可以使用 Task.WaitAll(tasks.ToArray())

标签: c# asp.net async-await task


【解决方案1】:

有两种方法可以解决这个问题

首选选项

通过以下步骤使用从 C# 7.1 开始提供的新的 async Main 支持:

  • 编辑您的项目文件以使用 C# 7.1(属性 -> 构建 -> 高级 -> 选择 C#7.1 作为您的语言版本)

  • 将 Main 方法更改为以下内容:

static async Task Main(string[] args) { ... }

这是一个演示工作版本的示例项目:

https://github.com/steveland83/AsyncMainConsoleExample

如果感兴趣,下面是我编写的一组非正式练习,用于演示处理异步任务的几种方法(以及一些常见的基本错误):https://github.com/steveland83/async-lab

选项 2

如果由于某种原因您无法使用上述方法,您可以强制异步代码同步运行(请注意,这几乎总是被认为是不好的做法)。

var aggregateTask = Task.WhenAll(tasks);
aggregateTask.Wait();

【讨论】:

  • @Steve Land 现在我安装了 VS 2017,我将代码更改为使用 c# 7.1。然后我将方法更改为 static async Task Main(string[] args) { ... }。但现在我收到 2 个错误 1) Program does not contain a static 'Main' method suitable for an entry point 2) An object reference is required for the non-static field, method, or property 'Program.gettingCustomerInfo(string)' .. 所以不确定是什么导致了这些错误?
  • @testtest 我添加了一些完整工作示例的链接。请将这些与您所拥有的进行比较 - 如果您仍然有错误,请打开一个新问题来具体解决这些问题。祝你好运:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多