我刚刚开始使用HangFire。当我从IIS Express本地运行它时,作业似乎工作正常,但是当我在我的开发服务器上运行它时,我得到的错误是没有无参数构造函数定义。我之前使用的是Simple Injector,但现在我已经转移到了Autofac。
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigureIoc(app);
ConfigureRoutes();
ConfigureHangfire(app);
ScheduleXeroMonitor();
}
public void ConfigureHangfire(IAppBuilder app)
{
var authList = new List<IDashboardAuthorizationFilter>
{
new ClaimsBasedAuthorizationFilter("role", "admin")
};
var options = new DashboardOptions
{
Authorization = authList,
AppPath = "/Manage"
};
app.UseHangfireDashboard("/hangfire", options);
app.UseHangfireServer();
}
public void ConfigureRoutes()
{
BundleTable.EnableOptimizations = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public void ConfigureIoc(IAppBuilder app)
{
var builder = DependencyRegistrar.RegisterClasses();
var container = builder.Build();
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
GlobalConfiguration.Configuration.UseAutofacActivator(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login"),
CookieSecure = CookieSecureOption.SameAsRequest,
});
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
AuthenticationMode = AuthenticationMode.Passive
});
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
}
public void ScheduleXeroMonitor()
{
var backGroundWorker = DependencyResolver.Current.GetService<IXeroBackGroundWorker>();
RecurringJob.AddOrUpdate("XeroPolling", () => backGroundWorker.PollJobs(), Cron.Minutely); //change the Cron to every 5 minutes
var emailWorker = DependencyResolver.Current.GetService<IEmailService>();
RecurringJob.AddOrUpdate("EmailPolling", () => emailWorker.SendQueuedEmails(), Cron.Minutely);//"*/10 * * * *"
}
}
这是我注册依赖项的代码。
public class DependencyRegistrar
{
public static ContainerBuilder RegisterClasses()
{
var builder = new ContainerBuilder();
builder.RegisterType<XeroSyncRestClient>().As<IXeroSyncRestClient>().InstancePerLifetimeScope();
builder.RegisterType<XeroBackGroundWorker>().As<IXeroBackGroundWorker>().InstancePerLifetimeScope();
builder.RegisterType<EmailService>().As<IEmailService>().InstancePerLifetimeScope();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
return builder;
}
}
有人可以告诉我我做错了什么吗?我也在Hangfire论坛上发布了这个
答案 0 :(得分:2)
由于您已在AutoFac
中添加了GlobalConfiguration.Configuration.UseAutofacActivator(container);
,您只需简单地让hangfire和Autofac为您解决依赖关系。
public void ScheduleXeroMonitor()
{
//var backGroundWorker = DependencyResolver.Current.GetService<IXeroBackGroundWorker>(); --> let hangfire and autofac do this for you
RecurringJob.AddOrUpdate<IXeroBackGroundWorker>("XeroPolling", backGroundWorker => backGroundWorker.PollJobs(), Cron.Minutely); //change the Cron to every 5 minutes
//var emailWorker = DependencyResolver.Current.GetService<IEmailService>();--> let hangfire and autofac do this for you
RecurringJob.AddOrUpdate<IEmailService>("EmailPolling", emailWorker => emailWorker.SendQueuedEmails(), Cron.Minutely);//"*/10 * * * *"
}