简体   繁体   English

使用Hangfire for ASP.NET Core的触发器控制器方法

[英]Trigger Controller Method using Hangfire for ASP.NET Core

Good day guys, 美好的一天,

I'm trying to trigger a certain command inside my Controller in ASP.NET Core. 我试图在ASP.NET Core中的Controller中触发某个命令。

I've installed hangfire and configured my Startup.cs 我安装了hangfire并配置了我的Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddHangfire(x => x.UseSqlServerStorage('my conn string'));
    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    // add hangfire
    app.UseHangfireDashboard();
    app.UseHangfireServer();    
    ...
}

But the problem is, my sample controller has multiple dependency injections 但问题是,我的样本控制器有多个依赖注入

public class ProductsController: Controller
{
    private readonly ISomething1 something1;
    private readonly ISomething2 something2;
    private readonly ISomething3 something3;

    public ProductsController(ISomething1 something1, ISomething2 something2, ISomething3 something3){
        this.something1 = something1;
        this.something2 = something2;
        this.something3 = something3;
    }

    public async Task<IActionResult> TriggerMe(){
        ...
    }
}

I'm trying to implement something like this on my startup 我正试图在我的启动时实现这样的东西

RecurringJob.AddOrUpdate(
() => // trigger the TriggerMe() controller method ,
Cron.Daily);

Any help please? 有什么帮助吗? Thank you 谢谢

UPDATE UPDATE
As the Rotavita is tightly coupled with controllers,views etc. I'd recommend solving this problem in two steps 由于Rotavita与控制器,视图等紧密耦合,我建议分两步解决这个问题

  1. Generate HTML from a view (for reporting/email purposes separate out that view from normal ones) using RazorEngine , you'd only need the view/template path and the model (containing values to be rendered) 使用RazorEngine从视图生成HTML(用于报告/电子邮件目的将该视图与普通视图分开),您只需要视图/模板路径和模型(包含要呈现的值)
  2. Use the HTML to generate a PDF using wkhtmltopdf ( Rotavita uses this lib under the hood) 使用HTML使用wkhtmltopdf生成PDF( Rotavita使用此库下的lib)


Original 原版的

As invoking a controller instance is a process done by the framework and it shouldn't be invoked or called by cron jobs. 由于调用controller实例是由框架完成的过程,因此不应由cron作业调用或调用它。
For your problem, you just need to add the reference to the method or delegate. 对于您的问题,您只需要添加对方法或委托的引用。 Assuming that you're going to move your code to some other class. 假设您要将代码移动到其他类。

var instance=new Foo();
RecurringJob.AddOrUpdate(() => instance.DoSomething,Cron.Daily);

and the Foo class Foo

public class Foo
{
    public void DoSomething()
    {
    //your code
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM