中 | EN
Install-Package Masa.Utils.Caller.HttpClient
-
修改
Program.cs
builder.Services.AddCaller(options => { options.UseHttpClient(httpClientBuilder => { httpClientBuilder.Name = "UserCaller";// 当前Caller的别名,仅存在一个HttpClient时,可以不对Name赋值 httpClientBuilder.BaseAddress = "http://localhost:5000" ; }); });
-
如何使用:
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) => userCallerProvider.GetAsync<string>($"/Hello", new { Name = name }));
完整请求的接口地址是:http://localhost:5000/Hello?Name={name}
-
当存在多个HttpClient时,则修改
Program.cs
为builder.Services.AddCaller(options => { options.UseHttpClient(httpClientBuilder => { httpClientBuilder.Name = "UserCaller"; httpClientBuilder.BaseAddress = "http://localhost:5000" ; }); options.UseHttpClient(httpClientBuilder => { httpClientBuilder.Name = "OrderCaller"; httpClientBuilder.BaseAddress = "http://localhost:6000" ; }); });
-
如何使用UserCaller或OrderCaller
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) => userCallerProvider.GetAsync<string>($"/Hello", new { Name = name })); app.MapGet("/Test/Order/Hello", ([FromServices] ICallerFactory callerFactory, string name) => { var callerProvider = callerFactory.CreateClient("OrderCaller"); return callerProvider.GetAsync<string>($"/Hello", new { Name = name }); });
当多个Caller被添加时,如何获取指定的Caller?
通过
CallerFactory
的CreateClient
方法得到指定别名的CallerProvider为什么
userCallerProvider
没有通过CallerFactory
的CreateClient
方法得到对应的Caller?如果未指定默认的ICallerProvider,则在
AddCaller
方法中第一个被添加的就是默认的CallerProvider
-
修改
Program.cs
builder.Services.AddCaller();
-
新增加类
UserCaller
public class UserCaller: HttpClientCallerBase { protected override string BaseAddress { get; set; } = "http://localhost:5000"; public HttpCaller(IServiceProvider serviceProvider) : base(serviceProvider) { } public Task<string> HelloAsync(string name) => CallerProvider.GetStringAsync($"/Hello", new { Name = name }); /// <summary> /// 默认不需要重载,对httpClient有特殊需求时可重载 /// </summary> /// <param name="httpClient"></param> protected override void ConfigureHttpClient(System.Net.Http.HttpClient httpClient) { httpClient.Timeout = TimeSpan.FromSeconds(5); } }
-
如何使用UserCaller
app.MapGet("/Test/User/Hello", ([FromServices] UserCaller caller, string name) => caller.HelloAsync(name));