简体   繁体   English

在Visual Studio中调试WebApi对MVC应用没有响应

[英]Debugging WebApi in Visual Studio returns no response to MVC app

I have a WebApi and MVC5 app within the same solution. 我在同一解决方案中有一个WebApi和MVC5应用程序。 When I debug these the MVC5 app calls through to the WebApi fine, however it receives no response. 当我调试这些时,MVC5应用程序会调用WebApi,但是它没有收到任何响应。

Using exactly the same code and data with a Console App instead of an MVC5 app then the response comes back fine. 通过控制台应用程序而不是MVC5应用程序使用完全相同的代码和数据,然后响应恢复正常。

In the mvc app no HTTP response is ever returned. 在mvc应用程序中,永远不会返回HTTP响应。 I have debugged the code and the call comes through into the web-api. 我已经调试了代码,并且调用进入了Web-api。

The code i'm working with is similar to below, where I'm calling DoMyPost(). 我正在使用的代码类似于下面的代码,在这里我称为DoMyPost()。 No response is returned from await HttpClient.PostAsJsonAsync(url, model); HttpClient.PostAsJsonAsync(url,model);等待未返回任何响应;

    public MyModel DoMyPost(ModelIN model)
    {
        return PostAsJsonAsync("url", model).Result;
    }

    protected async Task<MyModel> PostAsJsonAsync(string url, ModelIN model)
    {
        var response = await HttpClient.PostAsJsonAsync(url, model);
        return await ReadMyModel(response);
    }

Any thoughts as to what could be going wrong? 关于可能出了什么问题的任何想法?

I'm on a windows 8 machine. 我在Windows 8机器上。

===EDIT ===编辑

I fire up both the MVC5 and web-api app I debug the MVC 5 app and see it make the call through the code I've outlined above The break point is hit within the web-api and I can step through until the response is generated. 我同时启动了MVC5和Web-api应用程序,我调试了MVC 5应用程序,并看到它通过上面概述的代码进行了调用。在Web-api中命中了断点,我可以逐步执行,直到响应为产生。
Then the MVC 5 app sits waiting and never appears to get any response. 然后,MVC 5应用程序处于等待状态,并且从未出现任何响应。

I've replace the MVC5 app with a console app, and it receives the response fine. 我已经用控制台应用程序替换了MVC5应用程序,它收到了很好的响应。

Thank you 谢谢

==THE FIX ==修复

changing the code to below fixed the issue 将代码更改为以下可解决此问题

public MyModel DoMyPost(ModelIN model)
    {
         var response = await HttpClient.PostAsJsonAsync(url, model).Result;
     return ReadMyModel(response).Result;
    }

    protected async Task<MyModel> PostAsJsonAsync(string url, ModelIN model)
    {
        var response = await HttpClient.PostAsJsonAsync(url, model);
        return await ReadMyModel(response);
    }

I am going to try and guess what's going on here. 我将尝试猜测这里发生了什么。 It looks like you are making a synchronous call to the MVC5 method, "DoMyPost", and then trying to return an Asynchronous result in the PostAsJsonAsync. 看来您正在对MVC5方法“ DoMyPost”进行同步调用,然后尝试在PostAsJsonAsync中返回异步结果。

I think you should think about exactly what you need to happen and why. 我认为您应该仔细考虑发生什么以及为什么发生。 Here is good post on using async in MVC5. 这是有关在MVC5中使用异步的好帖子

Also, here is a quick and dirty explanation of the differences between MVC5 and WebApi. 另外, 是对MVC5和WebApi之间差异的快速而肮脏的解释。 See bottom comment. 请参阅底部注释。

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

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