In a .Net 4.7.1 targeted application I am using HttpClient to perform Http requests.
When performing a GET request for a URL that returns a Http 204 result (no content), this results in a System.NullReferenceException: 'Object reference not set to an instance of an object.'.
On top of that, the exception is not caught by the try catch, which I think has something to do with the async call, as specified in this question: Why can't I catch an exception from async code?, but I want to focus on not getting the exception in the first place.
The same request was tested in a .Net Core application using IHttpClientFactory where it works as expected.
protected async Task<HttpResponseMessage> GetResponse(string path, Dictionary<string, string> headers)
{
HttpResponseMessage responseMessage = null;
var request = new HttpRequestMessage(HttpMethod.Get, path);
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
var proxy = new WebProxy
{
Address = new Uri($"http://{ProxyHost}:{ProxyPort}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(userName: ProxyUser, password: ProxyPW)
};
// create a client handler which uses that proxy
var httpClientHandler = new HttpClientHandler
{
Proxy = proxy
};
var httpclient = new HttpClient(httpClientHandler);
try
{
responseMessage = await httpclient.SendAsync(request);
}
catch (Exception ex)
{
// handle error …
}
return responseMessage;
}
The line throwing the exception is:
responseMessage = await httpclient.SendAsync(request);
Visual Studio catches the unhandled exception:
Exception details:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
This exception was originally thrown at this call stack:
[External Code]
An exception with StackTrace is visible in Event Viewer, but it doesn't help me a lot:
StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
How can I make HttpClient handle the response correctly? I have no control over the server sending the Http 204 response.