1

In my project I have a problem with background Job. I am using Hangfire and default .Net Core Dependency injection.

Startup.cs

ConfigureServices:

services.AddTransient<IAuthorizable, AuthorizeService>();
services.AddTransient<IEditable, ConfigureService>();
services.AddTransient<IRequested, RequestService>();
services.AddTransient<INotified, NotificationService>();
services.AddHangfire(x => x.UseSqlServerStorage("ConnectionString"));

Configure:

app.UseHangfireDashboard();
app.UseHangfireServer();

When my Job tries to run, I receive this error :

System.InvalidOperationException: No service for type 'Mapper.Services.RequestService' has been registered.

What am I do wrong?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
donex93
  • 513
  • 2
  • 5
  • 16
  • DI knows how to deal with IRequested by providing a RequestService. But that is for classes asking for IRequested. It does not know how to deal with classes asking for RequestService directly. so you need to add RequestService to services as well. – Nkosi Jan 12 '17 at 13:17
  • 1
    when you run your job , in your contructor of the job class are you using `IRequested` or `RequestService` , you should use `IRequested` so it will understand the `RequestedService` will be used/registered to use. – Turbot Jan 12 '17 at 14:00

1 Answers1

0

DI knows how to deal with IRequested by providing a RequestService. But that is for classes asking for IRequested. It does not know how to deal with classes asking for RequestService directly.

So based on the error message you need to register RequestService with services as well.

services.AddTransient<IAuthorizable, AuthorizeService>();
services.AddTransient<IEditable, ConfigureService>();
services.AddTransient<IRequested, RequestService>();
services.AddTransient<RequestService, RequestService>(); //<-- 
services.AddTransient<INotified, NotificationService>();
services.AddHangfire(x => x.UseSqlServerStorage("ConnectionString"));

Update based on comment.

You should also consider updating the Job to be dependent on the IRequested abstraction as apposed to the implemented RequestService if that is indeed the case.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Can you please help me with this question : https://stackoverflow.com/questions/49514099/hangfire-server-unable-to-pick-job-in-case-of-strategy-design-pattern – I Love Stackoverflow Mar 28 '18 at 07:39