I have installed HangFire and also HangFire Autofac.
All workes fine. I have no problems resolving dependancies and my enqueued jobs are fired without problems. Now i have a need to schedule a job in the night so i want to configure my IIS to always running. So i followed http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html and configured as so. When i now run an enqueued job i am getting an error:
No parameterless constructor defined for this object
Has anyone an idea about this?
My Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
#if DEBUG
BundleTable.EnableOptimizations = false;
#else
BundleTable.EnableOptimizations = true;
#endif
MvcHandler.DisableMvcResponseHeader = true;
// Clears all previously registered view engines.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
HangfireBootstrapper.Instance.Start();
}
protected void Application_End(object sender, EventArgs e)
{
HangfireBootstrapper.Instance.Stop();
}
My StartUp.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
// Configure dependancies
Bootstrapper.SetAutofacContainer(app);
// Configure authentication
ConfigureAuth(app);
// Configure Hangfire
var dashboardOptions = new DashboardOptions { Authorization = new[] { new HangfireAuthorizationFilter() } };
app.UseHangfireDashboard("/hangfire", dashboardOptions);
}
}
Here is my Autofac configuration:
public class Bootstrapper
{
public static void SetAutofacContainer(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterFilterProvider();
builder.Register(c => new HttpContextWrapper(HttpContext.Current)).As<HttpContextBase>().InstancePerLifetimeScope();
builder.RegisterType<DataContextFactory>().As<IDataContextFactory>().InstancePerLifetimeScope();
builder.RegisterType(typeof(EDMSContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
builder.RegisterType<EDMSContext>().AsSelf().InstancePerLifetimeScope();
builder.RegisterModule(new IdentityModule());
builder.RegisterModule<AutofacModule>();
// Automapper
var autoMapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new ReportingAutoMapperProfile());
cfg.AddProfile(new WebAutoMapperProfile());
});
builder.Register(ctx => autoMapperConfig.CreateMapper()).SingleInstance();
builder.RegisterModule<AutofacModule>();
var container = builder.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}