我如何注入到库使用AutoFac一个自定义的Membership

我如何注入到库使用AutoFac一个自定义的Membership

本文介绍了我如何注入到库使用AutoFac一个自定义的MembershipProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的成员提供

I created a custom membership provider

public class MyMembership : MembershipProvider
{
    private IRepository<User> Repo;
}

我想通了如何使用autofac注入MyMembership:

I figured out how to inject MyMembership using autofac:

builder.Register(c => Membership.Provider);

不过,如果我有一个构造函数的IRepository这不起作用(它只调用参数的构造函数)。我试图做从私有字段更改为公共​​财产,并要求:

However, this doesn't work if I have a constructor that takes an IRepository (it only calls the parameterless constructor.) I tried doing changing from a private field to a public property and calling:

builder.Register(c => Membership.Provider).AutoWireProperties();

问题是,的MembershipProvider没有一个回购物业,这只是我的课。

The problem is, MembershipProvider doesn't have a Repo property, it's only my class.

有关目前,我还没有任何注射,只是创建一个空的构造,我简单地创建我的回购的新实例。但它使得它更难的测试。

For the time being, I've not injected anything, and just created an empty constructor where I simply create a new instance of my Repo. But it makes it harder for testing.

那么,有没有我可以使用AutoFac注入我的MyMembership,并用它注入回购什么办法?

So, is there any way that I can use AutoFac to inject my MyMembership, and have it use the injected Repo?

推荐答案

没有。这是不可能的。

DependencyResolver 不用于提供者(角色/成员)等。

The DependencyResolver is not used for the providers (roles/membership) etc.

我做了它使用 DependencyResolver 内部成员资格提供程序。所有你需要做的是落实 IAccountRepository ,并在容器中注册。

I've made a membership provider which uses DependencyResolver internally. All you need to do is to implement IAccountRepository and register it in your container.

这篇关于我如何注入到库使用AutoFac一个自定义的MembershipProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:38