I have a project with this structure
AdventureWorks.Web<- .NET Core web project (Services, Viewmodels, DataContext, Controller Views, etc )AdventureWorks.Console<- .NET Core console app (hangfire config server)AdventureWorks.Model<- just pure POCO model classes
I am trying to run the Hangfire server in a separate process (AdventureWorks.Console)
[https://docs.hangfire.io/en/latest/background-processing/processing-jobs-in-console-app.html]
Processing jobs in a console application.
I added a reference to AdventureWorks.Web to the AdventureWorks.Console - this way the Hangfire server would execute the code
static void Main(string[] args)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("Connection");
using (var server = new BackgroundJobServer())
{
Console.WriteLine("Hangfire Server started. Press any key to exit...");
Console.ReadKey();
}
}
In the web project I'm registering my services in the Startup class
services.AddTransient<IDepartmentServices, DepartmentServices>();
The constructor has a parameter witch is auto injected
private readonly AdventureWorksDataContext db;
public DepartmentServices(AdventureWorksDataContext _db)
{
db = _db;
}
In the controller the services are injected
private readonly IDepartmentServices Service;
public DepartmentsController(IDepartmentServices service)
{
Service = service;
}
In one method in the controller when the user click the button "create report" the method enqueue a job to create a report
BackgroundJob.Enqueue<**IDepartmentService**>((serv) => serv.CrearReport(data));
So far so good.
I can see in the dashboard the job created but it fails to execute
System.MissingMethodException Cannot create an instance of an interface.
which is obvious so my first question is
- How create a job respecting the dependency injection? I'm trying to follow the instruction here [https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html] but I don't see nothing about the dependency which is injected my best guest hangfire doesn't work with dependency injecting
So to continue I change the interface with the class
BackgroundJob.Enqueue<DepartmentService>((serv) => serv.CrearReport(data));
so far so good the job is created. But it fails with the error
System.MissingMethodException
No parameterless constructor defined for this object
which is obvious so my second question is
- how do I pass the parameter
datacontextto hangfire server? My best guess is I can't because hangfire server is using an Activator which wants an empty constructor
So to continue I followed the instructions here [https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html]
I created an empty constructor
public EmployeeService()
:this(new AdventureWorksDataContext())
{
}
and i need add an empty constructor to the datacontext
public AdventureWorksDataContext()
{
}
but now i need to override the "OnConfigure" method
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("????");<--connectionstring???
}
so my question is how do i get the connectionstring from the appsettings.json my best guest i cant ignoring this problem i add the direct connection
optionsBuilder.UseSqlServer("Server=.;Database=AdventureWorks2017;Trusted_Connection=True;");
this way the job is completed correct
but this solution i think is bad maybe the project structure is bad or how the dependency injection works with hangfire if is running in a separete process, how IoC works en hangfire server console
some one point me a better solution how structure the project
sorry for my English i dont't speak