我使用ninject作为我的IoC,并编写了一个角色提供程序,如下所示:
public class BasicRoleProvider : RoleProvider
{
private IAuthenticationService authenticationService;
public BasicRoleProvider(IAuthenticationService authenticationService)
{
if (authenticationService == null) throw new ArgumentNullException("authenticationService");
this.authenticationService = authenticationService;
}
/* Other methods here */
}我读到Provider类在inject注入实例之前被实例化。我该如何解决这个问题呢?
Bind<RoleProvider>().To<BasicRoleProvider>().InRequestScope();If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(MemberShip.Provider) - this will assign all dependencies to your properties.
我不明白这一点。
发布于 2011-01-11 02:50:18
我相信ASP.NET框架的这方面在很大程度上是由配置驱动的。
对于你的最后一条评论,他们的意思是,你可以使用setter注入,而不是依赖于构造函数注入(在创建组件时发生),例如:
public class BasicRoleProvider : RoleProvider
{
public BasicRoleProvider() { }
[Inject]
public IMyService { get; set; }
}它会自动将已注册类型的实例注入到属性中。然后,您可以从应用程序中进行调用:
public void Application_Start(object sender, EventArgs e)
{
var kernel = // create kernel instance.
kernel.Inject(Roles.Provider);
}假设您已经在配置中注册了角色提供程序。
https://stackoverflow.com/questions/4650155
复制相似问题