我正在尝试使用HangFire在类上调用操作方法。从下面的代码中,如果在HangFire之外调用action方法,则该方法可以正常工作,但是在使用HangFire时会引发异常。我也尝试使用Invoke()
,如其他类似文章所述。
表达主体应为类型 'MethodCallExpression'(Parameter'methodCall')'
我想弄清楚如何让HangFire执行这种类型的方法。
class Program
{
static void Main(string[] args)
{
var a = new actionTest();
// Calling this method prints out Hello World correctly
a.Doaction();
GlobalConfiguration.Configuration.UseSqlServerStorage(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True");
// Call this method from HangFire produces exception: Expression body should be of type 'MethodCallExpression'(Parameter'methodCall')'
BackgroundJob.Enqueue<actionTest>(a => a.Doaction());
}
}
public class actionTest
{
public actionTest()
{
Doaction = WriteHello;
}
public action Doaction;
public void WriteHello()
{
Console.WriteLine("Hello World");
}
}