Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the In-Memory HttpServer know which WebAPI project to host?

I want to run tests against WebAPI project using a popular in-memory hosting strategy.

My tests reside in a separate project.

Here's the start of my test

    [TestMethod]
    public void TestMethod1()
    {
        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional});

        HttpServer server = new HttpServer(config);
        HttpMessageInvoker client = new HttpMessageInvoker(server)
    }

The client is initialized with the HttpServer, establishing the direct client-server connection.

Other than providing route config info, how does HttpServer know which WebAPI project to host?

How to host multiple WebAPI projects at the same time?

Seems HttpServer does some magic to locate WebAPI projects?

Thanks

like image 604
puri Avatar asked May 29 '13 13:05

puri


People also ask

What do you think about in memory hosting with web API?

While the community, forums, bloggers have been buzzing about web-host and self-host capabilities of Web API, aside from the terrific post by Pedro Felix, very little has been said about in memory hosting.

How do I configure the in-memory web API host?

The configuration of the in-memory Web API host is almost exactly the same as configuring web- or self- host. You need specify, at the very least, the routes to be used, and that is done through the familiar HttpConfiguration. Server is instantiated by passing the configuration to the HttpServer constructor.

How to host a web API application?

The Web API application can be hosted in two ways. IIS Hosting. Web API can be hosted under IIS, in the same way as a web application. You have learned to create a Web API in the previous section. As you have seen there, a Web API is created with ASP.NET MVC project by default.

How to host a web API as separate process?

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. You need to do following steps in order to self-host a web API. Use HttpConfiguration to configure a Web API.


2 Answers

Web API depends on a service called IAssembliesResolver to get all the assemblies and scans them to find controllers which implement the IHttpController interface.

Now sometimes Web API might be unable to find your controller depending on whether the assembly has been loaded into the current app domain or not. In that scenario you would need to make sure that your assembly is loaded.

Looking at your sample test code, it appears that you are not referring to any type from your Web API project in which case i assume the Web API project's assembly would not be loaded.

Also you seem to be registering the routes again in your test. I would suggest to use the WebApiConfig.Register(HttpConfiguration) of your Web API project to do all the registration stuff. This way you would be testing with the same settings that your Web API project has.

Notes:

  1. When running tests using in-memory server, your requests/responses wouldn't go through the formatters' serialization/deserialization process which is dangerous as you could be having real issues during them. So you would need to make sure to take care about this. Long time back i wrote a blog post regarding this. You can check it out here.

  2. Fiddler tool is very useful in looking at the raw requests/responses to diagnose any issues. You would be loosing this ability if you are doing in-memory testing though.

like image 189
Kiran Avatar answered Sep 21 '22 04:09

Kiran


Web Api should find all your controllers that inherit from ApiController. As long as all your controllers are in the same solution it should work just fine. I have a very similar setup that runs tests using the in-memory httpserver against controllers in another project. This gives me the ability to do very fast "integration" tests in my unit test project.

like image 34
Joshua Peterson Avatar answered Sep 22 '22 04:09

Joshua Peterson