【问题标题】:The type IUserStore`1 does not have an accessible constructorIUserStore`1 类型没有可访问的构造函数
【发布时间】:2014-04-10 02:32:12
【问题描述】:

我想用 Unity 3 设置 MVC5 应用程序。 我从标准模板创建了一个默认的 web mvc5 应用程序,然后添加了统一

当我在 AccountController 中访问注册操作时,出现以下异常:

IUserStore`1 类型没有可访问的构造函数。

从这个帖子How to add MVC 5 authentication to Unity IoC?我知道问题是Unity选择了参数列表更长的构造函数。

解决方案是通过以下方式注册与默认构造函数一起使用的 Account 控制器:

container.RegisterType<AccountController>(new InjectionConstructor());

我想做的是将它注册到配置文件中没有在代码中 是否可以在 web.config 中做同样的事情?

最好的问候, 塞巴斯蒂安

【问题讨论】:

    标签: c# unity-container asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    您可以使用 XML 配置来配置 Unity。在您的情况下,它看起来像这样:

    <configSections>
      <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>    
    
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">    
      <container>
        <register type="MyApp.AccountController, MyApp">
          <constructor />
        </register>
       </container>
    </unity>
    

    然后你需要显式加载配置:

    IUnityContainer container = new UnityContainer();
    container.LoadConfiguration();
    

    【讨论】:

    • 正是我需要的。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多