问题
I've got the same problem as in this question
The error message is: A single instance of controller 'Search.Web.Controllers.AdvancedController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.
Code in Global.asax:
protected void Application_Start()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<AdvancedController>().InstancePerHttpRequest();
containerBuilder.RegisterType<MemoryBodyTypeRepository>().As<IBodyTypeRepository>;
containerBuilder.RegisterType<BodyTypePictureClassFinder>().As<IBodyTypePictureClassFinder>();
var container = containerBuilder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
This is some Code from AdvancedController:
private readonly IBodyTypeRepository _bodyTypeRepository;
private readonly IBodyTypePictureClassFinder _bodyTypePictureClassFinder;
public AdvancedController(IBodyTypeRepository bodyTypeRepository, IBodyTypePictureClassFinder bodyTypePictureClassFinder)
{
_bodyTypeRepository = bodyTypeRepository;
_bodyTypePictureClassFinder = bodyTypePictureClassFinder;
}
[HttpGet]
public ActionResult Index()
{
var advancedSearchViewModel = new AdvancedSearchViewModel();
return View(advancedSearchViewModel);
}
public ActionResult BodyTypes()
{
// this uses the repositories to create the ViewModel
return View(bodyTypesViewModel);
}
And the Index View:
<div>
@Html.Action("BodyTypes","Advanced")
</div>
If I execute this View I get the Message stated above. I also tried to remove the InstancePerHttpRequest or use RegisterControllers instead of regestering them explicitly, but that didn't work, too. If I use RegisterControllers I get the same Error. If I remove InstancePerHttpRequest it somehow executes the whole View two times, which is also not what I'd like to do ;)
I hope anybody can help. This is a real Showstopper for me.
Thansk a lot!!!!
Regards, Florian Fanderl
回答1:
I know this has been posted long time ago. I had same problem, I could solve my problem with your question. Hope this will be useful for some one in use.
var builder = new ContainerBuilder();
builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>().InstancePerHttpRequest();
builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker().InstancePerHttpRequest();
builder.RegisterType<ConfigService>().As<IConfigService>().InstancePerLifetimeScope();
builder.RegisterType<EntryService>().As<IEntryService>().InstancePerLifetimeScope();
builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();
builder.RegisterType<MessageService>().As<IMessageService>().InstancePerLifetimeScope();
builder.RegisterType<CloudService>().As<ICloudService>().InstancePerLifetimeScope();
builder.RegisterType<Services>().As<IServices>().InstancePerLifetimeScope();
builder.RegisterType<AccountController>().InstancePerDependency();
_containerProvider = new ContainerProvider(builder.Build());
ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
AuthConfig.RegisterAuth();
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
// Quartz.NET scheduler
ISchedulerFactory factory = new StdSchedulerFactory();
var scheduler = factory.GetScheduler();
scheduler.JobFactory = new AutofacJobFactory(ContainerProvider);
scheduler.Start();
}
if you notice this is my someController
builder.RegisterType<AccountController>().InstancePerDependency();
来源:https://stackoverflow.com/questions/9480177/autofac-mvc3-html-action