Autofac + MVC3 + HTML.Action [英] Autofac + MVC3 + Html.Action
问题描述
我遇到的问题与中的问题相同
错误消息是: 控制器"Search.Web.Controllers.AdvancedController"的单个实例不能用于处理多个请求.如果正在使用自定义控制器工厂,请确保为每个请求创建一个新的控制器实例.
Global.asax中的代码:
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);
}
这是AdvancedController的一些代码:
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);
}
和索引视图:
<div>
@Html.Action("BodyTypes","Advanced")
</div>
如果执行此视图,则会收到上述消息.我还尝试删除InstancePerHttpRequest或使用RegisterControllers而不是显式地重新注册它们,但这也没有用. 如果我使用RegisterControllers,则会出现相同的错误.如果我删除InstancePerHttpRequest,它将以某种方式执行整个View两次,这也不是我想要做的;)
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 ;)
我希望任何人都能提供帮助.对我来说,这是一个真正的Showstopper.
I hope anybody can help. This is a real Showstopper for me.
非常感谢!!!
关于, 弗洛里安·范德(Florian Fanderl)
Regards, Florian Fanderl
推荐答案
我知道这已经发布很久了. 我遇到了同样的问题,我可以用您的问题解决我的问题. 希望这对一些正在使用的人有用.
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();
}
如果您注意到这是我的someController
if you notice this is my someController
builder.RegisterType<AccountController>().InstancePerDependency();
这篇关于Autofac + MVC3 + HTML.Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!