Tuesday, 15 September 2015

c# - Parameterless constructor error. (upgrade to autofac 3) -



c# - Parameterless constructor error. (upgrade to autofac 3) -

in application_start() have next code. when business relationship controller get's created parameterless constructor error. accountcontroller not have parameterless constructor. seems autofac no longer configured?

account controller expects following.

public accountcontroller(iflexmembershipprovider membership, iflexoauthprovider openauth)

i not sure why autofac not injecting 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));

you need register assembly contains controllers before calling .build method:

builder.registercontrollers(typeof(mvcapplication).assembly); var container = builder.build();

in illustration assumed application class called mvcapplication , took assembly. if controllers defined in different assembly should specify assembly.

the documentation of autofac has illustration have gone through.

c# asp.net-mvc autofac

No comments:

Post a Comment