c# - .NET Core 中是否取代了 HttpContent.ReadAsAsync<T> 方法?

标签 c# asp.net-core httpresponse

以下是指一个.NET Core 应用程序,其依赖项如下...

Microsoft.NETCore.App
Microsoft.AspNet.WepApi.Client (5.2.7)
Microsoft.com 上的文档 Call a Web API From a .NET Client (C#)从 2017 年 11 月开始。
链接... https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
文档中包含此客户端调用 HTTP GET。
    static HttpClient client = new HttpClient();
    static async Task<Product> GetProductAsync(string path)
    {
        Product product = null;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode)
        {
            product = await response.Content.ReadAsAsync<Product>();
        }
        return product;
    }
response.Content指的是 HttpContent目的。截至 2020 年 7 月 HttpContent没有带有签名 ReadAsAsync<T>() 的实例方法,至少根据以下文件。但是,此实例方法有效。
没有带有签名的实例方法的引用链接 ReadAsAsync<T>() ...
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1
有一个静态方法 HttpContentExtensions.ReadAsAsync<T>(myContent)哪里myContent指的是 HttpContent目的。这种静态方法也有效。
引用链接...
https://docs.microsoft.com/en-us/previous-versions/aspnet/hh834253(v=vs.118)
例如,一个记录在案的签名具有...

static icon followed by ReadAsAsync<T>(HttpContent)


和说明它将返回 Task<T> .这个静态方法可能是实例方法的幕后实现。
但是,静态方法网页顶部的信息表明...
“我们不再定期更新此内容。有关如何支持此产品、服务、技术或 API 的信息,请查看 Microsoft 产品生命周期。”
HttpContent.ReadAsAsync<T>() .NET Core 3.1 取代了实例和静态两种形式?

最佳答案

其他答案都不对。
ReadAsAsync 方法是 System.Net.Http.Formatting.dll 的一部分
这又是 nuget 的一部分: Microsoft.AspNet.WebApi.Client
我刚刚创建了一个新的控制台项目 .Net Core 3.1 并添加了 2 个 nuget

  • 牛顿软件
  • Microsoft.AspNet.WebApi.Client

  • 我用 .NET Core 3.1 创建了一个项目,这里有一些图片:
    enter image description here
    这是我的项目文件:
    enter image description here
    这是我刚刚编写的代码,它编译得很好:
    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    namespace Custom.ApiClient
    {
        internal static class WebApiManager
        {
            //private const string _requestHeaderBearer = "Bearer";
            private const string _responseFormat = "application/json";
    
            private static readonly HttpClient _client;
    
            static WebApiManager()
            {
    
                // Setup the client.
                _client = new HttpClient { BaseAddress = new Uri("api url goes here"), Timeout = new TimeSpan(0, 0, 0, 0, -1) };
    
                _client.DefaultRequestHeaders.Accept.Clear();
                _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_responseFormat));
    
                // Add the API Bearer token identifier for this application.
                //_client.DefaultRequestHeaders.Add(RequestHeaderBearer, ConfigHelper.ApiBearerToken);       
            }
    
            public static async Task<T> Get<T>()
            {
                var response = _client.GetAsync("api extra path and query params go here");
    
                return await ProcessResponse<T>(response);
            }
    
            private static async Task<T> ProcessResponse<T>(Task<HttpResponseMessage> responseTask)
            {
                var httpResponse = await responseTask;
    
                if(!httpResponse.IsSuccessStatusCode)
                    throw new HttpRequestException(httpResponse.ToString());
    
                var dataResult = await httpResponse.Content.ReadAsAsync<T>();
    
                return dataResult;
            }
        
        }
    }
    
    更新:
    清除 Microsoft.AspNet.WebApi.Client 包依赖项的一些混淆
    这是显示截至 2020 年 10 月 27 日的依赖关系图片,清楚地表明它依赖于 Newtonsoft JSON 10 或更高版本。截至今天,没有使用 System.Text.Json 替代 ReadAsAsync ......因此您可以使用 ApiClient + Newtonsoft Json 或使用 System.Text.Json 创建自己的
    enter image description here

    关于c# - .NET Core 中是否取代了 HttpContent.ReadAsAsync<T> 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63108280/

    相关文章:

    c# - asp.net core blazor webassessivecrypto - 如何使用自定义 key

    c++ - 不带扩展名的文件下载 Socket编程

    c# - 如何显示在 Asp.net MVC 中找不到的图像

    c# - 我无法使用 C# 将数据保存到 SQL 数据库

    c# - .NET Core Identity as UI 取消注册

    asp.net-core - ASP.NET Core/Kestrel 中的摘要式身份验证

    c# - 如何为内插字符串设置模拟?

    c# - VS2015 : Will they use the Roslyn compiler? 中的 VS2013 C# 项目

    http-headers - 如何使用 NanoHttpd 响应发送文件名

    java - Spring RestTemplate - 错误处理