asp.net-mvc – IoC Castle Windsor – 没有为此对象定义的无参数构造函数

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – IoC Castle Windsor – 没有为此对象定义的无参数构造函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用Castle Windsor通过(DI / IoC)模式相应地注册控制器及其依赖关系时,我在控制器上获得了“为此对象定义的无参数构造函数”.有人可以看看下面的内容,看看我的错误,因为我看不到它.

在global.asax上注册代码

public class MyApplication : System.Web.HttpApplication
{ 
    public MvcApplication()
    {
        this.container = new WindsorContainer().Install(new DependencyInstaller());
    }
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),new WindsorActivator(this.container));
    }
}

我的DI

public class DependencyInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container,IConfigurationStore store)
    {
        container.Register(
            Component.For<IDatabaseFactory>().ImplementedBy<DatabaseFactory>().LifeStyle.PerWebRequest,Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().LifeStyle.PerWebRequest,Component.For<IMappingEngine>().ImplementedBy<MappingEngine>().UsingFactoryMethod(() => Mapper.Engine).LifeStyle.Singleton,Component.For<IFirmTasks>().ImplementedBy<FirmTasks>().LifeStyle.PerWebRequest,Classes.FromAssemblyContaining<PersonController>().BasedOn<IController>().LifestyleTransient(),Classes.FromThisAssembly().BasedOn<IHttpController>().LifestyleTransient(),Classes.FromAssemblyNamed("Core.Firm.Tasks").Where(type => type.Name.EndsWith("Tasks")).WithServiceAllInterfaces().LifestylePerWebRequest(),Classes.FromAssemblyNamed("Core.Firm.Repository")
                .Where(type => type.Name.EndsWith("Repository")).WithServiceAllInterfaces().LifestylePerWebRequest()
            );
    }
}

我的控制器

public class PersonController : Controller
{
    private IFirmTasks tasks;

    public PersonController(IFirmTasks tasks)
    {
        this.tasks = tasks;
    }

当我加载应用程序时,容器加载正常,所有依赖项按照调试时的屏幕截图解析.

那里的任何人都知道我的失败所在.提前致谢.

解决方法

When I load the application the container loads fine with all dependencies resolved as per screen shot on debug.

是的,您的所有类型都已注册.

如果你写var controller = container.Resolve< PersonController>();在Global.asax中,您应该获得控制器的实例.

您的实现对Asp.Net WebApi有效,它仅适用于System.Web.Http.ApiControllers.

您的代码无法正常工作,因为您的PersonController继承了System.Web.Mvc.Controller并使用了System.Web.Mvc.DefaultControllerFactory.你可以在第一张图片中看到它.

您应该在Global.asax中编写和注入自定义ControllerFactory,以便创建Asp.Net MVC控制器.

//Set the controller builder to use our custom controller factory
    var controllerFactory = new WindsorControllerFactory(container);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);

你可以阅读more in this answer on another SO question.

这是WindsorControllerFactory的默认实现:

/// <summary>
/// Controller factory the class is to be used to eliminate hard-coded dependencies 
/// between controllers and other components
/// </summary>
public class ControllerFactory : DefaultControllerFactory
{
    private readonly IWindsorContainer container;

    public WindsorControllerFactory(IWindsorContainer container)
    {
        this.container = container;
    }

    public override void ReleaseController(IController controller)
    {
        container.Release(controller.GetType());
    }

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)
    {
        return (IController)container.Resolve(controllerType);
    }
}

猜你在找的asp.Net相关文章