如何使用 Ninject 注入 asp.net (mvc2) 自定义成员资格提供程序?
好的,我已经为此工作了几个小时。我在这里找到了一些帖子,但没有任何内容能够真正解决问题。那么,让我再试一次...
我有一个使用 Ninject 的 MVC2 应用程序和一个自定义会员资格提供程序。
如果我尝试使用构造函数注入提供程序,则会收到错误:“没有为此对象定义无参数构造函数。”
public class MyMembershipProvider : MembershipProvider
{
IMyRepository _repository;
public MyMembershipProvider(IMyRepository repository)
{
_repository = repository;
}
我也一直在尝试工厂和Initialize(),但一切都是空白。
有什么想法/例子吗?
OK, so I've been working on this for hours. I've found a couple of posts here, but nothing that actually resolves the problem. So, let me try it again...
I have an MVC2 app using Ninject and a custom membership provider.
If I try and inject the provider using the ctor, I get an error: 'No parameterless constructor defined for this object.'
public class MyMembershipProvider : MembershipProvider
{
IMyRepository _repository;
public MyMembershipProvider(IMyRepository repository)
{
_repository = repository;
}
I've also been playing around with factories and Initialize(), but everything is coming up blanks.
Any thoughts/examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
成员资格提供程序模型只能在具有默认构造函数时实例化已配置的提供程序。您可以使用服务定位器模式来尝试此操作,而不是使用依赖项注入。例子:
The Membership provider model can only instantiate a configured provider when it has a default constructor. You might try this using the Service Locator pattern, instead of using Dependency Injection. Example:
这就是我能够做到这一点的方法:
1)我为 Ninject 创建了一个静态帮助器类
2)这是我的成员资格覆盖:
使用这种方法,何时实例化成员资格提供程序并不重要。
This is how I was able to do this:
1) I created a static helper class for Ninject
2) Here is my Membership override:
With this approach it doesn't really matter when the Membership provider is instantiated.
我在书中的同一位置遇到了同样的问题。直到本书的后面部分,我才注意到有两个单独的 web.config 文件。我最初将 connectionString 密钥放置在错误的 web.config 文件中。直到我将 connectionString 放入正确的 web.config 文件中,“无参数构造函数”错误才消失。
I had the same problem at the exact same spot in the book. It wasn't until later on in the book that I noticed there were two separate web.config files. I initially placed my connectionString key in the wrong web.config file. It wasn't until I placed the connectionString in the correct web.config file that the 'no parameterless constructor' error went away.