Thanks to Critiano's post and some searching on-line I managed to get it to work here's the code for anyone else having this issue. I've fgot it working with MVC3 and ASP.NET Web Api Beta but I think the same solution should work for MVC4.
Firstly I created a WindsorHttpControllerFactory as the ApiControllers use a different factory than the MVC ones.
public class WindsorHttpControllerFactory : IHttpControllerFactory
{
private readonly IKernel kernel;
private readonly HttpConfiguration configuration;
public WindsorHttpControllerFactory(IKernel kernel, HttpConfiguration configuration)
{
this.kernel = kernel;
this.configuration = configuration;
}
public IHttpController CreateController(HttpControllerContext controllerContext, string controllerName)
{
if (controllerName == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", controllerContext.Request.RequestUri.AbsolutePath));
}
var controller = kernel.Resolve<IHttpController>(controllerName);
controllerContext.Controller = controller;
controllerContext.ControllerDescriptor = new HttpControllerDescriptor(configuration, controllerName, controller.GetType());
return controllerContext.Controller;
}
public void ReleaseController(IHttpController controller)
{
kernel.ReleaseComponent(controller);
}
}
The tricky part was registration it seems to involved registering a whole bunch of other stuff. This is what I ended up with.
container.Register(Component.For<IHttpControllerFactory>().ImplementedBy<WindsorHttpControllerFactory>().LifeStyle.Singleton);
container.Register(Component.For<System.Web.Http.Common.ILogger>().ImplementedBy<MyLogger>().LifeStyle.Singleton);
container.Register(Component.For<IFormatterSelector>().ImplementedBy<FormatterSelector>().LifeStyle.Singleton);
container.Register(Component.For<IHttpControllerActivator>().ImplementedBy<DefaultHttpControllerActivator>().LifeStyle.Transient);
container.Register(Component.For<IHttpActionSelector>().ImplementedBy<ApiControllerActionSelector>().LifeStyle.Transient);
container.Register(Component.For<IActionValueBinder>().ImplementedBy<DefaultActionValueBinder>().LifeStyle.Transient);
container.Register(Component.For<IHttpActionInvoker>().ImplementedBy<ApiControllerActionInvoker>().LifeStyle.Transient);
container.Register(Component.For<System.Web.Http.Metadata.ModelMetadataProvider>().ImplementedBy<System.Web.Http.Metadata.Providers.CachedDataAnnotationsModelMetadataProvider>().LifeStyle.Transient);
container.Register(Component.For<HttpConfiguration>().Instance(configuration));
//Register all api controllers
container.Register(AllTypes.FromAssembly(assemblyToRegister)
.BasedOn<IHttpController>()
.Configure(registration => registration.Named(registration.ServiceType.Name.ToLower().Replace("controller", "")).LifeStyle.Transient));
//Register WindsorHttpControllerFactory with Service resolver
GlobalConfiguration.Configuration.ServiceResolver.SetService(typeof(IHttpControllerFactory), container.Resolve<IHttpControllerFactory>());
I had to create my own implementation of an ILogger you could use a stub version like bellow.
public class MyLogger : System.Web.Http.Common.ILogger
{
public void LogException(string category, TraceLevel level, Exception exception)
{
// Do some logging here eg. you could use log4net
}
public void Log(string category, TraceLevel level, Func<string> messageCallback)
{
// Do some logging here eg. you could use log4net
}
}
I have been scratching my head over this today.
My solution is to change the [FromBody]
to a HttpRequestMessage
, essentially moving up the HTTP stack.
In my case I am sending data across the wire which is zipped json which is then base64'd. All this from an android app.
The original signature of my web endpoint looked like this (using [FromBody]
) :
My fix for this issue was to revert to using a HttpRequestMessage
for the signature of my endpoint.
You can then get access to the post data using this line of code:
This works and allows you access to the raw untouched post data. You don't have to mess around with fiddler putting an = sign at the beginning of your string or changing the content-type.
As an aside, I first tried to following one of the answers above which was to change the content type to: "Content-Type: application/x-www-form-urlencoded". For raw data this is bad advice because it strips out + characters.
So a base64 string that starts like this: "MQ0AAB+LCAAAAAA" ends up like this "MQ0AAB LCAAAAAA"! Not what you want.
Another benefit of using HttpRequestMessage
is that you get access to all the http headers from within your endpoint.
Best Answer
Everything you need can be found by starting from here.
If you follow the links in the post you will find all the information you need, including everything you need to register the components in the container.