Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 StructureMap未解析MVC3应用程序中的控制器实例_Asp.net Mvc 3_Structuremap - Fatal编程技术网

Asp.net mvc 3 StructureMap未解析MVC3应用程序中的控制器实例

Asp.net mvc 3 StructureMap未解析MVC3应用程序中的控制器实例,asp.net-mvc-3,structuremap,Asp.net Mvc 3,Structuremap,我正在将StructureMap集成到我的MVC3应用程序中。我遇到一个问题,我的自定义ControllerFactory在尝试创建控制器时引发异常: StructureMap异常代码:200找不到名为 插件类型System.Web.Mvc.IController的“contentpage” 以下是容器中的内容: 控制器(System.Web.Mvc.Controller) 范围为:瞬态 AuctionCMS.Web.Controllers.HomeController,AuctionCMS.W

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

StructureMap异常代码:200找不到名为 插件类型System.Web.Mvc.IController的“contentpage”

以下是容器中的内容:

控制器(System.Web.Mvc.Controller)
范围为:瞬态

AuctionCMS.Web.Controllers.HomeController,AuctionCMS.Web, 版本=1.0.0.0,区域性=中性

AuctionCMS.Web.Controllers.ContentPageController,AuctionCMS.Web, 版本=1.0.0.0,区域性=中性,PublicKeyToken=空配置 AuctionCMS.Web.Controllers.ContentPageController的实例, AuctionCMS.Web,版本=1.0.0.0,区域性=中立,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 ...
    }
}
private void InitStructureMap()
{
var container=new StructureMap.container();
SetResolver(新结构映射容器(容器));
ControllerBuilder.Current.SetControllerFactory(新结构MapControllerFactory(容器));
执行卸载密度配置(容器);
}
专用void PerformRuntimeDepdencyConfiguration(IContainer容器)
{
container.Configure(x=>x.Scan(y=>
{
y、 装配件();
y、 使用默认约定();
y、 查找注册表();
y、 AddAllTypesOf();
}));
}
公共类StructureMapContainer:IDependencyResolver
{
静态IContainer_容器;
公共结构映射容器(IContainer容器)
{
_容器=容器;
}
公共对象GetService(类型serviceType)
{
if(serviceType.IsAbstract | | serviceType.IsInterface)
{
返回_container.TryGetInstance(服务类型);
}
其他的
{
return\u container.GetInstance(serviceType);
}
}
公共IEnumerable GetServices(类型serviceType)
{
返回_container.GetAllInstances(),其中(s=>s.GetType()==serviceType);
}
}
公共类结构MapControllerFactory:IControllerFactory
{
专用只读IContainer\u容器;
公共结构MapControllerFactory(IContainer容器)
{
_容器=容器;
}
公共IController CreateController(RequestContext RequestContext,string controllerName)
{
System.Diagnostics.Debug.WriteLine(_container.WhatDoIHave());
返回_container.GetInstance(controllerName.ToLowerInvariant());
}
公共会话状态行为GetControllerSessionBehavior(RequestContext RequestContext,string controllerName)
{
返回SessionStateBehavior.Default;
}
公共无效释放控制器(IController控制器)
{
返回;
}
}
公共类应用程序注册表:注册表
{
公共应用程序注册表()
{
…注册某些类型。。。
}
}

正如您在评论中指出的,您的控制器没有按名称注册。试试这个:

container.Configure(x => x.Scan(y =>
{
    y.TheCallingAssembly();
    y.WithDefaultConventions();
    y.LookForRegistries();
    y.AddAllTypesOf<Controller>()
         .NameBy(type => type.Name.Replace("Controller", "")
         .ToLowerInvariant());
}));
container.Configure(x=>x.Scan(y=>
{
y、 装配件();
y、 使用默认约定();
y、 查找注册表();
y、 AddAllTypesOf()
.NameBy(type=>type.Name.Replace(“控制器”,“”)
.ToLowerInvariant());
}));

您是否已确认通过扫描正确拾取了控制器?您可以在上面的容器转储中看到这一点。抱歉,我错过了。我在你的输出中看不到名称,但我怀疑你只需要在控制器名称后面加上“controller”——这可能是因为你正在使用的约定没有将其删除。我也尝试过,没有成功。我不认为容器配置为按名称查找实例。我认为使用nameby应该可以。ControllerFactory上还有一个方法,它采用IIRC类型,您实际上并没有进行任何特殊的命名,所以可能在这里也可以使用。