I have the below Autofac config but I’m still getting the error “No scope with a Tag matching AutofacWebRequest” when a scheduled Hangfire tries to execute
// Job ID: #7 using OnlinePortal.Client.Controllers;
var homeController = Activate<HomeController>(); homeController.InsertAllAccountsForLoginUserId();
public override void MVCInit(ContainerBuilder builder)
{
_logger.Info("Starting MVCInit ...");
// Register the content, configuration and cache managers, we have to register both as singleinstances which
// sucks a bit, since CacheManager doesn't need to be, but whatever
// Also need to register contentMangaer as IStartable so that autofac will call it after the container
// has been intialized
// Note: Order of these cannot be changed!
_logger.Info("Registering CacheManager, ConfigurationManager and ContentManager against IoC, singleInstances ...");
var requestTag = MatchingScopeLifetimeTags.RequestLifetimeScopeTag;
var jobTag = AutofacJobActivator.LifetimeScopeTag;
builder.RegisterType<CacheManager>().AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterType<ConfigurationManager>()
.As<IStartable>()
.As<IConfigurationManager>()
.InstancePerLifetimeScope();
// Register stuff for custom membership providers
_logger.Info("Manually registering membershipprovider and forms authentication services ...");
builder.RegisterType<WebMembershipProvider>();
builder.RegisterType<MembershipService>().AsImplementedInterfaces();
builder.Register(c => c.Resolve<WebMembershipProvider>()).As<MembershipProvider>();
builder.RegisterType<CookieManager>().As<ICookieManager>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
builder.RegisterType<Localize>().As<ILocalize>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
builder.RegisterType<BaseModelFactory>().AsSelf().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<LoginPortalManager>().As<ILoginPortalManager>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<AdminPortalManager>().As<IAdminPortalManager>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<RGMPDomainRepo.Repository>().As<RGMPDomainRepo.IRepository>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<ECMDoxim>().As<IECMDoxim>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<AppConfigManager>().As<IAppConfigManager>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<RGMPNotification>().As<IRGMPNotification>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<Base64Service>().As<IBase64Service>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<DataphileExtractManager>().As<IDataphileExtractManager>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
//builder.RegisterType<RGMPDomainSPFuncRepo.Repository>().As<RGMPDomainSPFuncRepo.IRepository>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
// Register controllers
_logger.Info("Registering MVC controllers ...");
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterControllers(typeof(ErrorController).Assembly);
builder.RegisterControllers(typeof(HomeController).Assembly);
builder.RegisterControllers(typeof(LocalizationController).Assembly);
builder.RegisterType<HomeController>().InstancePerMatchingLifetimeScope(requestTag, jobTag);
// Register model binders
_logger.Info("Registering MVC model binders ...");
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
_logger.Info("Registering filter provider ...");
builder.RegisterFilterProvider();
// Build container and set the resolver
_logger.Info("Setting the MVC dependency resolver to use Autofac ...");
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Assign to application
MvcApplication.Container = container;
_logger.Info("MVCInit complete ...");
GlobalConfiguration.Configuration.UseAutofacActivator(container);
}