尝试获取AccountController类型的实例时发生激活错误,键\" \""

时间:2014-03-10 10:35:54

标签: asp.net .net asp.net-mvc structuremap

那是我的AccountController

[Authorize]
public class AccountController : Controller
{
    private UserManager<User> _userManager { get; set; }

    public AccountController() : this(new UserManager<User>(new UserStore())) {}

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

堆栈跟踪:

Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
  HResult=-2146233088
  Message=Activation error occurred while trying to get instance of type AccountController, key ""
  Source=Microsoft.Practices.ServiceLocation
  StackTrace:
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 34
       at System.Web.Mvc.DependencyResolver.DelegateBasedDependencyResolver.GetService(Type type)
  InnerException: StructureMap.StructureMapException
       HResult=-2146232832
       Message=StructureMap Exception Code:  202
No Default Instance defined for PluginFamily Microsoft.AspNet.Identity.IUserStore`1[[Fables.BLL.Entities.User, Fables.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.AspNet.Identity.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
       Source=StructureMap
       ErrorCode=202
       StackTrace:
            at StructureMap.BuildSession.<.ctor>b__0(Type t) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 33
            at StructureMap.Util.Cache`2.get_Item(KEY key) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Util\Cache.cs:line 83
            at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 215
            at StructureMap.Pipeline.DefaultInstance.build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\DefaultInstance.cs:line 22
            at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 103
            at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 69
            at StructureMap.Pipeline.ConstructorInstance.Get(String propertyName, Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 74
            at StructureMap.Pipeline.ConstructorInstance.Get[T](String propertyName, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 79
            at StructureMap.Pipeline.Arguments.Get[T](String propertyName) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Arguments.cs:line 18
            at lambda_method(Closure , IArguments )
            at StructureMap.Construction.BuilderCompiler.FuncCompiler`1.<>c__DisplayClass2.<CreateBuilder>b__0(IArguments args) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Construction\BuilderCompiler.cs:line 56
            at StructureMap.Construction.InstanceBuilder.BuildInstance(IArguments args) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Construction\InstanceBuilder.cs:line 34
            at StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 237
            at StructureMap.Pipeline.ConstructorInstance.build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 218
            at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 103
            at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 69
            at StructureMap.Pipeline.ObjectBuilder.ConstructNew(Type pluginType, Instance instance, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ObjectBuilder.cs:line 39
            at StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ObjectBuilder.cs:line 27
            at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 176
            at StructureMap.BuildSession.<>c__DisplayClass3.<.ctor>b__1() in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 36
            at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 215
            at StructureMap.Pipeline.DefaultInstance.build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\DefaultInstance.cs:line 22
            at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 103
            at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 69
            at StructureMap.Pipeline.ConstructorInstance.Get(String propertyName, Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 74
            at StructureMap.Pipeline.ConstructorInstance.Get[T](String propertyName, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 79
            at StructureMap.Pipeline.Arguments.Get[T](String propertyName) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Arguments.cs:line 18
            at lambda_method(Closure , IArguments )
            at StructureMap.Construction.BuilderCompiler.FuncCompiler`1.<>c__DisplayClass2.<CreateBuilder>b__0(IArguments args) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Construction\BuilderCompiler.cs:line 56
            at StructureMap.Construction.InstanceBuilder.BuildInstance(IArguments args) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Construction\InstanceBuilder.cs:line 34
            at StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 237
            at StructureMap.Pipeline.ConstructorInstance.build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ConstructorInstance.cs:line 218
            at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 103
            at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\Instance.cs:line 69
            at StructureMap.Pipeline.ObjectBuilder.ConstructNew(Type pluginType, Instance instance, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ObjectBuilder.cs:line 39
            at StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Pipeline\ObjectBuilder.cs:line 27
            at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 176
            at StructureMap.BuildSession.<>c__DisplayClass3.<.ctor>b__1() in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 36
            at StructureMap.BuildSession.CreateInstance(Type pluginType) in c:\BuildAgent\work\767273992e840853\src\StructureMap\BuildSession.cs:line 215
            at StructureMap.Container.GetInstance(Type pluginType) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Container.cs:line 265
            at Fables.Web.DependencyResolution.StructureMapDependencyScope.DoGetInstance(Type serviceType, String key) in f:\MyProjects\Fables\Fables\Fables.Web\DependencyResolution\StructureMapDependencyScope.cs:line 123
            at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
       InnerException: 

有人可以解释一下,这个例外意味着什么,以及如何解决它? TY。

2 个答案:

答案 0 :(得分:8)

您的应用正在使用第二个构造函数,因为StructureMap 将自动选择要用于注入的类的“最贪婪”公共构造函数。您可以在StructureMap documents上检查此行为。

因此,通过查看错误,您似乎错过了在StructureMap中从IUserStore<User>UserStore类的映射。

如果希望StructureMap使用无参数构造函数,首先需要确保StructureMap正在创建AccountController实例。然后,您需要更新StructureMap注册表代码以指定应使用哪个构造函数。 (参见类似的问题here)。这也在the documentation

中进行了解释

答案 1 :(得分:6)

重要信息在这里:

Message=StructureMap Exception Code:  202 No Default Instance defined for PluginFamily Microsoft.AspNet.Identity.IUserStore`1[[Fables.BLL.Entities.User, Fables.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.AspNet.Identity.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
       Source=StructureMap
       ErrorCode=202

Structuremap不知道如何“构建”IUserStore。在您的structuremap注册表中,您需要告诉structuremap您要用于IUserStore的对象:

For<IUserStore>().Use<MyUserStore>();