'WcfEntAdmin' - это служба WcfService, которая внутренне вызывает службу WebApi Core для получения некоторых данных. Я использую HttpClient для вызова службы WebApi Core. Вызов успешно возвращается с кодом состояния 200, но когда я пытаюсь десериализовать ответ, приложение переходит в состояние прерывания. Я не уверен, что может пойти не так.
public class ApiHelper
{
private readonly JWTHandler _jwtHandler;
private readonly HttpClient _httpClient;
private readonly HttpClient _secureHttpClient;
public ApiHelper()
{
_httpClient = new HttpClient(ConfigureHandler());
_jwtHandler = new JWTHandler("myusername", "test", memoryCache, new AuthorizationClient(_configuration, _httpClient));
_jwtHandler.InnerHandler = ConfigureHandler();
_secureHttpClient = new HttpClient(_jwtHandler);
}
public bool IsAutoPolicy()
{
// the code here makes a call to the Web Api Core using the _securityHttpClient and determines if it's a Auto Policy or not
}
public async Task<ApiVehiclesInfo> GetVehicleList(string policyNumber, DateTime effectiveDate)
{
ApiVehiclesInfo vehiclesInfo = new ApiVehiclesInfo();
if (IsPATEnabled())
{
var secureApiClient = new BaseApiClient(_configuration, _secureHttpClient);
var response = await secureApiClient.GetAsync("PolicyApi",$"legacy_adapter/vehicles/{policyNumber}/{effectiveDate.ToString("yyyy-MMM-dd")}");
if (response.IsSuccessStatusCode)
{
try
{
vehiclesInfo = await response.Deserialize<ApiVehiclesInfo>();
}
catch (Exception e)
{
Debug.WriteLine(e);
throw;
}
}
}
return vehiclesInfo;
}
Вызывающий код сначала вызывает IsAutoPolicy()
, а затем вызывает GetVehiclesList("1234567",DateTime.Now)
. Режим перерыва
РЕДАКТИРОВАТЬ: у меня Deserialize как метод расширения HttpResponseMessage как
public static async Task<TResult> Deserialize<TResult>(this HttpResponseMessage response)
{
try
{
using (Stream stream = await response.Content.ReadAsStreamAsync())
using (StreamReader streamReader = new StreamReader(stream))
using (JsonReader textReader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
};
return serializer.Deserialize<TResult>(textReader);
}
}
catch (Exception ex)
{
ex.Data.Add("StatusCode", response.StatusCode);
ex.Data.Add("ReasonPhrase", response.ReasonPhrase);
throw new SerializationException("Failed to deserialize", ex);
}
}