I've got a simple .NET 4.5 console app with Hangfire.Core and Hangfire.SqlServer packages installed.
In my main method I enqueue a background job like this:
BackgroundJob.Enqueue<Test>((t) => Console.WriteLine(t.Sum(3,4)));
My Test class looks like this:
public class Test
{
public Test(){ }
public int Sum(int a, int b)
{
return a + b;
}
}
When i F5 my program I get an exception on the line with Enqueue:
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll Additional information: variable 't' of type 'HangfireTest.Test' referenced from scope '', but it is not defined"
When I create my Test class in code with "new" and use non-generic Enqueue method - everything works:
BackgroundJob.Enqueue(() => Console.WriteLine(new Test().Sum(3,4)));
But I need a generic one, cause I'd like to create an interface ITest and use dependency injection to do something like this:
BackgroundJob.Enqueue<ITest>((t) => Console.WriteLine(t.Sum(3,4)));
So, what am I doing wrong here?
t
is never instantiated. – Entremets