I get the following error message:
System.InvalidOperationException: An error occurred when trying to create a controller of type 'App.Web.Controllers.ContractWizardController'. Make sure that the controller has a parameterless public constructor. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'App.Web.Controllers.ContractWizardController' can be invoked with the available services and parameters: Cannot resolve parameter 'App.Service.IWizardTypeStepService wizardTypeStepService' of constructor 'Void .ctor(App.Service.IWizardTypeStepService, App.Service.IAppBrandService, App.Service.IAppInstitutionService)'.
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 parameters) at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) at Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType) at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) --- End of inner exception stack trace --- at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& 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);
}
}
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
}
}
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);
}
}
}
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; }
} }
The error message tells you that :
Cannot resolve parameter 'App.Service.IWizardTypeStepService wizardTypeStepService'
Your registration of the corresponding service is like this :
// Services
builder.RegisterAssemblyTypes(typeof(AppBrandService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();
But if you look at WizardTypeStepService, it doesn't implement any interfaces. Implement IWizardTypeStepService and your error should disappear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With