3

I've got this code:

private async Task SavePlatypusComplianceFileOnServer(string year, string month)
{
    string monthAsMM = ReportRunnerConstsAndUtils.GetMonthAsMM(month);
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(ReportRunnerConstsAndUtils.SERVER_BASE_ADDRESS);
    String uriToCall = String.Format("/api/PlatypusCompliance/{0}/{1}{2}", _unit, year, monthAsMM);
    HttpResponseMessage response = await client.PostAsync(uriToCall, null);
}

...that works fine.

On looking at it, though, I thought to myself, "Self, why are you assigning a value to "response" when you're not using it? Why not just call the method without that? After all, Visual Studio or Resharper greys out "response" to indicate to you that it's moot/redundant."

So I changed the code to this:

private async Task SavePlatypusComplianceFileOnServer(string year, string month)
{
    . . . // all but the last line of the code is exactly the same
    await client.PostAsync(uriToCall, null);
}

But with that change, all Dallas breaks loose - I get err msgs about tables not being able to be dropped because they don't exist, and the call to the method via PostAsync() does not succeed.

Why does it make a diffrence whether I assign a value to response when it is not used?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 10
    And if you put the assignment back in, it works? It really, really should be fine... Can you reproduce this in a short but complete example? – Jon Skeet Jan 05 '16 at 18:05
  • 1
    This is most likely not due to a redundant variable assignment. Are you doing anything else? – Yuval Itzchakov Jan 05 '16 at 18:09
  • That's the only change. With it, it works fine; without it, it fails. The client (Winforms app) and server (Web API app) both call the same Stored Procedure. It must be a timing issue, but I don't know why this difference makes that difference. – B. Clay Shannon-B. Crow Raven Jan 05 '16 at 18:10
  • 3
    The unused variable is probably optimized out in a release build anyway... – i3arnon Jan 05 '16 at 18:14
  • 1
    Try using [Fiddler](http://fiddlertool.com/) to see what goes on the wire on each case. – Paulo Morgado Jan 06 '16 at 00:19

0 Answers0