Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 622
Ross Avatar asked Jun 11 '12 23:06

Ross


People also ask

Can Web API be self hosted?

You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API. See Use OWIN to Self-Host ASP.NET Web API 2.

Can ASP Net Web API ability to both self hosting?

Self Hosting. You can host a Web API as separate process than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework.

When running a self hosted server might need administrative privileges on the machine select Yes or no?

Well, you do not need administrative privileges to start it. You only need them if you do not have the right to use the URL. ACLURL (tool) can be used in the installer to grant the right to a user or program. Then there will be no administrative privileges required at runtime.


2 Answers

Previous posts of Praveen and Janushirsha lead me into the right direction I resume here :

// Not reliable in Release mode :
Type controllerType = typeof(ReferencedControllers.ControllerType);

So, you should replace IAssembliesResolver with :

HttpConfiguration config = new HttpConfiguration();
config.Services.Replace(typeof(IAssembliesResolver), new CustomAssembliesResolver());

Here is an example of implementation for CustomAssembliesResolver

using System.Web.Http.Dispatcher;
internal class CustomAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<System.Reflection.Assembly> GetAssemblies()
    {
        var assemblies = base.GetAssemblies();

        // Interestingly, if we push the same assembly twice in the collection,
        // an InvalidOperationException suggests that there is different 
        // controllers of the same name (I think it's a bug of WebApi 2.1).
        var customControllersAssembly = typeof(AnotherReferencedAssembly.MyValuesController).Assembly;
        if (!assemblies.Contains(customControllersAssembly))
            assemblies.Add(customControllersAssembly);

        return assemblies;
    }
}

This code can easily be adapted if third party assemblies are not referenced or if you want late assembly binding.

Hope this help.

like image 119
Eric Boumendil Avatar answered Oct 15 '22 22:10

Eric Boumendil


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.

like image 8
cypressx Avatar answered Oct 15 '22 20:10

cypressx