Intermittent parameterless constructor errors

Tags: #<Tag:0x00007efd28557950>

I’m creating a proof of concept application. In response to an API call, I make a call to MyClass.method. Simple enough:

public MyController(IBackgroundJobClient backgroundJobClient, MyClass myClass)
        {
            _backgroundJobClient = backgroundJobClient;`
            _myClass = myClass;
        }


[HttpPost]
        [Route("MyRoute")]
        public async Task<IActionResult> MyPost([FromBody] Input inputmsg)
        {
            _backgroundJobClient.Schedule(() => _delayedPublisher.PublishMinEvent(eventToPublish), DateTime.UtcNow.AddSeconds(10));

                return Ok();
        }

So far so good. And I have my IOC set up like so in my startup:

ConfigureServices
services.AddHangfire(config =>
{
GlobalConfiguration.Configuration
.UseSqlServerStorage(“Database=Hangfire;Server=.;Integrated Security = True;MultipleActiveResultSets=True;”);
});
services.AddScoped();
GlobalConfiguration.Configuration
.UseActivator(new BasicJobActivator(services.BuildServiceProvider()));
services.AddHangfireServer();

My BasicJobActivator is pretty much as per the example in the documentation:

public class BasicJobActivator:JobActivator
{
    private readonly IServiceProvider _services;

    public BasicJobActivator(IServiceProvider services)
    {
        _services = services;
    }

    public override object ActivateJob(Type type)
    {
        return _services.GetService(type);
    }
}

And for the most part, this works. If I submit a batch of jobs, some of them hit my code and progress to “Succeeded” in my Jobs table. About 60% of them. However, some don’t and my logging claims that I’m calling a parameterless constructor for type MyClass.

I’ve added MyClass to my service collection. I’ve converted the service collection to a service provider. Service provider is resolved in my job activator and I’ve registered that activator with HangFire. Am I missing something?

Thanks.