且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 .Net Core Worker Service 中托管 Web API - 无法引用 IWebHostEnvironment

更新时间:2023-02-15 21:35:45

向 API 项目添加服务要容易得多.这只是额外的一行.

It is much easier to add a service to an API project. That's just one extra line.

如果您不能或不想重新开始,请手动更改您的项目.忘记 IWebHostBuilder,它现在已经过时或不推荐使用了.

If you can't or don't want to start over, change your project manually. Forget about IWebHostBuilder, it is now obsolete or deprecated or something.

我可以在这里发布一些片段,但要容易得多:创建一个临时 API 项目,复制 Program 和 Startup 类(但保留您的命名空间),然后在 Startup 中:

I could post some fragments here but much easier: create a temporary API project, copy over the Program and Startup classes (but keep your namespaces) and then, inside Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostedService<Worker>();  // the worker will run
    services.AddControllers();
}