C# 未找到具有';Autofac.Core.Activators.Reflection.DefaultConstructorFinder';
我收到以下错误消息: System.InvalidOperationException:尝试运行时出错 创建类型为的控制器 “App.Web.Controllers.ContractWizardController”。确保 控制器具有无参数公共构造函数。--> Autofac.Core.DependencyResolutionException:没有构造函数 发现 类型上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder” 可以使用调用“App.Web.Controllers.ContractWizardController” 可用服务和参数:无法解析参数 的“App.Service.IWizardTypeStepService向导TypeStepService” 构造函数的Void.ctor(App.Service.IWizardTypeStepService, App.Service.IAppBrandService,App.Service.IAppInstitutionService);C# 未找到具有';Autofac.Core.Activators.Reflection.DefaultConstructorFinder';,c#,autofac,C#,Autofac,我收到以下错误消息: System.InvalidOperationException:尝试运行时出错 创建类型为的控制器 “App.Web.Controllers.ContractWizardController”。确保 控制器具有无参数公共构造函数。--> Autofac.Core.DependencyResolutionException:没有构造函数 发现 类型上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”
在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable
1参数)位于
Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1
参数)在Autofac.Core.Resolving.InstanceLookup.Execute()上执行(在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(iShareInLiFeTimeScope 当前操作范围,IComponentRegistration, IEnumerable
1参数)在
Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration
注册,IEnumerable
1参数)
Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration
注册,IEnumerable1参数)
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration
注册,IEnumerable
1参数)
Autofac.ResolutionExtensions.TryResolveService(IComponentContext
上下文、服务、IEnumerable1参数、对象和实例)
在
Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext
上下文、服务、IEnumerable
1参数)位于
Autofac.ResolutionExtensions.ResolveOptional(IComponentContext
上下文,类型serviceType,IEnumerable`1参数)位于
Autofac.ResolutionExtensions.ResolveOptional(IComponentContext
上下文,键入serviceType)位于
Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(类型
服务类型)在
System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext
requestContext,类型controllerType)--内部异常结束
堆栈跟踪--at
System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext
requestContext,类型controllerType)位于
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext,类型controllerType)位于
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
requestContext,String controllerName)位于
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase
httpContext、IController和controller、IControllerFactory和factory)在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext、异步回调、对象状态)位于 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext HttpContext, 异步回调,对象状态)在 System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.BeginProcessRequest(HttpContext 上下文、异步回调、对象外部数据)位于 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤, 布尔值(同步完成(&C)) Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Autofac and Automapper configurations
Bootstrapper.Run();
}
}
Bootstrapper.cs
namespace App.Web.App_Start
{
public static class Bootstrapper
{
public static void Run()
{
SetAutofacContainer();
//Configure AutoMapper
AutoMapperConfiguration.Configure();
}
private static void SetAutofacContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerRequest();
// Repositories
builder.RegisterAssemblyTypes(typeof(AppBrandRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerRequest();
// Services
builder.RegisterAssemblyTypes(typeof(AppBrandService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
namespace App.Web.App\u开始
{
公共静态类引导程序
{
公共静态无效运行()
{
SetAutofacContainer();
//配置自动映射器
AutoMapperConfiguration.Configure();
}
私有静态void SetAutofacContainer()
{
var builder=new ContainerBuilder();
RegisterController(Assembly.getExecutionGassembly());
builder.RegisterType().As().InstancePerRequest();
builder.RegisterType().As().InstancePerRequest();
//存储库
RegisterAssemblyTypes(typeof(AppBrandRepository).Assembly)
.Where(t=>t.Name.EndsWith(“存储库”))
.AsImplementedInterfaces()
.InstancePerRequest();
//服务
builder.RegisterAssemblyTypes(typeof(AppBrandService).Assembly)
.Where(t=>t.Name.EndsWith(“服务”))
.AsImplementedInterfaces()
.InstancePerRequest();
IContainer容器=builder.Build();
SetResolver(新的AutofacDependencyResolver(容器));
}
}
}
i假设性
namespace App.Data.Infrastructure
{
public interface IRepository<T> where T : class
{
// Marks an entity as new
void Add(T entity);
// Marks an entity as modified
void Update(T entity);
// Marks an entity to be removed
void Delete(T entity);
void Delete(Expression<Func<T, bool>> where);
// Get an entity by int id
T GetById(int id);
// Get an entity using delegate
T Get(Expression<Func<T, bool>> where);
// Gets all entities of type T
IEnumerable<T> GetAll();
// Gets entities using delegate
IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
}
}
namespace App.Data.Repositories
{
public class WizardTypeStepRepository : RepositoryBase<WizardTypeStep>, IWizardTypeStepRepository
{
public WizardTypeStepRepository(IDbFactory dbFactory) : base(dbFactory)
{
}
public IEnumerable<StepNav> StepNav(string langCode = "en", int wizardType = 1)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
var step = from a in db.Steps
join b in db.ItemLanguages
on a.ItemCode equals b.ItemCode
where b.LangCode == langCode
where a.WizardTypeId == wizardType
select new StepNav
{
StepNo = a.StepNo,
ItemLegend = b.ItemLegend,
Url = a.Url
};
return step;
}
}
}
public interface IWizardTypeStepRepository : IRepository<WizardTypeStep>
{
IEnumerable<StepNav> StepNav(string langCode = "en", int wizardType = 1);
}
}
namespace App.Data.Infrastructure
{
公共接口IRepository,其中T:class
{
//将实体标记为新实体
无效添加(T实体);
//将实体标记为已修改
无效更新(T实体);
//标记要删除的实体
无效删除(T实体);
作废删除(如有);
//通过int-id获取实体
T GetById(int-id);
//使用委托获取实体
T Get(表达式在何处);
//获取T类型的所有实体
IEnumerable GetAll();
//使用委托获取实体
IEnumerable GetMany(表达式where);
}
}
RepositoryBase
namespace App.Data.Infrastructure
{
public abstract class RepositoryBase<T> where T : class
{
#region Properties
private ApplicationDbContext dataContext;
private readonly IDbSet<T> dbSet;
protected IDbFactory DbFactory
{
get;
private set;
}
protected ApplicationDbContext DbContext
{
get { return dataContext ?? (dataContext = DbFactory.Init()); }
}
#endregion
protected RepositoryBase(IDbFactory dbFactory)
{
DbFactory = dbFactory;
dbSet = DbContext.Set<T>();
}
#region Implementation
public virtual void Add(T entity)
{
dbSet.Add(entity);
}
public virtual void Update(T entity)
{
dbSet.Attach(entity);
dataContext.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
dbSet.Remove(entity);
}
public virtual void Delete(Expression<Func<T, bool>> where)
{
IEnumerable<T> objects = dbSet.Where<T>(where).AsEnumerable();
foreach (T obj in objects)
dbSet.Remove(obj);
}
public virtual T GetById(int id)
{
return dbSet.Find(id);
}
public virtual IEnumerable<T> GetAll()
{
return dbSet.ToList();
}
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return dbSet.Where(where).ToList();
}
public T Get(Expression<Func<T, bool>> where)
{
return dbSet.Where(where).FirstOrDefault<T>();
}
#endregion
}
}
namespace App.Service
{
public interface IWizardTypeStepService
{
IEnumerable<WizardTypeStep> GetSteps(int Id);
IEnumerable<StepNav> AllSteps(string language = "en", int type = 1);
}
public class WizardTypeStepService
{
private readonly IWizardTypeStepRepository wizardTypeStepRepository;
private readonly IUnitOfWork unitOfWork;
public WizardTypeStepService(IWizardTypeStepRepository wizardTypeStepRepository, IUnitOfWork unitOfWork)
{
this.wizardTypeStepRepository = wizardTypeStepRepository;
this.unitOfWork = unitOfWork;
}
public IEnumerable<WizardTypeStep> GetSteps(int Id)
{
return wizardTypeStepRepository.GetMany(a => a.WizardTypeId == Id).OrderBy(a => a.StepNo);
}
public IEnumerable<StepNav> StepNav(string language = "en", int type = 1)
{
return wizardTypeStepRepository.StepNav(language, type);
}
}
}
namespace App.Data.Infrastructure
{
公共抽象类RepositoryBase,其中T:class
{
#区域属性
私有应用程序上下文数据上下文;
专用只读IDbSet数据库集;
受保护的IDbFactory数据库工厂
{
得到;
私人设置;
}
受保护应用程序BCO
namespace App.Web.Controllers {
public class ContractWizardController : AppController
{
private readonly IWizardTypeStepService wizardTypeStepService;
public ContractWizardController(IWizardTypeStepService wizardTypeStepService, IAppBrandService brand, IAppInstitutionService institution) : base(brand, institution)
{
this.wizardTypeStepService = wizardTypeStepService;
IEnumerable<StepNav> steps = wizardTypeStepService.AllSteps();
this.Steps = new StepNavViewModel(steps)
{
Steps = steps
};
this.ViewData["Steps"] = this.Steps;
}
// GET: ContractWizard
public ActionResult Index()
{
return View();
}
public ActionResult Step(int Id = 1)
{
ViewBag.Step = Id;
switch (Id)
{
case 1:
ViewBag.Title = "State";
ViewBag.Message = "State content goes here...";
break;
case 2:
ViewBag.Title = "Property";
ViewBag.Message = "Property Content goes here...";
break;
case 3:
ViewBag.Title = "Listing Agent";
ViewBag.Message = "Listing Agent content goes here...";
break;
case 4:
ViewBag.Title = "Selling Agent";
ViewBag.Message = "Selling Ageng content goes here...";
break;
case 5:
ViewBag.Title = "Finish";
ViewBag.Message = "Finish content goes here...";
break;
}
return View();
}
public StepNavViewModel Steps { get; set; }
} }
// Services
builder.RegisterAssemblyTypes(typeof(AppBrandService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();