We left Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

MissingMethodException on used IoC-container

I’ve updated Hangfire to 1.4 version and got some bug.
When job starts it fails with error (seen at Dashboard):

System.MissingMethodExceptionNo parameterless constructor defined for this object.

I know with this error, but I cannot understand what is wrong. I use LightInject IoC-container and delegate call to create instance to it. Look:
Config:

    GlobalConfiguration.Configuration
        .UseActivator(new ContainerJobActivator(container));
        .UseSqlServerStorage(
            "database",
            new SqlServerStorageOptions
            {
                PrepareSchemaIfNecessary = false
            });

    app.UseHangfireServer();

Activator code:

public class ContainerJobActivator : JobActivator
    {
        private ServiceContainer _container;
 
        public ContainerJobActivator(ServiceContainer container)
        {
            _container = container;
        }
 
        public override object ActivateJob(Type type)
        {
            using (_container.BeginScope())
            {
                return _container.GetInstance(type);
            }
        }
    }

But when I connect to app through debugger, ActivateJob is never called.
I see this problem only at server. By other words whn I run application at my work machine from VS there is no problem: activator is called, job runs. The problem appears when I publish app to a server.

Versions of .NET are same at both of machines.

What kind of additional information can I give to find a reason of this issue?

System.MissingMethodExceptionNo parameterless constructor defined for this object.

This message is likely to be thrown by default JobActivator implementation that uses Activator.CreateInstance method to create job class instances. It knows how to build only classes with default constructors.

But when I connect to app through debugger, ActivateJob is never called.

Here are the possible reasons:

  • Your code does not call the UseActivator method, for example, due to the IF statement.
  • You are starting the server before calling the UseActivator method.
  • There is a code that calls UseActivator(new JobActivator()).

1st and 3rd clauses can not be a reason, I register activator through GlobalConfiguration (it’s seen in first code block) and I register an other activator nowhere, only in OWIN Startup class.
But second reason looks like the most possible. App logs time when it starts itself and when hangfire servers start.
But there is one interesting thing. I used this article http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html and I need keep app awaken.
Do I understand correctly I should register activator through GlobalConfiguration in HttpApplication descendant class? And I should leave configurations at two places: owin startup and httpapplication descendant classes to make sure that application will be configured right?

OWIN startup code is called after IProcessHostPreloadClient.Preload and after Application_Start methods. There is no need to call GlobalConfiguration methods in the Configure method. Place all the configuration to the HangfireBootstrapper class as written in the docs, including the call to UseActivator method.

By the way, if you have the following line in your HangfireBootstrapper class (it should be there), don’t call the app.UseHangfireServer, as you already have a server instance.

_backgroundJobServer = new BackgroundJobServer();

Good! Thanks! Works!
How can I mark this post solved?