I have an application in .NET Framework 4.8 (WCF) that makes http calls, also using Polly for retries and fallback management, but sometimes a System.NullReferenceException is raised, but I can't figure out where be the problem. This is the code:
private static async Task<bool> CallApi<TRequest>(TRequest request, string requestUri, Func<string, StringContent, Task<HttpResponseMessage>> func)
{
var jsonRequest = JsonConvert.SerializeObject(request);
var fallBackPolicy = Policy<HttpResponseMessage>.Handle<Exception>()
.FallbackAsync(new HttpResponseMessage(HttpStatusCode.SeeOther)
{
Content = new StringContent($"Exception has occurred in migration call. RequestUri: {requestUri}")
},
result =>
{
LogEventService.Logger.Error(result.Exception, "An unhandled exception occurred while retrying calling");
return Task.CompletedTask;
});
var waitAndRetryPolicy = Policy.HandleResult<HttpResponseMessage>(res => res.StatusCode == HttpStatusCode.InternalServerError).
WaitAndRetryAsync(2, retryAttempts => TimeSpan.FromMilliseconds(500));
var response = await fallBackPolicy
.WrapAsync(waitAndRetryPolicy)
.ExecuteAsync(async () =>
{
using (var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"))
{
content.Headers.Add("X-Correlation-ID", HttpContext.Current.Session[RequestId].ToString());
return await func(requestUri, content);
}
});
if(response.IsSuccessStatusCode)
return true;
await LogMessage(LogLevel.Error, response, requestUri);
return false;
}
and this is the StackTrace
System.NullReferenceException: Object reference not set to an instance of an object.
at UserMigrationService.<>c__DisplayClass22_0'1.<b__3>d.MoveNext() in ...\Services\UserMigrationService.cs:line 474
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Polly.Retry.AsyncRetryEngine.d__0'1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.AsyncPolicy'1.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.Wrap.AsyncPolicyWrapEngine.<>c__DisplayClass0_0'1.<b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() > at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.Fallback.AsyncFallbackEngine.d__0'1.MoveNext()
Can you help me find where the wrong part is? How can I better understand why this exception is thrown?
Thank you all
HttpContext.Currentisnullsometimes.content.Headers.Add("X-Correlation-ID", HttpContext.Current.Session[RequestId].ToString());How can HttpContext be null in an HTTP context?