hangfire后台只能调用public个方法怎么解决?
How to resolve Only public methods can be invoked in the background in hangfire?
我在 class
中有方法 public
。当我将它添加到 hangfire 时出现编译器错误
Only public methods can be invoked in the background. Ensure your
method has the public
access modifier, and you aren't using explicit
interface implementation.'
public class JobsService
{
public void TestJob2(Parameters parameters)
{
}
}
public class JobsController : ControllerBase
{
private readonly JobsService _jobsService;
public JobsController(JobsService jobsService)
{
_jobsService = jobsService;
}
public IActionResult AddOrUpdate([FromQuery]string recurringJobId = "", [FromQuery]string methodName = "", [FromQuery] bool remove = false, [FromQuery] string cronExpression = "*/1 * * * *", [FromBody] Parameters parameters = null)
{
Expression<Action> expression = ToExpression(() => { string t = (string)method.Invoke(_jobsService, new object[] { parameters }); });
RecurringJob.AddOrUpdate(recurringJobId, expression, cronExpression);
}
}
lambda 及其参数需要由 Hangfire 序列化。 AFAIK,它的主体必须是一个简单的调用,例如没有方法分配。
执行作业的 JobsService
实例不能是您的控制器引用的实例。我假设你已经为依赖注入设置了 Hangfire see "Using IoC containers", or JobsService
can be instantiated without needing any dependency see "Passing dependencies".
您需要为您的 JobsService
添加一个方法 class :
public void InvokeMethod(string methodName, Parameters parameters)
{
MethodInfo mi = typeof(JobsService).GetMethod(methodName);
string s = (string)mi.Invoke(this, new[] { parameters });
}
那么,您的 JobsController
AddOrUpdate
方法将是:
public IActionResult AddOrUpdate([FromQuery]string recurringJobId = "",[FromQuery]string methodName = "", [FromQuery] bool remove = false, [FromQuery] string cronExpression = "*/1 * * * *", [FromBody] Parameters parameters = null)
{
RecurringJob.AddOrUpdate<JobsService>(
recurringJobId,
js => js.InvokeMethod(methodName, parameters),
() => cronExpression
);
}
我在 class
中有方法 public
。当我将它添加到 hangfire 时出现编译器错误
Only public methods can be invoked in the background. Ensure your method has the
public
access modifier, and you aren't using explicit interface implementation.'
public class JobsService
{
public void TestJob2(Parameters parameters)
{
}
}
public class JobsController : ControllerBase
{
private readonly JobsService _jobsService;
public JobsController(JobsService jobsService)
{
_jobsService = jobsService;
}
public IActionResult AddOrUpdate([FromQuery]string recurringJobId = "", [FromQuery]string methodName = "", [FromQuery] bool remove = false, [FromQuery] string cronExpression = "*/1 * * * *", [FromBody] Parameters parameters = null)
{
Expression<Action> expression = ToExpression(() => { string t = (string)method.Invoke(_jobsService, new object[] { parameters }); });
RecurringJob.AddOrUpdate(recurringJobId, expression, cronExpression);
}
}
lambda 及其参数需要由 Hangfire 序列化。 AFAIK,它的主体必须是一个简单的调用,例如没有方法分配。
执行作业的 JobsService
实例不能是您的控制器引用的实例。我假设你已经为依赖注入设置了 Hangfire see "Using IoC containers", or JobsService
can be instantiated without needing any dependency see "Passing dependencies".
您需要为您的 JobsService
添加一个方法 class :
public void InvokeMethod(string methodName, Parameters parameters)
{
MethodInfo mi = typeof(JobsService).GetMethod(methodName);
string s = (string)mi.Invoke(this, new[] { parameters });
}
那么,您的 JobsController
AddOrUpdate
方法将是:
public IActionResult AddOrUpdate([FromQuery]string recurringJobId = "",[FromQuery]string methodName = "", [FromQuery] bool remove = false, [FromQuery] string cronExpression = "*/1 * * * *", [FromBody] Parameters parameters = null)
{
RecurringJob.AddOrUpdate<JobsService>(
recurringJobId,
js => js.InvokeMethod(methodName, parameters),
() => cronExpression
);
}