【问题标题】:Get data from REST API using .NET core使用 .NET 核心从 REST API 获取数据
【发布时间】:2018-10-06 15:19:26
【问题描述】:

我正在尝试使用 HttpClient 从 REST API 获取数据,但我遇到了问题。 使用相同的服务,但来自控制台应用程序,一切正常。

从 Controller 一切正常,但是当来自 HttpHandler 的 GetAsync(url) 方法被调用时,它看起来像是在后台工作但什么也没发生..

这是我的服务:

public class UserService : IUsersService
{
    private const string url = "https://jsonplaceholder.typicode.com/users";
    private IHttpHandler httpHandler;

    public UserService(IHttpHandler httpHandler)
    {
        this.httpHandler = httpHandler;
    }

    public List<User> GetAllUsers()
    {
        HttpResponseMessage response = httpHandler.Get(url);

        if (response.IsSuccessStatusCode)
        {
            return response.Content.ReadAsAsync<List<User>>().Result;
        }

        //Nice to add Logging system that we cannot connect into following URL
        return new List<User>();
    }

    public User GetUserById(int userId)
    {
        HttpResponseMessage response = httpHandler.Get(
            string.Concat(url,"?id=",userId));

        if (response.IsSuccessStatusCode)
        {
            return response.Content.ReadAsAsync<List<User>>().Result.FirstOrDefault();
        }

        //Nice to add Logging system that we cannot connect into following URL
        return null;
    }
}

这是我的控制器(使用 WEB API 控制器,httpClient 没有从 REST API 获取数据)

public class UsersController : ApiController
{
    IUsersService userService;

    public UsersController(IUsersService userService)
    {
        this.userService = userService;
    }

    public List<User> GetUsers()
    {
        return userService.GetAllUsers();
    }

    public User GetUser(int userId)
    {
        return userService.GetUserById(userId);
    }
}

这是我目前正在使用 HttpClient 的 HttpHandler:

public class HttpHandler : IHttpHandler
{
    private HttpClient client = new HttpClient();

    public HttpResponseMessage Get(string url)
    {
        return GetAsync(url).Result;
    }

    public HttpResponseMessage Post(string url, HttpContent content)
    {
        return PostAsync(url, content).Result;
    }

    public async Task<HttpResponseMessage> GetAsync(string url)
    {
        return await client.GetAsync(url);
    }

    public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content)
    {
        return await client.PostAsync(url, content);
    }
}

这是我的控制台应用程序,它运行良好并显示正确的结果:

class Program
{
    static void Main(string[] args)
    {
        HttpHandler handler = new HttpHandler();
        UserService service = new UserService(handler);

        var users = service.GetAllUsers();

        Console.WriteLine(users[0].Email);
        Console.ReadKey();
    }
}

我真的不知道,可能是什么问题。

【问题讨论】:

    标签: c# .net-core dotnet-httpclient


    【解决方案1】:

    在挖网的过程中,我找到了解决问题的方法https://stackoverflow.com/a/10369275/5002910

    在 GetAsync 方法中的 HttpHandler 类中,我必须返回 return await client.GetAsync(url).ConfigureAwait(continueOnCapturedContext:false);

    【讨论】:

      猜你喜欢
      • 2023-02-05
      • 2019-04-18
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 2016-12-01
      相关资源
      最近更新 更多