Hangfire ASP.NET 核心 MVC
Hangfire ASP.NET Core MVC
遇到问题scheduling/enqueuing Hangfire (1.6.5) 的 MVC 操作
(自定义 IServices 工作得很好..)
没有注册类型 'Controllers.MyController' 的服务。
public class MyController : Controller
{
public IActionResult RenderViewToString()
{
return View();
}
public IActionResult Test()
{
//Attempt 1
Hangfire.BackgroundJob.Enqueue<MyController>(c => c.RenderViewToString());
//Attempt 2
Hangfire.BackgroundJob.Enqueue(() => c.RenderViewToString());
return new EmptyResult();
}
}
默认情况下,控制器不会在 ASP.NET Core 中的依赖注入系统中注册。您需要显式调用 AddControllersAsService
来注册它们,如 this GitHub issue:
中所述
Hi,
Maybe I'm wrong but as I tested deeply (and checked Mvc source code), Controllers are not resolved from IServiceProvider
, but only constructor arguments of them are resolved from IServiceProvider
.
Is that by design? I'm very suprised. Because, I'm using a different DI framework which supports property injection. And I can not use property injection since Controller instances are not requested from IServiceProvider
.
您是否在您的 Startup (https://github.com/aspnet/Mvc/blob/ab76f743f4ee537939b69bdb9f79bfca35398545/test/WebSites/ControllersFromServicesWebSite/Startup.cs#L37) 中添加了 AddControllersAsServices
另请参阅 this answer 相关问题以获取示例和更多详细信息。
遇到问题scheduling/enqueuing Hangfire (1.6.5) 的 MVC 操作 (自定义 IServices 工作得很好..)
没有注册类型 'Controllers.MyController' 的服务。
public class MyController : Controller
{
public IActionResult RenderViewToString()
{
return View();
}
public IActionResult Test()
{
//Attempt 1
Hangfire.BackgroundJob.Enqueue<MyController>(c => c.RenderViewToString());
//Attempt 2
Hangfire.BackgroundJob.Enqueue(() => c.RenderViewToString());
return new EmptyResult();
}
}
默认情况下,控制器不会在 ASP.NET Core 中的依赖注入系统中注册。您需要显式调用 AddControllersAsService
来注册它们,如 this GitHub issue:
Hi,
Maybe I'm wrong but as I tested deeply (and checked Mvc source code), Controllers are not resolved from
IServiceProvider
, but only constructor arguments of them are resolved fromIServiceProvider
.Is that by design? I'm very suprised. Because, I'm using a different DI framework which supports property injection. And I can not use property injection since Controller instances are not requested from
IServiceProvider
.您是否在您的 Startup (https://github.com/aspnet/Mvc/blob/ab76f743f4ee537939b69bdb9f79bfca35398545/test/WebSites/ControllersFromServicesWebSite/Startup.cs#L37) 中添加了
AddControllersAsServices
另请参阅 this answer 相关问题以获取示例和更多详细信息。