Reputation: 16002
In application_start()
I have the following code.
When the Account Controller get's created I get the parameterless constructor error.
AccountController does not have a parameterless constructor. It seems that autofac is no longer configured?
Account Controller Expects the following.
public AccountController(IFlexMembershipProvider membership, IFlexOAuthProvider openAuth)
I am not sure why autofac is not injecting the dependences?
var builder = new ContainerBuilder();
builder.RegisterType<DataContext>()
.As<IRepository>()
.As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<FlexMembershipProvider>().As<IFlexMembershipProvider>();
builder.RegisterType<FlexMembershipProvider>().As<IFlexOAuthProvider>();
builder.RegisterType<FlexRoleProvider>().As<IFlexRoleProvider>();
builder.RegisterType<UserStorage>().As<IFlexUserStore>().InstancePerLifetimeScope();
builder.RegisterType<RoleStorage>().As<IFlexRoleStore>().InstancePerLifetimeScope();
builder.RegisterType<DefaultSecurityEncoder>().As<ISecurityEncoder>().SingleInstance();
builder.RegisterType<AspnetEnvironment>().As<IApplicationEnvironment>();
builder.RegisterType<InvestorService>().As<IInvestorService>();
builder.RegisterType<InvestmentService>().As<IInvestmentService>();
builder.RegisterType<BrokerService>().As<IBrokerService>().As<IListService<Broker>>();
builder.RegisterType<PortfolioManagerService>().As<IListService<PortfolioManager>>();
builder.RegisterModelBinderProvider();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
Upvotes: 1
Views: 1259
Reputation: 11
Try add this before calling builder.Build method:
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(x => !(x.IsValueType) && !(x.IsAssignableFrom(typeof(string)))));
Upvotes: 0
Reputation: 1038710
You need to register the assembly that contains your controllers before calling the .Build
method:
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
In this example I assumed that your application class is called MvcApplication
and took its assembly. If your controllers are defined in a different assembly you should specify this assembly.
The documentation of AutoFac
has an example that you could have gone through.
Upvotes: 4