代码之家  ›  专栏  ›  技术社区  ›  SlackerCoder

.NET标准2.NET核心2,HttpClient未按预期运行(传递null)

  •  4
  • SlackerCoder  · 技术社区  · 7 年前

    略作概述:

    问题:

    以下是.NET标准项目的代码:

            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());
            }
    

    当这个调用传递给WebApi时,对象不再包含任何数据,而是充满了null和空字符串,这让我相信POST实际上并没有发送数据。

    在这一点上,我真的不确定我错过了什么。谷歌几乎帮不上什么忙,我也找不到与我的问题类似的东西。

    > 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()
    

    有人看到/遇到/解决过这个问题吗?在与HttpClient的通话中,我是否做错了什么?

    1 回复  |  直到 7 年前
        1
  •  4
  •   SlackerCoder    7 年前

    好的,在一个关于绝对和完全愚蠢的行为中(不是一个词,但我需要编造一些东西来解释我现在感觉有多么愚蠢),我发现了这个问题。

    webapi方法签名是罪魁祸首:

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

    我错过了 [FromBody] 我以前为我们的大多数系统做过api,我不知道为什么我错过了这么简单的东西。