首页 > 解决方案 > api启动后尝试运行方法

问题描述

所以我在 ASP.Net-Core 上编写了一个 api,我想在服务器启动后立即运行一些 csvReader 方法。目前它只是启动并且不运行我的 DataMigrationController。

class Program
{
    public const string _path =
        "C://path";

    static void Main(string[] args)
    {
        
        CreateWebHostBuilder(args)
            .UseUrls("https://localhost:44301/")
            .Build()
            .Run();

        DataMigrationController DMC = new DataMigrationController();
        DMC.readCSV(_path);
        Console.WriteLine("Done");
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

标签: c#asp.netasp.net-core

解决方案


你可以用hangfire做到这一点。

您可以设置启动应用程序后仅执行一次的作业。请参阅hangfire 文档中的即发即弃作业。

请注意不要将太多作业克隆添加到队列中。您可以使用 WorkerCount 选项轻松避免这种情况。有关更多信息,请查看: https ://docs.hangfire.io/en/latest/background-processing/configuring-degree-of-parallelism.html


推荐阅读