HttpActionExecutedContext response content always is empty - c#

When throws an exception in server side, the response content in OnException works well but in client side (SendAsync) it is empty..
public override void OnException(HttpActionExecutedContext context)
{
var exception = context.Exception;
if (exception == null) return;
context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
context.Response.Content = new StringContent(exception.Message);
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
var responseBodyAsText = string.Empty;
try
{
response = await base.SendAsync(request, cancellationToken);
if (!response.IsSuccessStatusCode)
{
responseBodyAsText = await response.Content.ReadAsStringAsync();
}
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
throw new BusinessException(responseBodyAsText);
}
return response;
}

Related

Xamarin - Logout when token expires

I have an Api hosted on Azure which I consume on my Xamarin Forms project.
I show the login page at the beginning and I check if the JWT token has expired but I also want to check that on each http method in case it expires while the user is using the app.
So I need to either show the user the login page and tell them to login again I have been searching how to do that I can't get it right.
Here is my AzureApiService class.
public class AzureApiService
{
HttpClient httpClient;
public AzureApiService()
{
#if DEBUG
var httpHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (o, cert, chain, errors) => true
};
#else
var httpHandler = new HttpClientHandler();
#endif
httpClient = new HttpClient(httpHandler);
httpClient.Timeout = TimeSpan.FromSeconds(15);
httpClient.MaxResponseContentBufferSize = 256000;
}
public async Task<string> LoginAsync(string url, AuthUser data)
{
var user = await HttpLoginPostAsync(url, data);
if (user != null)
{
//Save data on constants
CurrentPropertiesService.SaveUser(user);
return user.Token;
}
else
{
return string.Empty;
}
}
// Generic Get Method
public async Task<T> HttpGetAsync<T>(string url, string token)
{
T result = default(T);
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = httpClient.GetAsync(url).Result;
HttpContent content = response.Content;
if (response.IsSuccessStatusCode)
{
var jsonResponse = await content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
if (IsExpired(token))
{
await Logout();
}
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
}
catch (Exception ex)
{
OnError(ex.ToString());
}
return result;
}
// Generic Post Method
public async Task<T> HttpPostAsync<T>(string url, string token, T data)
{
T result = default(T); // résultat de type générique
try
{
string json = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await httpClient.PostAsync(new Uri(url), content);
var jsonResponse = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var jsons = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
if (IsExpired(token))
{
await Logout();
}
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
return result;
}
catch (Exception ex)
{
OnError(ex.ToString());
return result;
}
}
// Generic Put Method
public async Task<T> HttpPutAsync<T>(string url, string token, T data)
{
T result = default(T); // résultat de type générique
try
{
string json = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await httpClient.PutAsync(new Uri(url), content);
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
if (IsExpired(token))
{
await Logout();
}
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
return result;
}
catch (Exception ex)
{
OnError(ex.ToString());
return result;
}
}
// Generic Delete Method
public async Task<bool> HttpDeleteAsync(string url, string token)
{
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await httpClient.DeleteAsync(url);
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
if (IsExpired(token))
{
await Logout();
}
return false;
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
}
catch (Exception ex)
{
OnError(ex.ToString());
return false;
}
}
// Login Post Method
public async Task<T> HttpLoginPostAsync<T>(string url, T data)
{
T result = default(T); // résultat de type générique
try
{
string json = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(new Uri(url), content);
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
return result;
}
catch (Exception ex)
{
OnError(ex.ToString());
return result;
}
}
public bool IsExpired(string token)
{
if (token == null || "".Equals(token))
{
return true;
}
/***
* Make string valid for FromBase64String
* FromBase64String cannot accept '.' characters and only accepts stringth whose length is a multitude of 4
* If the string doesn't have the correct length trailing padding '=' characters should be added.
*/
int indexOfFirstPoint = token.IndexOf('.') + 1;
String toDecode = token.Substring(indexOfFirstPoint, token.LastIndexOf('.') - indexOfFirstPoint);
while (toDecode.Length % 4 != 0)
{
toDecode += '=';
}
//Decode the string
string decodedString = Encoding.ASCII.GetString(Convert.FromBase64String(toDecode));
//Get the "exp" part of the string
Regex regex = new Regex("(\"exp\":)([0-9]{1,})");
Match match = regex.Match(decodedString);
long timestamp = Convert.ToInt64(match.Groups[2].Value);
DateTime date = new DateTime(1970, 1, 1).AddSeconds(timestamp);
DateTime compareTo = DateTime.UtcNow;
int result = DateTime.Compare(date, compareTo);
return result < 0;
}
private async Task Logout()
{
CurrentPropertiesService.Logout();
CurrentPropertiesService.RemoveCart();
await Shell.Current.GoToAsync($"//main");
}
private void OnError(string error)
{
Console.WriteLine("[WEBSERVICE ERROR] " + error);
}
}
So you can see that in each http method I'm trying yo check if the token has expired already and then logout but it just gives an error.
On my Logout method I just want to delete all the properties and then navigate to the login page but it isn't working.
Please help I would like to know how to do this. Thanks.
EDIT
Trying to implement DelegatingHandler stops at SendAsync
Here is my HttpDelegatingHandler class
public class HttpDelegatingHandler : DelegatingHandler
{
public HttpDelegatingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("Bearer", CurrentPropertiesService.GetToken());
// before request
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// after request
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
await Logout();
}
return response;
}
private async Task Logout()
{
CurrentPropertiesService.Logout();
CurrentPropertiesService.RemoveCart();
await Shell.Current.GoToAsync($"//main");
}
}
Here my AzureApiService class
public class AzureApiService
{
HttpClient httpClient;
public AzureApiService()
{
var clientHandler = new HttpClientHandler();
#if DEBUG
clientHandler.ServerCertificateCustomValidationCallback =
(sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
#endif
httpClient = new HttpClient(new HttpDelegatingHandler(clientHandler));
httpClient.Timeout = TimeSpan.FromSeconds(15);
httpClient.MaxResponseContentBufferSize = 256000;
}
public async Task<string> LoginAsync(string url, AuthUser data)
{
var user = await HttpLoginPostAsync(url, data);
if (user != null)
{
//Save data on constants
CurrentPropertiesService.SaveUser(user);
return user.Token;
}
else
{
return string.Empty;
}
}
// Generic Get Method
public async Task<T> HttpGetAsync<T>(string url, string token)
{
T result = default(T);
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await httpClient.GetAsync(url);
HttpContent content = response.Content;
var jsonResponse = await content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
catch (Exception ex)
{
OnError(ex.ToString());
}
return result;
}
It works for PostAsync
// Login Post Method
public async Task<T> HttpLoginPostAsync<T>(string url, T data)
{
T result = default(T); // résultat de type générique
try
{
string json = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(new Uri(url), content);
var jsonResponse = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
return result;
}
catch (Exception ex)
{
OnError(ex.ToString());
return result;
}
}
But as I said it stops when trying to get data
You can handle 401 Unauthorized response in a custom Delegating handler. This way you can handle anything before and after request execution in a single place.
public class HttpDelegatingHandler : DelegatingHandler
{
public HttpDelegatingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("Authorization", string.Format("Basic {0}", MyUserRepository.AuthToken));
// before request
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// after request
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
await Shell.Current.GoToAsync($"//main");
}
return response;
}
}
public class AzureApiService
{
HttpClient httpClient;
public AzureApiService()
{
var clientHandler = new HttpClientHandler();
#if DEBUG
clientHandler.ServerCertificateCustomValidationCallback =
(sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
#endif
httpClient = new HttpClient(new HttpDelegatingHandler(clientHandler));
httpClient.Timeout = TimeSpan.FromSeconds(15);
httpClient.MaxResponseContentBufferSize = 256000;
}
....
// Generic Get Method
public async Task<T> HttpGetAsync<T>(string url, string token)
{
T result = default(T);
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await httpClient.GetAsync(url);
var jsonResponse = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
catch (Exception ex)
{
OnError(ex.ToString());
}
return result;
}

System.ObjectDisposedException: The CancellationTokenSource has been disposed

I sent 2 and more requests together for rest service and after second response I get an error. Also locally all works great, but on the env I catch exception.
The operation was canceled.
The error acquired: System.ObjectDisposedException: The
CancellationTokenSource has been disposed.
at
System.Threading.CancellationTokenSource.ThrowObjectDisposedException()
Can anyone suggest something?
[NotNull]
[ItemNotNull]
private async Task<TResponse> PostAsync<TRequest, TResponse>([NotNull] string path, [NotNull] TRequest request, CancellationToken cancellationToken) where TRequest : class where TResponse : class {
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console()
.CreateLogger();
Console.WriteLine("client");
var client = this.GetHttpClient();
Console.WriteLine("content");
using var content = this.CreateHttpContent(request);
Console.WriteLine("response");
try {
using var response = await client.PostAsync(path, content, cancellationToken).ConfigureAwait(false);
return await this.ParseResponseAsync<TResponse>(response, cancellationToken).ConfigureAwait(false);
} catch (OperationCanceledException exception) {
logger.Verbose(exception.Message);
throw;
} catch (Exception exception) {
Console.WriteLine(exception);
logger.Verbose(exception.Message);
logger.Error(exception.StackTrace);
logger.Fatal(exception.InnerException?.Message);
throw;
}
}
[NotNull]
private HttpClient GetHttpClient() {
var client = new HttpClient { BaseAddress = this.m_baseAddress };
var headers = client.DefaultRequestHeaders;
headers.Accept.Clear();
headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MEDIA_TYPE_JSON));
headers.ConnectionClose = true;
return client;
}
[NotNull]
private HttpContent CreateHttpContent<TRequest>([NotNull] TRequest request) where TRequest : class {
if(request == null) throw new ArgumentNullException(nameof(request));
var content = JsonSerializer.Serialize(request, s_serializerOptions);
return new StringContent(content, Encoding.UTF8, MEDIA_TYPE_JSON);
}
public async Task<long> CreateEmployeeRequestAsync(EmployeeRequestCreateRequest request, CancellationToken cancellationToken = default) {
if(request == null) throw new ArgumentNullException(nameof(request));
const string path = "employeerequest";
return (await this.PostAsync<EmployeeRequestCreateRequest, EmployeeRequestCreateResponse>(path, request, cancellationToken).ConfigureAwait(false)).RequestID;
}

Intercept Json before returning from web.api

I am looking to intercept the returning model from my web.api and run the returning data through a translation service, i.e. Google or Azure for languages other than English. Is there way to add an attribute or additional config to intercept the model, perform the translation, and then return back to the controller?
try this code also add this code
//add this line global.asax file
GlobalConfiguration.Configuration.MessageHandlers.Add(new CustomLogHandler());
public class CustomLogHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var logMetadata = await BuildRequestMetadata(request);
var response = await base.SendAsync(request, cancellationToken);
logMetadata = await BuildResponseMetadata(logMetadata, response);
await SendToLog(logMetadata);
return response;
}
private async Task<LogMetadata> BuildRequestMetadata(HttpRequestMessage request)
{
LogMetadata log = new LogMetadata
{
RequestMethod = request.Method.Method,
RequestTimestamp = DateTime.Now,
RequestUri = request.RequestUri.ToString(),
RequestContent = await request.Content.ReadAsStringAsync(),
};
return log;
}
private async Task<LogMetadata> BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
{
logMetadata.ResponseStatusCode = response.StatusCode;
logMetadata.ResponseTimestamp = DateTime.Now;
logMetadata.ResponseContentType = response.Content == null ? string.Empty : response.Content.Headers.ContentType.MediaType;
logMetadata.Response = await response.Content.ReadAsStringAsync();
return logMetadata;
}
private async Task<bool> SendToLog(LogMetadata logMetadata)
{
try
{
//write this code
}
catch
{
return false;
}
return true;
}
}

Cannot send the same request message multiple times using Func<HttpRequestMessage>

Our API has retry logic for calling another endpoint. But it kept giving me an error of
The request message was already sent. Cannot send the same request
message multiple times
Here's my code
public async Task<object> GetResponse()
{
var httpRequestMessage = ConstructHttpRequestForBatchUpdate(batchRequest, client, requestUri);
HttpResponseMessage httpResponseMessage = await _retryHttpRequest.ExecuteAsync(() => httpRequestMessage, client, maxRetryValue);
}
private HttpRequestMessage ConstructHttpRequestForBatchUpdate(JArray batchRequest, HttpClient client, Uri requestUri)
{
var batchReqStr = JsonConvert.SerializeObject(batchRequest);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var httpRequestMessage = new HttpRequestMessage()
{
Content = new StringContent(batchReqStr, Encoding.UTF8, "application/json"),
Method = HttpMethod.Put,
RequestUri = requestUri
};
return httpRequestMessage;
}
public class RetryHttpRequest : IRetryHttpRequest
{
public async Task<HttpResponseMessage> ExecuteAsync(Func<HttpRequestMessage> requestMessage, HttpClient client, int maxTryValue)
{
var remainingTries = maxTryValue;
var exceptions = new List<Exception>();
do
{
--remainingTries;
try
{
return await ExecuteSingleAsync(requestMessage(), client);
}
catch (Exception e)
{
exceptions.Add(e);
}
}
while (remainingTries > 0);
throw new AggregateException(exceptions);
}
public async Task<HttpResponseMessage> ExecuteSingleAsync(HttpRequestMessage requestMessage, HttpClient client)
{
try
{
HttpResponseMessage httpResponseMessage = await client.SendAsync(requestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
{
return httpResponseMessage;
}
else
{
var exception = new InvalidOperationException();
throw new Exception();
}
}
catch (HttpRequestException httpException)
{
throw httpException;
}
}
}
To my understanding, Func<HttpRequestMessage> allows it to create a new instance of HttpRequestMessage. For example, for this line of code
return await ExecuteSingleAsync(requestMessage(), client);
requestMessage() is creating a new instance for every loop. But if my understanding is correct, i am not sure why it is still giving me this error of sending the same request.
requestMessage() is creating a new instance for every loop.
This is not the case - the Func you handed in is () => httpRequestMessage, which always returns the same instance.
Try this instead:
_retryHttpRequest.ExecuteAsync(() => ConstructHttpRequestForBatchUpdate(batchRequest, client, requestUri), client, maxRetryValue);

httpclient exception handling

I am handing HttpRequestException when I use PostAsync and it works fine, but when I am trying to handle same exception on GetAsync it throws TaskCanceledException a task was cancelled with a long timeout instead. How do I make GetAsync throw HttpRequestException?
public async Task<bool> AddQrCodeToRequest(int projectId, int requestId, string code, string token)
{
var data = JsonConvert.SerializeObject(new { code });
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var result = await client.PostAsync(url, content);
if (result.IsSuccessStatusCode)
{
return true;
}
else
{
throw new Exception(CreateExceptionDescription(await result.Content.ReadAsStringAsync()));
}
}
public async Task<List<string>> GetUpdatedQrCodesList(Request request, string token)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var result = await client.GetAsync(url);
if (result.IsSuccessStatusCode)
{
var requestsJson = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<string>>(requestsJson);
}
else
{
throw new Exception(CreateExceptionDescription(await result.Content.ReadAsStringAsync()));
}
}
handling post
try
{
string QrCode = result.Text;
if (await restService.AddQrCodeToRequest(Request, result.Text, Vars.User.Token))
{
QrCodes.Add(QrCode);
await DisplayAlert("Code added", QrCode, "OK");
}
}
catch (Exception ex)
{
if (ex is HttpRequestException)
{
//network ex handling
}
else
{
//other handling
}
}
handling get (app crashes after timeout)
try
{
UpdatedQrCodes = await restService.GetUpdatedQrCodesList(Request, Vars.User.Token);
}
catch (Exception ex)
{
if (ex is HttpRequestException)
{
//never thrown
}
else
{
//never also thrown
}
}
As a workaround use nuget Xamarin.Essentials and before you execute your GET check if there's internet available:
var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
{
// Connection to internet is available
}

Categories