在asp.net mvc分项目开发中,如果类库位于其他的项目中,则必须在global中对其他项目的类库进行注册,否则会报“
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'SportsStore.WebUI.Controllers.ListController' can be invoked with the available services and parameters:
Cannot resolve parameter 'SportsStore.Domain.Abstrace.IProductRepository productRepository' of constructor 'Void .ctor(SportsStore.Domain.Abstrace.IProductRepository)'.”
解决方案很简单,注册就行。
ContainerBuilder builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); //注册类库程序集 builder.RegisterAssemblyTypes(Assembly.Load("SportsStore.Domain")).AsImplementedInterfaces(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
注:通过nuget 安装Autofac.mvc4