没有定义无参数构造函数 Hangfire Ninject

No parametless constructor defined Hangfire Ninject

我在激活 class 的实例时遇到问题,没有定义无参数构造函数。

构造函数:

public HangfireExecutor(ICommandDispatcher commandDispatcher, IQueryDispatcher queryDispatcher, IMapper mapper)

我如何注册和配置 Hangfire(使用三个点代替敏感信息):

[assembly: OwinStartupAttribute(typeof(Web2.Startup))]
    public partial class Startup

    private IAppBuilder _app;
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        _app = app;

        GlobalConfiguration.Configuration.UseSqlServerStorage("...");

        _app.UseHangfireDashboard("/...", new DashboardOptions
        {
            Authorization = new[] { new HangfireDashboardAuthorizationFilter() },
            AppPath = "/Identity/Create"
        });

        _app.UseHangfireServer();

        _app.UseNinjectMiddleware(CreateKernel);
    }

在 IoC 容器中注册:

public partial class Startup
{
...
protected IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    ...
    kernel.Bind<HangfireExecutor>().ToSelf().InBackgroundJobScope();
    GlobalConfiguration.Configuration.UseNinjectActivator(kernel);
    return kernel;

错误:

System.MissingMethodException
No parameterless constructor defined for this object hangfire ninject System.RuntimeTypeHandle.CreateInstance
System.MissingMethodException: No parameterless constructor defined for this object
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Hangfire.JobActivator.ActivateJob(Type jobType)
at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)

对我来说,Hangfire 似乎没有使用 Ninject 激活器(?),但我不知道为什么。

我已经学习了两个教程:在 Hangfire 站点和 Hangfire.Ninject github 以及几个 github 回购和 SO 问题。

实例化 Hangfire 未使用的其他 classes 效果很好;使用无参数构造函数实例化 Hangfire 执行器也能正常工作。

我正在使用:

由于方法 _app.UseNinjectMiddleware(CreateKernel); 不创建内核(只是保留委托给创建内核的方法),在我的例子中,Hangfire 配置中命令的正确顺序应该是:

public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        _app = app;

        GlobalConfiguration.Configuration.UseSqlServerStorage("...");

        _app.UseHangfireDashboard("/...", new DashboardOptions
        {
            Authorization = new[] { new HangfireDashboardAuthorizationFilter() },
            AppPath = "/Identity/Create"
        });

        _app.UseNinjectMiddleware(CreateKernel);
    }

然后在 CreateKernel 方法的末尾:

kernel.Bind<HangfireExecutor>().ToSelf().InBackgroundJobScope();

        GlobalConfiguration.Configuration.UseNinjectActivator(kernel);

        _app.UseHangfireServer();

        return kernel;

现在 Hangfire 开始解决依赖关系。我认为在启动应用程序后尽快创建内核很重要 - 否则 Hangfire 可能无法初始化并且后台作业将无法执行。