using System;
using System.Net.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Polly;
using Volo.Abp.AspNetCore.Mvc.Client;
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Domain;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client.IdentityModel.Web;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.Swashbuckle;
using Volo.Abp.Users;
using Win_in.Sfs.Auth;
using Win_in.Sfs.Basedata.Application.Contracts;
using Win_in.Sfs.FileStorage;
using Win_in.Sfs.Label.Application.Contracts;
using Win_in.Sfs.Message.Application.Contracts;
using Win_in.Sfs.Shared.Host;
using Win_in.Sfs.Wms.Inventory.Application.Contracts;
using Win_in.Sfs.Wms.Store.Application.Contracts;
namespace Win_in.Sfs.Wms.Dashboard.Host;
[DependsOn(
typeof(AbpIdentityHttpApiClientModule),
typeof(AbpAspNetCoreMvcClientModule),
typeof(AbpHttpClientIdentityModelWebModule),
typeof(SharedHostModule),
typeof(AbpSettingManagementEntityFrameworkCoreModule)
)]
///
///
///
[DependsOn(
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
typeof(AbpAutofacModule),
typeof(AbpAspNetCoreSerilogModule),
typeof(AbpSwashbuckleModule),
typeof(AbpHttpClientModule),
typeof(AbpAutoMapperModule)
)]
[DependsOn(
typeof(AbpIdentityApplicationContractsModule),
typeof(BasedataApplicationContractsModule),
typeof(InventoryApplicationContractsModule),
typeof(StoreApplicationContractsModule),
typeof(LabelApplicationContractsModule),
typeof(MessageApplicationContractsModule),
typeof(AuthApplicationContractsModule)
)]
public class DashboardHttpApiHostModule : ModuleBase
{
///
/// 预配置服务
///
///
public override void PreConfigureServices(ServiceConfigurationContext context)
{
/*
context.Services.Configure(options =>
{
var configuration = context.Services.GetConfiguration();
var remoteServiceBaseUrl = configuration["RemoteServices:Default:BaseUrl"];
options.RemoteServices.Default = new RemoteServiceConfiguration(remoteServiceBaseUrl);
});
*/
context.Services.AddAutoMapperObjectMapper();
Configure(options => { options.AddMaps(validate: true); });
PreConfigure(options =>
{
//Polly 重试3次
options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) =>
{
clientBuilder.AddTransientHttpErrorPolicy(policyBuilder =>
policyBuilder.WaitAndRetryAsync(
3,
i => TimeSpan.FromSeconds(Math.Pow(2, i))
)
);
});
//默认添加Authorization Header: Bearer Token
options.ProxyClientActions.Add((a, s, h) =>
{
var httpAuthorizationHandler = s.GetService();
if (httpAuthorizationHandler != null && httpAuthorizationHandler.IsLoggedIn())
{
h.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", httpAuthorizationHandler.GetCurrentBearer());
}
});
});
}
public override void PostConfigureServices(ServiceConfigurationContext context)
{
// 这里必须手动替换一下
context.Services.Replace(ServiceDescriptor.Transient(typeof(IExternalUserLookupServiceProvider), typeof(HttpClientExternalUserLookupServiceProvider)));
context.Services.Replace(ServiceDescriptor.Transient());
}
protected override void ConfigureHttpClientProxies()
{
var context = this.ServiceConfigurationContext;
context.Services.AddHttpClientProxies(
typeof(AuthApplicationContractsModule).Assembly,
"Auth"
);
context.Services.AddHttpClientProxies(
typeof(MessageApplicationContractsModule).Assembly,
"Message"
);
context.Services.AddHttpClientProxies(
typeof(BasedataApplicationContractsModule).Assembly,
"BaseData"
);
context.Services.AddHttpClientProxies(
typeof(InventoryApplicationContractsModule).Assembly,
"Inventory"
);
context.Services.AddHttpClientProxies(
typeof(StoreApplicationContractsModule).Assembly,
"Store"
);
context.Services.AddHttpClientProxies(
typeof(LabelApplicationContractsModule).Assembly,
"Label"
);
context.Services.AddHttpClientProxies(
typeof(AbpIdentityApplicationContractsModule).Assembly,
"Default"
);
context.Services.AddHttpClientProxies(
typeof(FileStorageApplicationContractsModule).Assembly,
"FileStorage"
);
}
}