I have a standalone handler (.ASHX
) written in C# that deals with incoming push notifications that carry XML payloads, it then parses these XMLs and extracts and deals with any necessary information taken from them. The issue is I cannot test this locally as our development environment is firewalled too heavily and I'm not allowed to allow exceptions for incoming notifications.
I have tested this on our test environment and the notifications are being received and processed correctly, however after a random code review it looks like there may be an issue with how I am handling responseStreams
and streamReaders
.
My question is, is this a valid way to handle the closing of these resources or would this potentially cause a NullReferenceException
?
What is the standard practice for dealing with these resources, should I be using the using
statement to keep them only in use for that scope or will this code suffice?
Stream responseStream = null;
StreamReader reader = null;
string serverResponse = null;
try
{
responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream);
serverResponse = reader.ReadToEnd();
}
finally
{
if (reader == null)
{
reader.Close();
}
if (responseStream == null)
{
responseStream.Close();
}
response.Close();
}