0
votes

This is sort of a Helleresque (Catch-22) situation; or perhaps more appropriately "unrepositoried if I do, and unrepositoried if I don't"

With this Castle Windsor Controller Factory:

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel _kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        _kernel = kernel; 
        //According to http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx, might need this:
        //_kernel.AddFacility<TypedFactoryFacility>(); // This breaks ("Typed factory facility already registered"), so commenting it out; see http://stackoverflow.com/questions/20914635/how-do-i-connect-the-various-pieces-of-my-web-api-castle-windsor-di-code
        //but that requires uncommenting the "TypedFactory" using above.
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
        }
        return (IController)_kernel.Resolve(controllerType);
    }

    public override void ReleaseController(IController controller)
    {
        _kernel.ReleaseComponent(controller);
    }

}

If this line is live:

_kernel.AddFacility<TypedFactoryFacility>();

...I get this runtime exception: "TypedFactoryFacility' has already been registered with the container"

...If I comment that line out, I get this runtime exception:

"No component for supporting the service HandheldServer.Controllers.HomeController was found"

...on this line:

return (IController)_kernel.Resolve(controllerType);

Why is HomeController getting referenced, and how do I support it (assuming I should)?

HomeController is a default class that looks like this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}

Should I get rid of HomeController? Or...???

UPDATE

I changed HomeController from this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}

...to this:

public class HomeController : ApiController
{
}

And now the runtime exception I get here:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    if (controllerType == null) // <-- now controllerType is null
    {
        throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
    }
    return (IController)_kernel.Resolve(controllerType); 
}

...is: "System.Web.HttpException was unhandled by user code HResult=-2147467259 Message=The controller for path '/' could not be found. Source=HandheldServer ErrorCode=-2147467259 WebEventCode=0 StackTrace: at HandheldServer.DIPlumbing.WindsorControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in c:\HandheldServer\HandheldServer\DIPlumbing\WindsorControllerFactory.cs:line 36 at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()"

1
Do you have any other castle windsor config file. I think the issue will be found elsewhere.Ashley Medway
Looks like your HomeController type is not registered with DI container. How to register depends on way you configure container...Alexei Levenkov
I don't want the HomeController, if it's not needed; it's just something Web API added by itself; how can I get rid of it?B. Clay Shannon

1 Answers

1
votes

Check that your routing is not referencing the home controller.

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });