Quantcast
Channel: Hangfire .net core console Dependency Injection - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Hangfire .net core console Dependency Injection

$
0
0

I have a proyect with this structure

  • AdventureWorks.Web <- web .net core project (Services, Viewmodels, DataContext, Controller Views, etc )
  • AdventureWorks.Console <- Console .net core (hangfire config server)
  • AdventureWorks.Model <- just pure POCO class model

i trying to run the Hangfire server in a separate proccess (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 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 im register 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 is 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

so to continue i change the interface whith the class

BackgroundJob.Enqueue<**DepartmentService**>((serv) => serv.CrearReport(data));

so far so good the job is created. but it fails whit 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 datacontext to hangfire server? my best guest is i can't because hangfire server is using an Activator wich wants an empty constructor

so to continu i follow the instruccion here [https://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html] i created a 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


Viewing all articles
Browse latest Browse all 2

Trending Articles