c# - 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>();
}
解决方案
你可以用hangfire做到这一点。
您可以设置启动应用程序后仅执行一次的作业。请参阅hangfire 文档中的即发即弃作业。
请注意不要将太多作业克隆添加到队列中。您可以使用 WorkerCount 选项轻松避免这种情况。有关更多信息,请查看: https ://docs.hangfire.io/en/latest/background-processing/configuring-degree-of-parallelism.html
推荐阅读
- opengl - 传统 OpenGL 中的顺序独立透明度
- batch-file - 将批处理的内容附加到文件到现有文件名
- android - Azure 门户目录未显示在 appcenter 中
- java - spring boot JAR作为服务运行时无法加载系统环境变量
- python - Azure python sdk 从虚拟机获取属性
- google-cloud-dataflow - 组合 KV 对的 PCollection 时,如何访问 CombinerFn 子类中的密钥?
- vb.net - 如何在不分散现有数据的情况下将 csv 数据合并到 datagridview 中
- sql - Oracle SQL - 没有计数结果时返回日期记录
- python - select specific face from multiple detected faces inside image from Haar-cascade in python
- javascript - 检查嵌套对象中是否存在键