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

Small diagram:

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

Problem: When I call WebApi from a .NET Standard project (using HttpClient), it doesn't actually send the object on demand. When it is run through a .NET Standard project, the object is populated with all the fields that are needed, in this case it is the exception that is caught and the message, stacktrace, etc. Transferred together.

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 is passed to WebApi, the object no longer contains any data, but is filled with zeros and empty strings, which leads me to believe that POST is not actually sending data.

At this point, I'm really not sure what I'm missing. Google hasn't helped much and I haven't been able to find anything similar to my problem.

After it burns through the call stack, it spits out some error messages, which again, I had no 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 / faced / fixed this issue? Is there something I am doing wrong in the call with the HttpClient?

+3


source to share


1 answer


Okay, in an act of absolute and complete selflessness (not a word, but I needed to explain something about how uncomfortable I feel right now). I figured out the problem.

The webapi method subscription was the culprit:

public async Task<IActionResult> SaveAsync([FromBody]CaughtException caughtExceptionItem)

      



I missed it [FromBody]

. I've done an api for most of our systems before and I don't know why I missed something so simple.

Hope my mistake (and the subsequent waste of days) helps others!

+3


source







All Articles