ScottThornton-7551 avatar image
0 Votes"
ScottThornton-7551 asked ScottThornton-7551 commented

Can the response to a PostAsJsonAsync post be NULL


Very occasionally, there will be a bug in my software that will result in a claim being sent to a government web service twice.

The first time the claim is sent there occurs an exception, and the transaction is rolled back. Later it will be sent again successfully. The government web site will have processed two claims.

Basically the code looks like this:

 try {
     var response = await _httpClientManager.PostAsJsonAsync<ClaimRequest>(url, request, null);
     return ResponseMessage(response);
 catch (Exception e)
     return Content(HttpStatusCode.BadGateway, e);

The caught exception is logged and looks like:

      "Response" : {
          "ClassName" : "System.ArgumentNullException",
          "Message" : "Value cannot be null.",
          "Data" : null,
          "InnerException" : null,
          "HelpURL" : null,
          "StackTraceString" : "   at System.Web.Http.Results.ResponseMessageResult..ctor(HttpResponseMessage response)\r\n   at System.Web.Http.ApiController.ResponseMessage(HttpResponseMessage response)\r\n   at some private stuff removed from here .MoveNext() in C:\\......Controller.cs:line 92",
          "RemoteStackTraceString" : null,
          "RemoteStackIndex" : 0,
          "ExceptionMethod" : "1\n.ctor\nSystem.Web.Http, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35\nSystem.Web.Http.Results.ResponseMessageResult\nVoid .ctor(System.Net.Http.HttpResponseMessage)",
          "HResult" : -2147467261,
          "Source" : "System.Web.Http",
          "WatsonBuckets" : null,
          "ParamName" : "response" 

The exception refers to Line 92, which is

 return ResponseMessage(response);

It isn't my code, and I am very very new to this software, and using PostAsJsonAsync

Is the error saying the PostAsJsonAsync response didn't return anything ( NULL ), or is the response to the post "Value cannot be null" and the service is responding to the JSON I have sent ( something is incorrect with the JSON)?

I do not know if the government service is responding correctly to my post ( or at all), but they do process the JSON claim that I have sent which is the confusing thing.

( I have reached out to them, but who knows if they will acknowledge the issue ).

Thanks for any advice.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ScottThornton-7551,

NULL exception will only happen the first time, the second time the data is sent successfully?

0 Votes 0 ·

Hi, the null response only happens once in a few thousand posts's.

0 Votes 0 ·
LanHuang-MSFT avatar image LanHuang-MSFT ScottThornton-7551 ·

Hi @ScottThornton-7551,
I recommend printing the logs to see exactly where the problem is. Generally, an ArgumentNullException will be thrown at runtime in the following two main cases:

  • An uninstantiated object is passed to a method. To prevent the error, instantiate the object.

  • An object returned from a method call is then passed as an argument to a second method, but the value of the original returned object is null. To prevent the error, check for a return value that is null and call the second method only if the return value is not null.

Or you can check whether the response is empty first, and then execute it if it is not empty.

 var response = await _httpClientManager.PostAsJsonAsync<ClaimRequest>(url, request, null);
  if(   response!= null){
      return ResponseMessage(response);

Best regards,
Lan Huang

0 Votes 0 ·
Show more comments

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

The error you are getting means the response did not properly parse into your object. You will need to look at the actual response to see the issue. It could be an error response, or some variant you did not code for.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.