![](/img/trans.png)
[英]Debugger stops after async HttpClient.GetAsync() call in visual studio
[英]Visual Studio breakpoint inside HttpClient.GetAsync call not getting hit
我正在尝试从.NET Web服务中的测试脚本调试异步调用,但是异步调用中的断点永远不会发生。 我什至尝试将Debugger.Break()放入其中。 下面是调用代码...
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["BaseAddress"]);
string uri = "/api/Rd_Regions";
// Below is the line of code I want to step into, but it won't step into the 'client.GetAsync(uri)'...
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
// Convert the result into business object
// Do stuff...
}
else do other stuff...
此处应该调用断点所在的Web服务部分,首先是Web api的上下文,然后是被调用的方法。 如果它停在任何一个地方,我都会很高兴...
public partial class PIMSContext : DbContext
{
public PIMSContext()
: base(new OracleConnection(Security.ConfigurationReader.GetAppSetting("PIMS")), true)
//: base(new OracleConnection(ConfigurationManager.ConnectionStrings["PIMS"].ConnectionString), true)
etc....
这是最终被称为的方法:
// GET: api/RD_REGIONS
public IQueryable<RD_REGIONS> GetRD_REGIONS()
{
// I want the debugger to stop here!
Debugger.Break();
return db.RD_REGIONS;
}
我想念什么吗? 不可能进入此异步调用吗? 任何见解均表示赞赏。
忘记了更早地使用答案进行更新-事实证明我不小心在发布模式(VS2015)中进行调试。 切换到“调试”模式可修复该问题-所有断点均按预期方式运行。
如果我正确地理解了您-上面列出的所有断点都没有命中? 不在等待客户端中。GetAsync(uri); 都不在Web服务GetRD_REGIONS()中? 没有到达client.GetAsync之后的代码?
如果是这样-也许这种方法永远不会完成?
await client.GetAsync(uri);
很有可能您在这个地方遇到了例外。 尝试用try / catch包围GetAsync方法,并将断点放置在catch块内。 像这样:
...
HttpResponseMessage response = null;
try {
response = await client.GetAsync(uri);
}
catch (Exception e) {
throw e; //breakpoint goes here
}
...
有时,由于您的方法是异步的,因此未调试的异常无法通过调试器或事件日志进行全局注册。 您只能使用try / catch获得此异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.