如何使用 Ninject 注入 asp.net (mvc2) 自定义成员资格提供程序?

发布于 2024-09-04 08:52:43 字数 443 浏览 10 评论 0原文

好的,我已经为此工作了几个小时。我在这里找到了一些帖子,但没有任何内容能够真正解决问题。那么,让我再试一次...

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜中书 2024-09-11 08:52:43

成员资格提供程序模型只能在具有默认构造函数时实例化已配置的提供程序。您可以使用服务定位器模式来尝试此操作,而不是使用依赖项注入。例子:

public class MyMembershipProvider : MembershipProvider
{
    IMyRepository _repository;

    public MyMembershipProvider()
    {
        // This example uses the Common Service Locator as IoC facade, but
        // you can change this to call NInject directly if you wish.
        _repository = ServiceLocator.Current.GetInstance<IMyRepository>;
    }

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:

public class MyMembershipProvider : MembershipProvider
{
    IMyRepository _repository;

    public MyMembershipProvider()
    {
        // This example uses the Common Service Locator as IoC facade, but
        // you can change this to call NInject directly if you wish.
        _repository = ServiceLocator.Current.GetInstance<IMyRepository>;
    }
月亮坠入山谷 2024-09-11 08:52:43

这就是我能够做到这一点的方法:

1)我为 Ninject 创建了一个静态帮助器类

public static class NinjectHelper
{
    public static readonly IKernel Kernel = new StandardKernel(new FooServices());

    private class FooServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IFooRepository>()
                .To<EntityFooRepository>()
                .WithConstructorArgument("connectionString",
                    ConfigurationManager.ConnectionStrings["FooDb"].ConnectionString);
        }
    }
}

2)这是我的成员资格覆盖:

    public class FooMembershipProvider : MembershipProvider
    {
        private IFooRepository _FooRepository;

        public FooMembershipProvider()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IFooRepository Repository 
        { 
            set
            {
                _FooRepository = value;
            }
        }
        ...

使用这种方法,何时实例化成员资格提供程序并不重要。

This is how I was able to do this:

1) I created a static helper class for Ninject

public static class NinjectHelper
{
    public static readonly IKernel Kernel = new StandardKernel(new FooServices());

    private class FooServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IFooRepository>()
                .To<EntityFooRepository>()
                .WithConstructorArgument("connectionString",
                    ConfigurationManager.ConnectionStrings["FooDb"].ConnectionString);
        }
    }
}

2) Here is my Membership override:

    public class FooMembershipProvider : MembershipProvider
    {
        private IFooRepository _FooRepository;

        public FooMembershipProvider()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IFooRepository Repository 
        { 
            set
            {
                _FooRepository = value;
            }
        }
        ...

With this approach it doesn't really matter when the Membership provider is instantiated.

心欲静而疯不止 2024-09-11 08:52:43

我在书中的同一位置遇到了同样的问题。直到本书的后面部分,我才注意到有两个单独的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文