簡體   English   中英

HttpClient.GetAsync調用中的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM