Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:02:38+00:00 2026-06-13T09:02:38+00:00

I am trying to set up Castle Windsor with ASP.NET WebAPI. I am also

  • 0

I am trying to set up Castle Windsor with ASP.NET WebAPI.

I am also using the Hyprlinkr package (https://github.com/ploeh/Hyprlinkr) and so need an instance of HttpRequestMessage injected in to one of the dependencies of my controller.

I am following this article by Mark Seemann – http://blog.ploeh.dk/2012/04/19/WiringHttpControllerContextWithCastleWindsor.aspx , but I am finding that although the API runs, when I make a call to it, the request just hangs. No error message. It’s as if it’s in an infinite loop. It’s hanging on the call to Resolve in my Custom ControllerActivator

I am thinking I have some of my Castle registrations wrong. If I remove the ones mentioned in the article above then I can successfully make a call to the API (albeit without the dependacies I need getting resolved)

Any ideas?

Code is Below

//Global.asax
public class WebApiApplication : HttpApplication
{
    private readonly IWindsorContainer container;

    public WebApiApplication()
    {
        container = 
            new WindsorContainer(
                new DefaultKernel(
                    new InlineDependenciesPropagatingDependencyResolver(), 
                    new DefaultProxyFactory()), 
                new DefaultComponentInstaller());

        container.Install(new DependencyInstaller());
    }

    protected void Application_Start()
    {        
        GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(this.container));
    }

// installer
public class DependencyInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>();

        container.Register(
            Component.For<ValuesController>()
                .Named("ValuesController")
                .LifeStyle.PerWebRequest,

            Component.For<IResourceLinker>()
                .ImplementedBy<RouteLinker>()
                .LifeStyle.PerWebRequest,

            Component.For<IResourceModelBuilder>()
                .ImplementedBy<ResourceModelBuilder>()
                .LifeStyle.PerWebRequest,

                Component.For<HttpRequestMessage>()
                .Named("HttpRequestMessage")
                .LifeStyle.PerWebRequest
            );
    }
}

//Activator

public class WindsorCompositionRoot : IHttpControllerActivator
{
    private readonly IWindsorContainer container;

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

    public IHttpController Create(
        HttpRequestMessage request,
        HttpControllerDescriptor controllerDescriptor,
        Type controllerType)
    {
        var controller = (IHttpController)this.container.Resolve(controllerType, new { request = request });

        request.RegisterForDispose(
            new Release(
                () => this.container.Release(controller)));

        return controller;
    }

// DependencyResolver   
public class InlineDependenciesPropagatingDependencyResolver : DefaultDependencyResolver
{
    protected override CreationContext RebuildContextForParameter(CreationContext current, Type parameterType)
    {
        if (parameterType.ContainsGenericParameters)
        {
            return current;
        }

        return new CreationContext(parameterType, current, true);
    }
}

EDIT***********
ADDITIONAL INFO****************

So I set up a scenario where the controller just takes a HttpRequestMessage as a ctor argument and found :

This works:

//controller
public class ValuesController : ApiController
    {
        private readonly HttpRequestMessage _httpReq;

        public ValuesController(HttpRequestMessage httpReq)
        {
            _httpReq = httpReq;
        }
//IHttpControllerActivator
public IHttpController Create(
            HttpRequestMessage httpRequest,
            HttpControllerDescriptor controllerDescriptor,
            Type controllerType)
        {

            var controller = (IHttpController)this.container.Resolve(
                controllerType, new { httpReq = httpRequest });

            return controller;

However, this Doesn’t.

//controller
public class ValuesController : ApiController
    {
        private readonly HttpRequestMessage _httpReq;

        public ValuesController(HttpRequestMessage request)
        {
            _httpReq = request;
        }

//IHttpControllerActivator
public IHttpController Create(
            HttpRequestMessage request,
            HttpControllerDescriptor controllerDescriptor,
            Type controllerType)
        {

            var controller = (IHttpController)this.container.Resolve(
                controllerType, new { request = request });

            return controller;

i.e. when the anon object has a property called “request” and the controller ctor arg is called “request”. It is somehow making the controller think it’s request property is null. Which is what causes the error I see:

Cannot reuse an ‘ApiController’ instance. ‘ApiController’ has to be
constructed per incoming message. Check your custom
‘IHttpControllerActivator’ and make sure that it will not manufacture
the same instance.

at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext
controllerContext, CancellationToken cancellationToken) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage
request, CancellationToken cancellationToken)

have a read of this
How can I enrich object composition in StructureMap without invoking setter injection?

It explains a similar scenario.

Of course, hyprlinkr has it’s ctor arg for HttpRequestMessage called “request”, so I do need to specify the anon object with that property name.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:02:40+00:00Added an answer on June 13, 2026 at 9:02 am

    Here’s a Composition Root that works for me:

    public class WindsorCompositionRoot : IHttpControllerActivator
    {
        private readonly IWindsorContainer container;
    
        public WindsorCompositionRoot(IWindsorContainer container)
        {
            this.container = container;
        }
    
        public IHttpController Create(
            HttpRequestMessage request,
            HttpControllerDescriptor controllerDescriptor,
            Type controllerType)
        {
            var controller = (IHttpController)this.container.Resolve(
                controllerType,
                new
                {
                    request = request
                });
    
            request.RegisterForDispose(
                new Release(
                    () => this.container.Release(controller)));
            return controller;
        }
    
        private class Release : IDisposable
        {
            private readonly Action release;
    
            public Release(Action release)
            {
                this.release = release;
            }
    
            public void Dispose()
            {
                this.release();
            }
        }
    }
    

    Here’s how I create the container:

    this.container =
        new WindsorContainer(
            new DefaultKernel(
                new InlineDependenciesPropagatingDependencyResolver(),
                new DefaultProxyFactory()),
            new DefaultComponentInstaller())
            .Install(new MyWindsorInstaller());
    

    and here’s the InlineDependenciesPropagatingDependencyResolver:

    public class InlineDependenciesPropagatingDependencyResolver : 
        DefaultDependencyResolver
    {
        protected override CreationContext RebuildContextForParameter(
            CreationContext current,
            Type parameterType)
        {
            if (parameterType.ContainsGenericParameters)
            {
                return current;
            }
    
            return new CreationContext(parameterType, current, true);
        }
    }
    

    Finally, here’s how I register RouteLinker:

    container.Register(Component
        .For<RouteLinker, IResourceLinker>()
        .LifestyleTransient());
    

    One thing to be aware of is that the ApiController base class has a public property named Request of the HttpRequestMessage type. As explained in section 10.4.3 of my book Windsor will attempt to assign a value to each writeable property if it has a matching component – and that match is case-insensitive.

    When you pass an HttpRequestMessage named request to the Resolve method, this is exactly what happens, so you need to tell Castle Windsor that it should forego Property Injection for ApiControllers. Here’s how I don that in a convention-based registration:

    container.Register(Classes
        .FromThisAssembly()
        .BasedOn<IHttpController>()
        .ConfigureFor<ApiController>(c => c.Properties(pi => false))
        .LifestyleTransient());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to set up an extension method in .Net 3.0 using generics and I
I'm trying to use Castle Windsor to create my message handlers because just using
I'm trying to set up some Moq repositories to test my service with Castle
I'm trying to implement Steven Sanderson's WinsorControllerFactory from his pro Asp.Net MVC Framework book
I'm trying to remove some logging dependencies and stumbled across Castle Windsor's logging facility.
I'm trying to configure Castle Windsor I have a IFileReader interface implemented by FileReader,
I'm trying to get log4net integration for Castle Windsor working. I wrote my class
I have written the following simple test in trying to learn Castle Windsor's Fluent
I'm trying set up an execution account following the steps here: http://msdn.microsoft.com/en-us/library/ms156302.aspx I follow
Trying to set up a new site using MVC RC4 web API in Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.