StructureMap错误:没有为PluginFamily定义默认实例

时间:2011-09-30 11:52:38

标签: c# asp.net-mvc asp.net-mvc-3 dependency-injection structuremap

结构图非常新。试图弄清楚它是如何工作的,我怎样才能从中受益。

到目前为止,我已经得到了这个......

的Global.asax.cs:

IContainer container = new Container(x =>
    {
         x.For<IControllerActivator>().Use
              <StructureMapControllerActivator>();
         x.For<IUserRepo>().Use<UserRepo>();
    }); 

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

StructureMapControllerActivator:

public class StructureMapControllerActivator : IControllerActivator
{
    private readonly IContainer _container;

    public StructureMapControllerActivator(IContainer container )
    {
        this._container = container;
    }

    public IController Create(RequestContext requestContext, Type controllerType)
    {
        return _container.GetInstance(controllerType) as IController;
    }
}

StructreMapDependencyResolver:

private readonly IContainer _container;

    public StructureMapDependencyResolver(IContainer container )
    {
        this._container = container;
    }

    public object GetService(Type serviceType)
    {
        object instance = _container.TryGetInstance(serviceType);
        if(instance == null && !serviceType.IsAbstract)
        {
            _container.Configure(c => c.AddType(serviceType,serviceType));
            instance = _container.TryGetInstance(serviceType);
        }
        return instance;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.GetAllInstances(serviceType).Cast<object>();
    }
}

我的AccountController:

public class AccountController : Controller
{
    private readonly IUserRepo _userRepo;

    private AccountController()
    {
        _userRepo = ObjectFactory.GetInstance<IUserRepo>();
    }

    public ActionResult Login()
    {
        return View();
    }
}

错误代码&amp;说明

  

StructureMap异常代码:202   没有为PluginFamily MBP_Blog.Controllers.AccountController定义默认实例   MBP-Blog,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null

我有一个接口名称:IUserRepo和一个存储库名称:UserRepo

请在我尝试google时提供帮助,但dint在前3页内找到任何解决方案。

使用@ Martin代码后的新错误:

  

StructureMap例外代码:180   StructureMap无法构造类MBP_Blog.Controllers.AccountController,MBP-Blog,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null的对象,因为找不到公共构造函数。

1 个答案:

答案 0 :(得分:3)

取出StructureMapControllerActivator,我觉得你不需要它。如果保留,则需要为AccountController添加映射。

此外,使用控制器注入,它将使单元测试更容易:

public class AccountController : Controller
{
    private readonly IUserRepo _userRepo;

    public AccountController(IUserRepo userRepo)
    {
        _userRepo = userRepo;
    }

    public ActionResult Login()
    {
        return View();
    }
}

同样,对于您的Container,您可以默认映射。这会自动将IService映射到Service

IContainer container = new Container(
            x =>
                {
                    x.Scan(scan =>
                               {
                                   scan.Assembly("MBP_Blog");
                                   scan.Assembly("MBP_Blog.Data");
                                   scan.WithDefaultConventions();
                               });
                });