且构网

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

如何从 ASP.NET Core 中的 IHostedService 获取网站 URL 或访问 HttpContext?

更新时间:2023-02-16 09:04:50

HttpContext 在 http 请求点击您的网站时被填充(非常简单的解释).

HttpContext is populated when a http request hits your site (very simple explanation).

将 IHostedService 视为独立于任何 http 请求在后台运行的东西,它在与例如命中控制器的请求完全不同的上下文中运行.

Think of a IHostedService as something that runs in the background independent of any http requests, it runs in a completely different context than for example the requests that hits your controllers.

HttpContext 与 ASP.NET Core 紧密相关,而 IHostedService 不需要 ASP.NET Core 来运行.

HttpContext is heavily tied to ASP.NET Core while IHostedService does not need ASP.NET Core to run.