简体   繁体   English

为什么调试器在异步调用后没有在断点处停止?

[英]Why is debugger not stopping at breakpoint after async call?

I have two services that inherit from the same base service. 我有两个从同一个基本服务继承的服务。 Both make asynchronous API calls that I am writing tests for. 两者都进行我正在编写测试的异步API调用。 I am able to debug through the base service call for one service as expected. 我可以按预期通过基本服务调用调试一个服务。 For the second service the debugger does not stop at any of the breakpoints after the async call is made, but rather completes execution and exits. 对于第二个服务,调试器在进行异步调用后不会在任何断点处停止,而是完成执行并退出。

Here is the base service that both services inherit: 这是两个服务继承的基本服务:

namespace MyCoach.Services
{
    public enum Caching { Normal, Forced };
    public abstract class BaseService
    {
        protected readonly INetworkManager _networkManager;
        protected readonly IConfigurationService _configurationService;
        protected readonly ISettingsService _settings;
        protected readonly ICacheService _cache;

        public BaseService(INetworkManager networkManager, IConfigurationService configurationService, ISettingsService settingsService, ICacheService cacheService)
        {
            _networkManager = networkManager;
            _configurationService = configurationService;
            _settings = settingsService;
            _cache = cacheService;
        }

        protected void Execute<T>(RestRequest request, Action<T> onSuccess, Action<HttpStatusCode> onFailure) where T : class, new()
        {
            ExecuteAsync(request, onSuccess, onFailure);
        }

        protected async Task ExecuteAsync<T>(RestRequest request, Action<T> onSuccess, Action<HttpStatusCode> onFailure) where T : class
        {
            if (_networkManager.IsConnectionAvailable())
            {
                var restClient = new RestClient(new Uri(_configurationService.Configuration.MyCoachBaseUrl));
                AddImageSetParameter(request);
                try
                {
                    *IRestResponse<T> response = await restClient.Execute<T>(request);
                    if (IsSuccessfulStatusCode(response.StatusCode))
                    {
                        onSuccess(response.Data);
                    }
                    else
                    {
                        onFailure(response.StatusCode);
                    }
                }
                catch (Exception ex)
                {
                    int statusCode = Int32.Parse(ex.Message.Substring(0, 3));
                    onFailure((HttpStatusCode)Enum.ToObject(typeof(HttpStatusCode), statusCode));
                }
            }
            else
            {
                _networkManager.ShowNetworkUnavailableError();
            }
        }
    }    
}

I can put a breakpoint on the await command in the executeAsync method (marked by an *) that is hit by the debugger when I run either service. 当我运行任一服务时,我可以在executeAsync方法(由*标记)中的await命令上设置一个断点,该命令由调试器命中。 Any breakpoint within the if/else or exception handler following that call is not hit as expected when I debug the PingService. 调试PingService时,在调用之后的if / else或异常处理程序中的任何断点都未按预期命中。 Those same breakpoints are hit when I debug the HomeService, so I'm guessing there must be some issue with how I've implemented the PingService. 当我调试HomeService时会遇到那些相同的断点,所以我猜我必须在如何实现PingService时遇到一些问题。

Here is my service that works as expected: 这是我的服务按预期工作:

namespace MyCoach.Services
{
  public interface IHomeService 
  {
    void ThisWeek(Action<HomeResponse> onSuccess, Action<HttpStatusCode> onFailure);
  }

  public class HomeService : BaseService, IHomeService
  {
    public HomeService(INetworkManager networkManager, IConfigurationService configurationService, ISettingsService settingsService, ICacheService cacheService)
        : base(networkManager, configurationService, settingsService, cacheService)
    {
    }

    public void ThisWeek(Action<HomeResponse> onSuccess, Action<HttpStatusCode> onFailure)
    {
        var request = RequestBuilder.GET("/home/thisweek", version: 2)
                                    .WithAccessToken(_settings)
                                    .Build();
        Execute(request, onSuccess, onFailure);
    }
  }
}

And here's my service where I see the strange debugger behavior: 这是我的服务,我看到奇怪的调试器行为:

namespace MyCoach.Services
{
    public interface IPingService
    {
        void PingValidEndpoint(Action<PingResponse> onSuccess, Action<HttpStatusCode> onFailure);
    }

    public class PingService : BaseService, IPingService
    {
        public PingService(INetworkManager networkManager, IConfigurationService configurationService, ISettingsService settingsService, ICacheService cacheService)
            : base(networkManager, configurationService, settingsService, cacheService)
        {
        }

        public void PingValidEndpoint(Action<PingResponse> onSuccess, Action<HttpStatusCode> onFailure)
        {
            var request = RequestBuilder.GET("/ping", version: 2)
                                        .WithAccessToken(_settings)
                                        .Build();
            Execute(request, onSuccess, onFailure);
        }
    }
}

What is the difference between those two services that is causing me to see the different behavior in the debugger? 这两个服务之间的区别是什么导致我在调试器中看到不同的行为?

According to the HomeService and PingService class, they are implement the BaseService with the same way. 根据HomeService和PingService类,它们以相同的方式实现BaseService。 So the problem should be related to the way that call them. 所以问题应该与调用它们的方式有关。 Please check the code that you used to call PingService to make sure it use async and await to call the PingService. 请检查您用来调用PingService的代码,以确保它使用async并等待调用PingService。 Detailed explanation and steps, please refer to following document. 详细说明和步骤请参考以下文件。

https://msdn.microsoft.com/en-us/library/jj155813.aspx https://msdn.microsoft.com/en-us/library/jj155813.aspx

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

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