我有两个从同一个基本服务继承的服务。两者都进行我正在编写测试的异步API调用。我可以按预期通过基本服务调用调试一个服务。对于第二个服务,调试器在进行异步调用后不会在任何断点处停止,而是完成执行并退出。
以下是两种服务都继承的基本服务:
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();
}
}
}
}
当我运行任一服务时,我可以在await
方法的executeAsync
命令中设置断点(由*标记),该命令由调试器命中。调试PingService时,在调用之后的if / else或异常处理程序中的任何断点都未按预期命中。当我调试HomeService时会遇到相同的断点,所以我猜我必须在如何实现PingService时遇到一些问题。
这是我的服务按预期工作:
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);
}
}
}
这是我的服务,我看到了奇怪的调试器行为:
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);
}
}
}
导致我在调试器中看到不同行为的那两个服务有什么区别?