StructureMap无法解析MVC3应用程序中的控制器实例

时间:2012-03-28 21:46:17

标签: asp.net-mvc-3 structuremap

我正在将StructureMap集成到我的MVC3应用程序中。我遇到了一个问题,我的自定义ControllerFactory在尝试创建控制器时抛出异常:

  

StructureMap异常代码:200找不到名为的实例   PluginType System.Web.Mvc.IController

的“contentpage”

这是我在容器中的内容:

  

Controller(System.Web.Mvc.Controller)
  Scoped as:Transient

     

AuctionCMS.Web.Controllers.HomeController,AuctionCMS.Web,   版本= 1.0.0.0,文化=中立,

     

AuctionCMS.Web.Controllers.ContentPageController,AuctionCMS.Web,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null已配置   AuctionCMS.Web.Controllers.ContentPageController的实例,   AuctionCMS.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null

我注意到控制器工厂正在请求“contentpage”而不是“contentpagecontroller”。这是正确的行为吗?出了什么问题?

这是我的代码:

    private void InitStructureMap()
    {
        var container = new StructureMap.Container();

        DependencyResolver.SetResolver(new StructureMapContainer(container));
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));

        PerformRuntimeDepdendencyConfiguration(container);
    }



    private void PerformRuntimeDepdendencyConfiguration(IContainer container)
    {
        container.Configure(x => x.Scan(y =>
        {
            y.TheCallingAssembly();
            y.WithDefaultConventions();
            y.LookForRegistries();
            y.AddAllTypesOf<Controller>();
        }));
    }



public class StructureMapContainer : IDependencyResolver
{
    static IContainer _container;

    public StructureMapContainer(IContainer container)
    {
        _container = container;
    }

    public object GetService(Type serviceType)
    {
        if (serviceType.IsAbstract || serviceType.IsInterface)
        {
            return _container.TryGetInstance(serviceType);
        }
        else
        {
            return _container.GetInstance(serviceType);
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.GetAllInstances<object>().Where(s => s.GetType() == serviceType);
    }
}


    public class StructureMapControllerFactory : IControllerFactory
    {
        private readonly IContainer _container;

        public StructureMapControllerFactory(IContainer container)
        {
            _container = container;
        }

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            System.Diagnostics.Debug.WriteLine(_container.WhatDoIHave()); 

            return _container.GetInstance<IController>(controllerName.ToLowerInvariant());
        }

        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

        public void ReleaseController(IController controller)
        {
            return;
        }
    }

public class ApplicationRegistry : Registry
{
    public ApplicationRegistry()
    {
        ... register some types ...
    }
}

1 个答案:

答案 0 :(得分:1)

与评论中提到的一样,您的控制器未按名称注册。试试这个:

container.Configure(x => x.Scan(y =>
{
    y.TheCallingAssembly();
    y.WithDefaultConventions();
    y.LookForRegistries();
    y.AddAllTypesOf<Controller>()
         .NameBy(type => type.Name.Replace("Controller", "")
         .ToLowerInvariant());
}));