I次のエラーメッセージが出ます:どれもしていない「Autofac.Core.Activators.Reflection.DefaultConstructorFinder」
のSystem.InvalidOperationExceptionを:タイプ のコントローラを作成 にしようとしたときにエラーが発生しました'App.Web.Controllers.ContractWizardController'。 コントローラにパラメータのないパブリックコンストラクタがあることを確認します。 ---> Autofac.Core.DependencyResolutionException:タイプ 「App.Web.Controllers.ContractWizardController」に 「Autofac.Core.Activators.Reflection.DefaultConstructorFinder」で見つかったコンストラクタ のいずれも 利用可能なサービスを使用して起動することはできませんパラメーター: コンストラクター 'Void .ctor(App.Service.IWizardTypeStepService、 App.Service.IAppBrandService、App.Service.IAppInstitutionService)の' App.Service.IWizardTypeStepService wizardTypeStepService 'を解決できません。 Autofac.Core.Resolving.InstanceLookup.Executeで Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstanceで
(IComponentContext コンテキスト、IEnumerableをパラメータ)() Autofac.Core.Resolving.ResolveOperationで
。 Autofac.ResolutionExtensions.TryResoでAutofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 登録、IEnumerableを1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1パラメータ)でGetOrCreateInstance(ISharingLifetimeScope currentOperationScope、IComponentRegistration登録、 IEnumerableを1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable
1パラメータ) Autofac.ResolutionExtensions.ResolveOptional(IComponentContext コンテキスト、タイプサービス種別)で Autofac.ResolutionExtensions.ResolveOptional(IComponentContext コンテキスト、タイプサービス種別、IEnumerable`1パラメータ)でlveService(IComponentContext コンテキスト、サービスサービス、IEnumerableを1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable
1パラメータ)で System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContextの RequestContextの、タイプcontrollerType)---内部例外 スタックトレースの終わり---でで Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(タイプ サービス種別) System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Cr システムでSystem.Web.Mvc.DefaultControllerFactory.CreateController(RequestContextの RequestContextの、ストリングコントローラ名)で System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContextの RequestContextの、タイプcontrollerType)で(RequestContextの RequestContextの、タイプcontrollerType)をeate .Web.Mvc.MvcHandler.ProcessRequestInit System.Webのに(HttpContextBase のHttpContext、IController &コントローラ、IControllerFactory &工場)System.Web.Mvc.MvcHandler.BeginProcessRequestで
(HttpContextBase のHttpContext、AsyncCallbackコールバックオブジェクトの状態)。 Mvc.MvcHandler.BeginProcessRequest(Htt pContextのHttpContext、AsyncCallback System.Web.HttpApplication.CallHandlerExecutionStep.SystemでSystem.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequestでコールバック、状態オブジェクト()のHttpContext コンテキスト、AsyncCallback CBと、extraDataオブジェクト)。 System.Web.HttpApplicationでWeb.HttpApplication.IExecutionStep.Execute() を呼び出します。ExecuteStep(IExecutionStepステップ、 ブール& completedSynchronously)
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));
}
}
}
IRepository
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.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
}
}
WizardTypeStepRepository
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);
}
}
WizardTypeStepService
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);
}
}
}
01 RepositoryBase
ContractWizardController
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; }
} }