1

我正在尝试使用 Autofac 和示例站点上的自定义用户类来配置 MRB

例如

  public class CustomUser : RelationalUserAccount
{
    [Display(Name="First Name")]
    public virtual string FirstName { get; set; }
    [Display(Name = "Last Name")]
    public virtual string LastName { get; set; }
    public virtual int? Age { get; set; }
}

public class CustomUserAccountService : UserAccountService<CustomUser>
{
    public CustomUserAccountService(CustomConfig config, CustomUserRepository repo)
        : base(config, repo)
    {
    }
}

我的回购

 public class CustomDb : MembershipRebootDbContext<CustomUserAccount>
{
    public CustomDb()
        : base("CustomDatabase")
    {
    }
}

public class CustomUserAccountRepository : DbContextUserAccountRepository<CustomDb, CustomUserAccount>
{
    public CustomUserAccountRepository(CustomDb db)
        : base(db)
    {
    }
}

我的控制器没什么特别的

UserAccountService<CustomUserAccount> _userAccountService;
    AuthenticationService<CustomUserAccount> _authService;

    public UserAccountController(
        UserAccountService<CustomUserAccount> userAccountService, AuthenticationService<CustomUserAccount> authSvc)
    {
        this._userAccountService = userAccountService;
        this._authService = authSvc;
    }

最后我的startup.cs

private static void BuildAutofacContainer(IAppBuilder app, string authType)
    {
        var builder = new ContainerBuilder();

        var config = CreateMembershipRebootConfiguration(app);

        builder.RegisterInstance(config).As<MembershipRebootConfiguration>();

        builder.RegisterType<DefaultMembershipRebootDatabase>()
            .InstancePerLifetimeScope();

        builder.RegisterType<DefaultUserAccountRepository>()
            .As<IUserAccountRepository>()
            .As<IUserAccountRepository<RelationalUserAccount>>()
            .As<IUserAccountQuery>()
          //  .As<IUserAccountRepository<CustomUserAccount>>()

            .As<IUserAccountQuery<BrockAllen.MembershipReboot.Relational.RelationalUserAccount>>()
            .InstancePerLifetimeScope();

        //builder.RegisterType<CustomUserRepository>().As<IUserAccountRepository<CustomUserAccount>>()
        //    .InstancePerLifetimeScope();//doesnt work

        //builder.RegisterType<CustomUserAccount>()
        //    .As<CustomUserRepository>()
        //    .InstancePerRequest();doesnt work

        //UserAccountService<CustomUserAccount> _userAccountService;
        //AuthenticationService<CustomUserAccount> _authService;



        builder.RegisterType<UserAccountService>().OnActivating(e =>
        {
            var owin = e.Context.Resolve<IOwinContext>();
            var debugging = false;
#if DEBUG
            debugging = true;
#endif
            e.Instance.ConfigureTwoFactorAuthenticationCookies(owin.Environment, debugging);
        })
        .AsSelf()
        .InstancePerLifetimeScope();

        builder.RegisterType<UserAccountService<RelationalUserAccount>>().OnActivating(e =>
        {
            var owin = e.Context.Resolve<IOwinContext>();
            var debugging = false;
#if DEBUG
            debugging = true;
#endif
            e.Instance.ConfigureTwoFactorAuthenticationCookies(owin.Environment, debugging);
        })
        .AsSelf()
        .InstancePerLifetimeScope();

        builder.Register(ctx =>
        {
            var owin = ctx.Resolve<IOwinContext>();
            return new OwinAuthenticationService(authType, ctx.Resolve<UserAccountService>(), owin.Environment);
        })
        .As<AuthenticationService>()
        .InstancePerLifetimeScope();

        builder.Register(ctx => HttpContext.Current.GetOwinContext()).As<IOwinContext>();
        builder.RegisterControllers(typeof(Startup).Assembly);

        var container = builder.Build();
        System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

    private static MembershipRebootConfiguration CreateMembershipRebootConfiguration(IAppBuilder app)
    {
        var config = new MembershipRebootConfiguration();
        config.RequireAccountVerification = false;
        config.AddEventHandler(new DebuggerEventHandler());

        var appInfo = new OwinApplicationInformation(
            app,
            "Test",
            "Test Email Signature",
            "/UserAccount/Login",
            "/UserAccount/ChangeEmail/Confirm/",
            "/UserAccount/Register/Cancel/",
            "/UserAccount/PasswordReset/Confirm/");

        var emailFormatter = new EmailMessageFormatter(appInfo);
        // uncomment if you want email notifications -- also update smtp settings in web.config
        config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));
        // uncomment to enable SMS notifications -- also update TwilloSmsEventHandler class below
        //config.AddEventHandler(new TwilloSmsEventHandler(appinfo));

        // uncomment to ensure proper password complexity
        //config.ConfigurePasswordComplexity();

        return config;
    }

如您所见,我尝试通过这两次尝试配置 autofac

        //builder.RegisterType<CustomUserRepository>().As<IUserAccountRepository<CustomUserAccount>>()
        //    .InstancePerLifetimeScope();//doesnt work

        //builder.RegisterType<CustomUserAccount>()
        //    .As<CustomUserRepository>()
        //    .InstancePerRequest();doesnt work

        //UserAccountService<CustomUserAccount> _userAccountService;
        //AuthenticationService<CustomUserAccount> _authService;

你能看出我是如何绑定 CustomUser 以允许我使用 autofac 的吗?

谢谢

4

0 回答 0