【愚公系列】2022年02月 .NET架构班 010-ABP vNext 动态C# API客户端
【摘要】 一、Httpclient调用OAModule 模块类添加扩展AddHttpClientpublic class OAModule : AbpModule{ public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddControl...
一、Httpclient调用
OAModule 模块类添加扩展AddHttpClient
public class OAModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddControllers();
context.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "XT.ABP.Web", Version = "v1" });
});
context.Services.AddHttpClient(); // 把httpclient加载IOC
}
}
调用
public HttpClient _httpClient { set; get; }
#region 1、httpClient调用
{
//1、查询请求
var httpResponseMessage = _httpClient.GetAsync("https://localhost:44389/api/EBusiness/product").Result;
string productContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
// 2、json转换成对象
var products = JsonConvert.DeserializeObject<PagedResultDto<ProductDto>>(productContent);
return products;
}
#endregion
二、动态C# API客户端
1、在电商系统中的YDT.EBusiness.HttpApi.Client模块中的EBusinessHttpApiClientModule引入
[DependsOn(
typeof(EBusinessApplicationContractsModule),
typeof(AbpAccountHttpApiClientModule),
typeof(AbpIdentityHttpApiClientModule),
typeof(AbpPermissionManagementHttpApiClientModule),
typeof(AbpTenantManagementHttpApiClientModule),
typeof(AbpFeatureManagementHttpApiClientModule),
typeof(AbpSettingManagementHttpApiClientModule)
)]
public class EBusinessHttpApiClientModule : AbpModule
{
public const string RemoteServiceName = "EBusiness";
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpHttpClientBuilderOptions>(options =>
{
options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) =>
{
clientBuilder.AddTransientHttpErrorPolicy(policyBuilder =>
policyBuilder.WaitAndRetryAsync(
3,
i => TimeSpan.FromSeconds(Math.Pow(2, i))
)
);
});
});
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddHttpClientProxies(
typeof(EBusinessApplicationContractsModule).Assembly,
RemoteServiceName
);
}
}
2、在OA系统中引入EBusinessHttpApiClientModule模块并在配置文件appsettings.json中添加
"RemoteServices": {
"EBusiness": {
"BaseUrl": "https://localhost:44389/"
},
"ERP": {
"BaseUrl": "https://localhost:44389/"
},
"OA": {
"BaseUrl": "https://localhost:44389/"
}
}
3、然后在OA系统中添加项目引用引入YDT.EBusiness.HttpApi.Client
4、然后在OA系统中OAModule 类上引入
[DependsOn(typeof(EBusinessHttpApiClientModule))]
public class OAModule : AbpModule
{}
5、然后使用OA系统中OAController中引入
/// <summary>
/// Web API控制器
/// </summary>
[ApiController]
[Route("[controller]")]
public class OAController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public IProductAppService _productAppService { set; get; }
/// <summary>
/// 1、获取商品API
/// 总结:如果使用的ABP vNext HttpClient开发的项目,就必须使用动态API客户端远程。
/// 1、只能C#语音调用。不能夸语言
/// 2、如果OA是Java,就无法调用了。
/// gRPC来实现。
/// </summary>
/// <returns></returns>
[HttpGet]
public PagedResultDto<ProductDto> Get()
{
#region 1、httpClient调用
{
/*//1、查询请求
var httpResponseMessage = _httpClient.GetAsync("https://localhost:44389/api/EBusiness/product").Result;
string productContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
// 2、json转换成对象
var products = JsonConvert.DeserializeObject<PagedResultDto<ProductDto>>(productContent);
return products;*/
}
#endregion
#region 2、ProductService直接调用
{
//1、查询请求
return _productService.GetProducts();
}
#endregion
#region 2、动态API调用
{
var products = _productAppService.GetListAsync(new PagedAndSortedResultRequestDto()).Result;
return products;
}
#endregion
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)