|
| 1 | +# 快速上手 |
| 2 | + |
| 3 | +## 依赖环境 |
| 4 | + |
| 5 | +`WebApiclientCore`要求项目的`.NET`版本支持`.NET Standard2.1`,并且具备依赖注入的环境。 |
| 6 | + |
| 7 | +## 从 Nuget 安装 |
| 8 | + |
| 9 | +| 包名 | Nuget | 描述 | |
| 10 | +| ----------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- | |
| 11 | +| [WebApiClientCore](https://www.nuget.org/packages/WebApiClientCore) |  | 基础包 | |
| 12 | +| [WebApiClientCore.Extensions.OAuths](https://www.nuget.org/packages/WebApiClientCore.Extensions.OAuths) |  | OAuth2 与 token 管理扩展包 | |
| 13 | +| [WebApiClientCore.Extensions.NewtonsoftJson](https://www.nuget.org/packages/WebApiClientCore.Extensions.NewtonsoftJson) |  | Newtonsoft 的 Json.NET 扩展包 | |
| 14 | +| [WebApiClientCore.Extensions.JsonRpc](https://www.nuget.org/packages/WebApiClientCore.Extensions.JsonRpc) |  | JsonRpc 调用扩展包 | |
| 15 | +| [WebApiClientCore.OpenApi.SourceGenerator](https://www.nuget.org/packages/WebApiClientCore.OpenApi.SourceGenerator) |  | 将本地或远程 OpenApi 文档解析生成 WebApiClientCore 接口代码的 dotnet tool | |
| 16 | + |
| 17 | +## 声明接口 |
| 18 | + |
| 19 | +```csharp |
| 20 | +[LoggingFilter] |
| 21 | +[HttpHost("http://localhost:5000/")] |
| 22 | +public interface IUserApi |
| 23 | +{ |
| 24 | + [HttpGet("api/users/{id}")] |
| 25 | + Task<User> GetAsync(string id); |
| 26 | + |
| 27 | + [HttpPost("api/users")] |
| 28 | + Task<User> PostAsync([JsonContent] User user); |
| 29 | +} |
| 30 | + |
| 31 | +public class User |
| 32 | +{ |
| 33 | + [JsonPropertyName("account")] |
| 34 | + public string Account { get; set; } = string.Empty; |
| 35 | + |
| 36 | + public string Password { get; set; } = string.Empty; |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## 注册和配置接口 |
| 41 | + |
| 42 | +AspNetCore Startup |
| 43 | + |
| 44 | +```csharp |
| 45 | +public void ConfigureServices(IServiceCollection services) |
| 46 | +{ |
| 47 | + services.AddHttpApi<IUserApi>().ConfigureHttpApi(o => |
| 48 | + { |
| 49 | + o.UseLogging = Environment.IsDevelopment(); |
| 50 | + o.HttpHost = new Uri("http://localhost:5000/"); |
| 51 | + |
| 52 | + // o.JsonSerializeOptions -> json 序列化选项 |
| 53 | + // o.JsonDeserializeOptions -> json 反序列化选项 |
| 54 | + // o.KeyValueSerializeOptions -> 键值对序列化选项 |
| 55 | + // o.XmlSerializeOptions -> xml 序列化选项 |
| 56 | + // o.XmlDeserializeOptions -> xml 反序列化选项 |
| 57 | + // o.GlobalFilters -> 全局过滤器集合 |
| 58 | + }); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Console |
| 63 | + |
| 64 | +```csharp |
| 65 | +public static void Main(string[] args) |
| 66 | +{ |
| 67 | + // 无依赖注入的环境需要自行创建 |
| 68 | + var services = new ServiceCollection(); |
| 69 | + services.AddHttpApi<IUserApi>().ConfigureHttpApi(o => |
| 70 | + { |
| 71 | + o.UseLogging = Environment.IsDevelopment(); |
| 72 | + o.HttpHost = new Uri("http://localhost:5000/"); |
| 73 | + }); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## 全局配置接口 |
| 78 | + |
| 79 | +全局配置可以做为所有接口的默认初始配置,当项目中有很多接口时就很有用。 |
| 80 | + |
| 81 | +```csharp |
| 82 | +public void ConfigureServices(IServiceCollection services) |
| 83 | +{ |
| 84 | + services.AddWebApiClient().ConfigureHttpApi(o => |
| 85 | + { |
| 86 | + o.JsonSerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
| 87 | + o.JsonDeserializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
| 88 | + o.KeyValueSerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
| 89 | + }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## 注入和调用接口 |
| 94 | + |
| 95 | +在Scoped或Transient服务中注入 |
| 96 | + |
| 97 | +```csharp |
| 98 | +public class YourService |
| 99 | +{ |
| 100 | + private readonly IUserApi userApi; |
| 101 | + public YourService(IUserApi userApi) |
| 102 | + { |
| 103 | + this.userApi = userApi; |
| 104 | + } |
| 105 | + |
| 106 | + public async Task GetAsync() |
| 107 | + { |
| 108 | + // 调用接口 |
| 109 | + var user = await this.userApi.GetAsync(id:"id001"); |
| 110 | + ... |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +在Singleton服务中注入 |
| 116 | + |
| 117 | +```csharp |
| 118 | +public class YourService |
| 119 | +{ |
| 120 | + private readonly IServiceScopeFactory serviceScopeFactory; |
| 121 | + public YourService(IServiceScopeFactory serviceScopeFactory) |
| 122 | + { |
| 123 | + this.serviceScopeFactory = serviceScopeFactory; |
| 124 | + } |
| 125 | + |
| 126 | + public async Task GetAsync() |
| 127 | + { |
| 128 | + // 从创建的scope中获取接口实例 |
| 129 | + using var scope = this.serviceScopeFactory.CreateScope(); |
| 130 | + var userApi = scope.ServiceProvider.GetRequiredService<IUserApi>(); |
| 131 | + var user = await userApi.GetAsync(id:"id001"); |
| 132 | + ... |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments