Home > Mobile >  ASP.NET Web API - HttpClient.PostAsync local API does not step into code
ASP.NET Web API - HttpClient.PostAsync local API does not step into code

Time:10-23

So here's my situation...

I have a unit test project wherein I instantiated a Web API controller. Both the unit test project and the ASP.NET Web API project are in the same solution. The Web API controllers' implementation includes one part that is to call HttpClient.PostAsync to another Web API that is in another solution and is deployed to local IIS.

The project of the caller method and the project that is deployed to IIS are both opened in Visual Studio (2 VS windows opened). I have already copied the pdb and all from the solution that is deployed to IIS to the bin/debug folder of the unit test project.

But everytime the control goes to the PostAsync call, when I pressed F11, it doesn't get into the code that is opened in another VS editor.

May I know how will I be able to achieve this?

Unit Test project:

[TestMethod]
public void TestController
{
TestController t = new TestController();
t.Get();
}

TestController:

[HttpPost]
public ActionResult Get()
{

//assume that HttpClient.BaseAddress was already set in constructor

client.PostAsync("/testapi/getdata");
}

Controller in another solution, deployed in IIS

[Route("testapi/getdata")]
[HttpPost]
public ActionResult Get()
{
//implementation here
}

CodePudding user response:

You cannot share debugging instances in Visual Studio between 2 solutions when the other solution is not in debug mode.

Run the other solution within Visual Studio itself (in a separate instance, on a different port) & place a breakpoint where the request should hit - Get().

Make sure the API is being run in debug mode and not release mode for any breakpoints to be hit.

The request from solution 1 should then hit the breakpoint in the controller in solution 2 and allow you to step into the request as needed.

P.S. you don't need to deal with PDB files for this - your issue is how you're running the second solution/API.

CodePudding user response:

The problem is that you are calling the API using Post, but your Api is accepting only Get

client.PostAsync("/testapi/getdata");

///Controller in another solution, deployed in IIS

[Route("testapi/getdata")]
[HttpGet]
public ActionResult Get()

you have to change client.GetAsync maybe or you can use post but change api.

and when you use async you have to use await or Result, but await is preferable

 awant client.GetAsync("/testapi/getdata")
//or
client.GetAsync("/testapi/getdata").Result;

///Controller in another solution, deployed in IIS

[Route("testapi/getdata")]
[HttpGet]
public ActionResult Get()

// but should be
public async Task<ActionResult> Get()
  • Related