How to debug/unit test webAPi in one solution

前端 未结 7 1842
借酒劲吻你
借酒劲吻你 2021-02-12 22:17

Is there a way to unit test or debug a web api in one vs solution? I am consuming the WebAPI using HttpClient and have two instances of VS up to do this.

in 1 VS instan

7条回答
  •  囚心锁ツ
    2021-02-12 22:39

    You can self host the web api as mike mentioned,

    var config = new HttpSelfHostConfiguration("http://localhost:8080");
    
    config.Routes.MapHttpRoute(
        "API Default", "api/{controller}/{id}", 
        new { id = RouteParameter.Optional });
    
    using (HttpSelfHostServer server = new HttpSelfHostServer(config))
    {
        server.OpenAsync().Wait();
        Console.WriteLine("Press Enter to quit.");
        Console.ReadLine();
    }
    

    for more details, http://www.asp.net/web-api/overview/older-versions/self-host-a-web-api

    you could start the hosting when you initialize your unit test suite, and shutdown when you cleanup the test suite.

提交回复
热议问题