我想在我的MVC3項目中創建一個自定義RoleProvider。我RoleProvider定義如下:MVC3 CustomRoleProvider依賴注入問題
public class MyRoleProvider : System.Web.Security.RoleProvider
{
private IAccountService _accountService;
public MyRoleProvider()
{
_accountService = DependencyResolver.Current.GetService<IAccountService>();
}
//...
}
正如你所看到的,我嘗試使用DependencyResolver讓我的綁定服務。我的結合是在我做App_Start:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
// Add data and infrastructure modules
var modules = new List<INinjectModule>
{
new ConfigModule(),
new LoggingModule(),
new RepositoryModule(),
new ServiceModule()
};
kernel.Load(modules);
}
ServiceModule就是即時通訊做實際的結合:
public class ServiceModule : NinjectModule
{
public override void Load()
{
Kernel.Bind<ICoreService>().To<CoreService>();
Kernel.Bind<IAccountService>().To<AccountService>();
}
}
我的問題是,當我調試的網站,我得到一個配置錯誤:
配置錯誤
描述:處理服務此請求所需的配置文件時發生錯誤。請查看下面的具體錯誤細節並適當修改您的配置文件。
解析器錯誤消息:異常已被調用的目標拋出。
源錯誤:
Line 45: <roleManager>
Line 46: <providers>
Line 47: <clear />
Line 48: <add name="MyRoleProvider" type="Project.Web.Ui.Helpers.MyRoleProvider, Project.Web.Ui" connectionStringName="MyConnection" />
Line 49: </providers>
Line 50: </roleManager>
如果我把一個斷點在我的角色提供的構造函數:
_accountService = DependencyResolver.Current.GetService();
,我把一個斷點在我App_Start依賴注入:
var kernel = new StandardKernel();
_accountService斷點首先遇到,但如果我嘗試跨過或踩入,它只是吹向上。關於我可能做錯了什麼想法或想法找出更深入的方法?
編輯:我假設原因是因爲我的依賴注入器沒有將我的服務綁定到接口,並且DependencyResolver不知道綁定。
我猜我必須使用這個:DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));但是我該如何使用它,因爲我的內核沒有被首先創建? – 2012-07-12 17:27:18