I am attempting to run awaited Async connections to a remote webservice. The URL is configurable, and if it is invalid, I am getting a Null Reference exception that ignores Try/Catch blocks and crashes the entire IIS Worker Process.
public static void SendMessage(FileInfo FileToSend, string RecipientName, string RecipientNumber){
{
//Connection Info retrieved from DB
string url = "http://NotARealUrl.com/";
string username = "NotARealUN";
string password = "NotARealPW";
//not Awaiting this because all logging in event of job failure is handled by the job itself. There is no reason to wait for completion
Send (FileToSend, url, username, password, RecipientName, RecipientNumber)
}
public static async Task Send(FileInfo FileToSend, string url, string username, string password, string sentByID, string faxRecipient, string faxNumber)
{
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential(username, password),
PreAuthenticate = true
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var response = await client.GetAsync(url); //Exception is throw when I step past this point
response.EnsureSuccessStatusCode();
var contentAsString = await response.Content.ReadAsStringAsync();
var root = JsonConvert.DeserializeObject<FaxingAPIRoot>(contentAsString);
//Code continues, using 'root' in order to send the message in accordance with the Faxing API I'm using
}
catch (Exception e)
{
//Graceful Error Handling
}
}
At the line indicated above...
var response = await client.GetAsync(url);
The code throws the following exception, which bypasses the Catch block and immediately crashes the host process, whether that be Visual Studio's IIS Express, or the IIS Worker Process.
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.
With a Callstack of...
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.ThrowAsyncIfNecessary.AnonymousMethod__18_0(object s)
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
[Native to Managed Transition]
I have verified that the URL being passed in is not null and the HttpClient is not null. Is there a way to force this exception to be caught or, barring that, to preempt the error by checking the validity of that URL before it attempts to call it?