简体   繁体   English

ASP.NET Web API - HttpClient.PostAsync 本地 API 不会进入代码

[英]ASP.NET Web API - HttpClient.PostAsync local API does not step into code

So here's my situation...所以这是我的情况...

I have a unit test project wherein I instantiated a Web API controller.我有一个单元测试项目,我在其中实例化了一个 Web API 控制器。 Both the unit test project and the ASP.NET Web API project are in the same solution.单元测试项目和 ASP.NET Web API 项目都在同一个解决方案中。 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. Web API 控制器的实现包括将 HttpClient.PostAsync 调用到另一个解决方案中并部署到本地 IIS 的另一个 Web API 的部分。

The project of the caller method and the project that is deployed to IIS are both opened in Visual Studio (2 VS windows opened).调用方方法的项目和部署到 IIS 的项目都在 Visual Studio 中打开(打开了 2 个 VS 窗口)。 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.我已经将部署到 IIS 的解决方案中的 pdb 和所有内容复制到单元测试项目的 bin/debug 文件夹中。

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.但是每次控件转到 PostAsync 调用时,当我按 F11 时,它都不会进入在另一个 VS 编辑器中打开的代码。

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另一个解决方案中的控制器,部署在 IIS 中

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

You cannot share debugging instances in Visual Studio between 2 solutions when the other solution is not in debug mode.当另一个解决方案未处于调试模式时,您无法在两个解决方案之间共享 Visual Studio 中的调试实例。

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() .在 Visual Studio 本身中运行另一个解决方案(在一个单独的实例中,在不同的端口上)并在请求应该命中的地方放置一个断点 - Get()

Make sure the API is being run in debug mode and not release mode for any breakpoints to be hit.确保 API 在调试模式下运行,而不是在任何要命中的断点的发布模式下运行。

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.来自解决方案 1 的请求应该会在解决方案 2 中的控制器中命中断点,并允许您根据需要进入请求。

PS you don't need to deal with PDB files for this - your issue is how you're running the second solution/API. PS 您不需要为此处理 PDB 文件 - 您的问题是您如何运行第二个解决方案/API。

The problem is that you are calling the API using Post, but your Api is accepting only Get问题是您使用 Post 调用 API,但您的 Api 只接受 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.您可能必须更改 client.GetAsync 或者您可以使用 post 但更改 api。

and when you use async you have to use await or Result, but await is preferable当您使用 async 时,您必须使用 await 或 Result,但最好使用 await

 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()

UPDATE更新

you have updated your question, but after this it looks even more strange你已经更新了你的问题,但在这之后它看起来更奇怪

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

for getdata without any input parameters and null request body you select HttpPost.对于没有任何输入参数和空请求正文的 getdata,您选择 HttpPost。

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

It will not be even compiled, since post needs content.它甚至不会被编译,因为帖子需要内容。

I am wondering what is API controller like.我想知道 API 控制器是什么样的。 Does it have an attribute route too?它也有属性路由吗? Or all your code is just a fake?或者你所有的代码都是假的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM