Webapi 2.0 - Apicontrollers in another Assembly do not suffer mapping if runAllManagedModulesForAllRequests="false"

Asked

Viewed 86 times

1

I have an app Webapi2 that implements some Apicontrollers.

This same application references a Assembly that implements some more.

All of these controllers utilize Attribute Routing, as in the following example:

[RoutePrefix("sample1.endpoint")]
public class SampleController : ApiController
{
    [Route("")]
    [HttpGet]
    public HttpResponseMessage WebApiTest()

Define runAllManagedModulesForAllRequests="true" in the web.config, the application works perfectly - but I want to turn off this attribute (which can be quite costly in production).

However, if I adjust the value of runAllManagedModulesForAllRequests for false, only local Apicontrollers are correctly mapped; calls to others generate one 404.

Which part, probably obvious, I didn’t implement?

(cross-post: https://stackoverflow.com/questions/29376376/webapi-2-0-apicontrollers-in-another-assembly-fail-to-map-if-runallmanagedmodu)

1 answer

2

According to the answer to this question in the SOEN, the problem is that the module is not being loaded. For this just call any method of the module to be able to ensure that it was loaded, before calling the MapHttpAttributeRoutes.

The answer suggests that you create the following method in your external library:

public static class MyApiConfig {
  public static void Register(HttpConfiguration config) {
      config.MapHttpAttributeRoutes();
  }
}

And call this method to register the module.

You will have problems if there are several modules, because there the method MapHttpAttributeRoutes would be called several times.

If this is the case, you can create a method that receives nothing, called LoadModule, whose only operation will be something that the compiler cannot resolve to delete the method:

public static class MyApiConfig {
  public static Type LoadModule() {
      return typeof(MyApiConfig);
  }
}

You must call this method, from each module, before calling the MapHttpAttributeRoutes.

  • It seems to me to be the way. Testing right now, thank you for the prompt response!

  • Let me know if it works.

  • @Onosendai You still need help with this?

  • Sorry to keep you waiting. The initial implementation didn’t work, but I suspect the controllers are being loaded in the Owin context (used by a parallel Signalr implementation). As there is still a chance your answer is correct, I would like to test this scenario.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.