依赖注入Structuremap ASP.NET MVC身份5 [英] Dependency Injection Structuremap ASP.NET Identity MVC 5

查看:149
本文介绍了依赖注入Structuremap ASP.NET MVC身份5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的用于身份验证的新的ASP.NET MVC 5标识框架。我用传统的StructureMap依赖注入,但我有接线它与新的AccountController的工作问题。

I'm using the new ASP.NET MVC 5 Identity framework for authentication. I've traditionally used StructureMap for dependency injection, but I'm having problems wiring it up to work with the new AccountController.

我的AccountController构造是这样的:

My AccountController constructors look like this:

    public AccountController()
        : this(new UserManager<OmpUser>(new UserStore<OmpUser>(new OmpDbContext())))
    {    

    }

    public AccountController(UserManager<OmpUser> userManager)
    {
        UserManager = userManager;
    }

我的StructureMap的配置是这样的:

My StructureMap config looks like this:

public static class IoC {
    public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });

                        //x.Register<IUserStore<OmpUser>>(() =>
                        //    new UserStore<OmpUser>(new OmpDbContext()));

                        x.For<OMPEntities>().HttpContextScoped();

                    });
        return ObjectFactory.Container;
    }
}

当我运行了项目,我得到以下错误:

When I run up the project I get the following error:

激活出错
  的AccountController,键

Activation error occured while trying to get instance of type AccountController, key ""

如何了新的建设注入的UserManager对象中的任何想法?我已经试过周围寻找,但找不到太多指导在那里。

Any ideas of how to new up a UserManager object for construction injection? I've tried searching around but can't find much guidance out there.

推荐答案

添加以下code到Container初始化方法。

Add following code to your Container initialize method.

x.For<Microsoft.AspNet.Identity.IUserStore<ApplicationUser>>()
.Use<Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>>();

x.For<System.Data.Entity.DbContext>().Use(() => new ApplicationDbContext());

这篇关于依赖注入Structuremap ASP.NET MVC身份5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆