简体   繁体   中英

Visual Studio breakpoint inside HttpClient.GetAsync call not getting hit

I'm trying to debug an asynchronous call from a test script inside my .NET webservice, but the breakpoints inside my async call are never getting hit. I even tried putting a Debugger.Break() inside of it. Below is the calling code...

        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...

and the part of the webservice that should be getting called where the breakpoints are is here, the first is the context of the web api, followed by the method being called. I'd be happy if it stopped in either...

public partial class PIMSContext : DbContext
{

    public PIMSContext()

    : base(new OracleConnection(Security.ConfigurationReader.GetAppSetting("PIMS")), true)
    //: base(new OracleConnection(ConfigurationManager.ConnectionStrings["PIMS"].ConnectionString), true) 
etc....

And here is the method that is ultimately called:

    // GET: api/RD_REGIONS
    public IQueryable<RD_REGIONS> GetRD_REGIONS()
    {
        // I want the debugger to stop here!
        Debugger.Break();
        return db.RD_REGIONS;
    }

Am I missing something? Is it not possible to step into this asynchronous call? Any insight is appreciated.

Forgot to update with the answer earlier - it turns out I was accidentally debugging in Release mode (VS2015). Switching to Debug mode fixed it - all breakpoints started behaving as expected.

If i have understood you correctly - all breakpoints listed above are not getting hit? Not in await client.GetAsync(uri); neither in web-service GetRD_REGIONS() ? Is the code following after client.GetAsync not reached?

If it's true - maybe this method is never completed?

await client.GetAsync(uri);

Than it's possible that you are getting an exception in this place. Try to surround your GetAsync method with try/catch and place the breakpoint inside the catch block. Something like this:

...
HttpResponseMessage response = null;
try {
  response = await client.GetAsync(uri);
} 
catch (Exception e) {
  throw e; //breakpoint goes here
}
...

Sometimes, because of your method is asynchronous, unhandled exceptions can't be registered globally by debugger or event-log. You can get this exception only with try/catch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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