.NET Standard 2, .NET Core 2, HttpClient not behaving as expected (passing null)

Viewed 9377

A little outline:

We have a .NET Standard (2.0) project that calls the WebApi (.NET Core 2.0) which should process a message (by message, it can be ANYTHING the user wants to record, could be exceptions, warnings, or just notes to store in the database) store that message and return the updated message to the caller.

The issue: When I call the WebApi from the .NET Standard project (using HttpClient) it's not actually POSTing the object as requested. When it runs through the .NET Standard project, the object is populated with all the fields that are required, in this case, it's an exception that is caught and the message, stacktrace, etc is passed along.

Here's the code from the .NET Standard project:

        IDataAccessResult<CaughtException> result = new DataAccessResult<CaughtException>();

        string baseUrl = "baseurl";
        var client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("a", "b");

        // HTTP POST
        var myContent = JsonConvert.SerializeObject(caughtException);
        StringContent sc = new StringContent(myContent, System.Text.Encoding.UTF8, "application/json");
        System.Uri uri = new System.Uri(string.Format("{0}/api/v6/CaughtException/SaveAsync", baseUrl));

        HttpResponseMessage response = await client.PostAsync(uri, sc);

        response.EnsureSuccessStatusCode();
        if (response.IsSuccessStatusCode)
        {
            result = JsonConvert.DeserializeObject<DataAccessResult<CaughtException>>(await response.Content.ReadAsStringAsync());
        }

When this call gets passed to the WebApi, the object no longer contains any data, but is full of nulls and empty strings, which leads me to believe the POST is not actually sending the data.

At this point, I'm really not sure what I am missing. Google has been little to no help, and I haven't been able to find anything similar to my issue.

After it burns through the call stack it spits out some error messages, that again, I've had little luck connecting to my code, but here they are:

> WinHttpException: The connection with the server was terminated abnormally

> Unknown location

> IOException: The read operation failed, see inner exception.

>System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
>System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Threading.Tasks.RendezvousAwaitable.GetResult()
System.Net.Http.WinHttpResponseStream+<CopyToAsyncCore>d__18.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Net.Http.NoWriteNoSeekStreamContent+<>c.<SerializeToStreamAsync>b__4_0(Task t, object s)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Net.Http.HttpContent+<LoadIntoBufferAsyncCore>d__48.MoveNext()

> HttpRequestException: Error while copying content to a stream.

>System.Net.Http.HttpContent+<LoadIntoBufferAsyncCore>d__48.MoveNext()
>System.Net.Http.HttpContent+<LoadIntoBufferAsyncCore>d__48.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Net.Http.HttpClient+<FinishSendAsyncBuffered>d__58.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
LogService.Gateway.CaughtExceptionGateway+<SaveAsync>d__6.MoveNext() in CaughtExceptionGateway.cs
+
            HttpResponseMessage response = await client.PostAsync(uri, sc);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
LogService.Api.CaughtExceptionController+<LogError>d__11.MoveNext() in CaughtExceptionController.cs
+
            ce = (await _exceptions.SaveAsync(ce)).Payload;
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
LogService.Api.CaughtExceptionController+<GetCaughtExceptionAsync>d__5.MoveNext() in CaughtExceptionController.cs
+
                    CaughtException ce = await LogError(action, controller, loggingApplicationCodeId, accountId, loggingExceptionTypeId, e.Exception.Message, e.Exception.StackTrace, null, token, storedProcedureName, storedProcedureParameters);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Has anyone seen/come across/fixed this issue? Is there something I'm doing wrong in the call with the HttpClient?

1 Answers
Related