Search code examples
.netasp.net-web-apiself-hosting

Self-hosting WebAPI application referencing controller from different assembly


I came across this gem, which seemed to be close to what I wanted. However, I want to use the already-written controllers from a referenced assembly.

My first crack was to reference the assembly, set up the routing rules the same as the original webAPI project and go, but I get 400s every time I try to call the self-hosted service. I've picked through the innards of the request with Fiddler, and aside from the address differences, the requests against the webAPI project and the self-hosted project are identical.

I feel like this ought to be relatively straightforward, but I haven't found an acceptable answer.


Solution

  • This seems to be a known issue. You have to force the .NET to load the assemblies with the Controllers you need.

    Before you Self Host the Web API, you should retrieve a type from the Reference Assembly which you want to be loaded by the runtime. Something like this:

    Type controllerType = typeof(ReferencedControllers.ControllerType);
    

    This should load the controllers from this assembly and it won't give you 404 error.