Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using local variable to avoid null ref #2709

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,11 @@ public async Task SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
_timeoutHelper = timeoutHelper;
_factory.ApplyManualAddressing(ref _to, ref _via, message);
_httpClient = await _channel.GetHttpClientAsync(_to, _via, _timeoutHelper);
_httpRequestMessage = _channel.GetHttpRequestMessage(_via);

// The _httpRequestMessage field will be set to null by Cleanup() due to faulting
// or aborting, so use a local copy for exception handling within this method.
HttpRequestMessage httpRequestMessage = _httpRequestMessage;
HttpRequestMessage httpRequestMessage = _channel.GetHttpRequestMessage(_via);
_httpRequestMessage = httpRequestMessage;
Message request = message;

try
Expand All @@ -1033,7 +1033,7 @@ public async Task SendRequestAsync(Message message, TimeoutHelper timeoutHelper)

if (!suppressEntityBody)
{
_httpRequestMessage.Content = MessageContent.Create(_factory, request, _timeoutHelper);
httpRequestMessage.Content = MessageContent.Create(_factory, request, _timeoutHelper);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering if line 981 should be changed to

httpRequestMessage = _channel.GetHttpRequestMessage(_via);

Copy link
Member Author

@KKhurin KKhurin Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking a closer look - yes, at least this would finish hardening 1 method :)

}

try
Expand All @@ -1052,13 +1052,13 @@ public async Task SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
{
using (timeoutToken.Register(s_cancelCts, _httpSendCts))
{
_httpResponseMessage = await _httpClient.SendAsync(_httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, _httpSendCts.Token);
_httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, _httpSendCts.Token);
}

// As we have the response message and no exceptions have been thrown, the request message has completed it's job.
// Calling Dispose() on the request message to free up resources in HttpContent, but keeping the object around
// as we can still query properties once dispose'd.
_httpRequestMessage.Dispose();
httpRequestMessage.Dispose();
success = true;
}
catch (HttpRequestException requestException)
Expand Down