首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Refit使用入门

Refit使用入门

原创
作者头像
吴晓阳
发布2024-11-22 15:53:36
发布2024-11-22 15:53:36
2460
举报
文章被收录于专栏:微服务微服务

前言

原文:Refit: The automatic type-safe REST library for .NET Core, Xamarin and .NET

百度翻译:Refit:适用于.NET Core、Xamarin和.NET的自动类型安全REST库

官方网站:https://github.com/reactiveui/refit

新建weapi项目并添加包Refit与Refit.HttpClientFactory

新建接口IGitHubApi.cs

代码语言:C#
复制
using Refit;

namespace RefitDemo
{
    public interface IGitHubApi
    {
        [Get("/WeatherForecast")]
        Task<IEnumerable<WeatherForecast>> GetWeatherForecastList();
    }
}

修改Program.cs

代码语言:c#
复制
builder.Services
    .AddRefitClient<IGitHubApi>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://localhost:7159"));

这里的7159改成你的项目端口

修改WeatherForecastController.cs

代码语言:C#
复制
using Microsoft.AspNetCore.Mvc;
using Refit;

namespace RefitDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IGitHubApi _gitHubApi;
        public WeatherForecastController(ILogger<WeatherForecastController> logger, IGitHubApi gitHubApi)
        {
            _logger = logger;
            _gitHubApi = gitHubApi;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpPost(Name = "GetTest")]
        public async Task<IEnumerable<WeatherForecast>> GetTestAsync()
        {
            return await _gitHubApi.GetWeatherForecastList();
        }

        [HttpPost("GetTest1")]
        public async Task<IEnumerable<WeatherForecast>> GetTest1Async()
        {
            var gitHubApi = RestService.For<IGitHubApi>("https://localhost:7159");
            return await gitHubApi.GetWeatherForecastList();
        }
    }
}

这里代码就是这两种调用方式

代码语言:C#
复制
        [HttpPost(Name = "GetTest")]
        public async Task<IEnumerable<WeatherForecast>> GetTestAsync()
        {
            return await _gitHubApi.GetWeatherForecastList();
        }

        [HttpPost("GetTest1")]
        public async Task<IEnumerable<WeatherForecast>> GetTest1Async()
        {
            var gitHubApi = RestService.For<IGitHubApi>("https://localhost:7159");
            return await gitHubApi.GetWeatherForecastList();
        }

后面两个api就是Refit的调用结果

作者

吴晓阳

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 新建weapi项目并添加包Refit与Refit.HttpClientFactory
  • 新建接口IGitHubApi.cs
  • 修改Program.cs
  • 修改WeatherForecastController.cs
  • 这里代码就是这两种调用方式
  • 后面两个api就是Refit的调用结果
  • 作者
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档