我在激活类的实例时遇到了问题,没有定义任何非参数构造函数。
构造函数:
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 repos等等问题。
对其他未被Hangfire使用的类进行统计,效果良好;使用无参数构造函数安装Hangfire执行器也能正常工作。
我在用:
发布于 2018-11-22 08:50:09
由于方法_app.UseNinjectMiddleware(CreateKernel);不创建内核(只保留对metod的委托,创建内核),在我的示例中,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可能不会被初始化,后台作业也不会被执行。
https://stackoverflow.com/questions/53412014
复制相似问题