I am using Microsoft.Owin.Hosting in my one of integration test project to self-host the Web API in order to test end to end functionality.
[TestMethod]
public void GetLoanApplications()
{
using (WebApp.Start<Startup>(url: url))
{
using (var client = new HttpClient())
{
// Create httpclient and send request-and-response-metadata-in-aspnet-web-api
}
}
}
I am able to self-host the web API and able to invoke the controller action. Owin requires some Startup class configuration, which is as follows:
[assembly: OwinStartup(typeof(MyService.App_Start.Startup))]
namespace MyService.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(config);
}
}
}
Here is my Web API Config method looks like:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Problem
- When I run my main application (not the test project) both the Owin startup and web API config methods are getting called.
- Let's say if I have some services or filters to be configured, it will get invoked twice.
- What I thought is IF I am running test project it should only invoke owin startup file (which it is doing right now) and when I am debugging my main app it should only call web API config register method.
Any idea on is this the way it should work or I am doing something wrong?
Application_Startinto owin start up. - Nkosi