且构网

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

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

更新时间:2023-02-16 09:13:24

http请求到达您的网站时,就会填充HttpContext (非常简单的解释)。

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.