Having trouble scheduling/enqueuing an MVC action with Hangfire (1.6.5) (Custom IServices works just fine..)
No service for type 'Controllers.MyController' has been registered.
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();
}
}
By default, controllers aren't being registered with the dependency injection system in ASP.NET Core. You need to explicitly call
AddControllersAsService
to register them, as explained in this GitHub issue:See also this answer to a related question for an example and more details.