2013-02-17 96 views
1

application_start()我有以下代碼。 當賬戶控制器被創建時,我得到了無參數的構造函數錯誤。 AccountController沒有無參數的構造函數。看來autofac不再配置?無參數構造函數錯誤。 (升級到autofac 3)

帳戶控制器期望以下。

public AccountController(IFlexMembershipProvider membership, IFlexOAuthProvider openAuth)

我不知道爲什麼autofac沒有注入相關性?

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)); 
+0

我有問題。 – IamStalker 2013-02-17 08:27:41

回答

4

您需要調用.Build方法之前註冊包含您的控制器組件:

builder.RegisterControllers(typeof(MvcApplication).Assembly); 
var container = builder.Build(); 

在這個例子中,我假設你的應用程序類被稱爲MvcApplication,把它組裝。如果您的控制器在不同的裝配體中定義,則應指定此裝配體。

documentation of AutoFac有一個例子,你可以通過。

0

嘗試調用builder.Build方法之前補充一點:

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(x => !(x.IsValueType) && !(x.IsAssignableFrom(typeof(string)))));