1

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

构造函数:

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 未使用的其他类效果很好;使用无参数构造函数来初始化 Hangfire 执行器也可以正常工作。

我在用着:

  • ASP .NET MVC 5
  • .NET 框架 4.6.1,
  • 吊火 1.6.21
  • Hangfire.Ninject 1.2
4

1 回答 1

0

由于方法 _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 并且不会执行后台作业。

于 2018-11-22T08:50:09.117 回答