using System.Security.Authentication; using Fake.AspNetCore; using Fake.EventBus.RabbitMQ; using Fake.Modularity; using Fake.ObjectMapping.AutoMapper; using Fake.ObjectStorage; using Fake.RabbitMQ; using FreeRedis; using InterfaceForward.Application.Contracts; using InterfaceForward.Application.Contracts.ForwardCore; using InterfaceForward.Application.ForwardCore; using InterfaceForward.Application.Helpers; using InterfaceForward.Application.HostServices; using InterfaceForward.Domain.Shared; using InterfaceForward.Repositories; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace InterfaceForward.Application; [DependsOn(typeof(FakeRabbitMqModule))] [DependsOn(typeof(FakeAspNetCoreModule))] [DependsOn(typeof(FakeObjectMappingAutoMapperModule))] [DependsOn(typeof(FakeEventBusRabbitMqModule))] [DependsOn(typeof(FakeObjectStorageTencentCosModule))] [DependsOn(typeof(InterfaceForwardRepositoriesModule))] [DependsOn(typeof(InterfaceForwardDomainSharedModule))] public class InterfaceForwardApplicationModule:FakeModule { public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); context.Services.Configure(options => { options.ScanProfiles(); }); context.Services.Configure(configuration.GetSection("InterfaceRelay")); context.Services.AddHostedService(); var options = configuration.GetSection("Redis").Get(); if (options == null) throw new FakeException("未找到Redis相关配置,请检查配置文件中是否包含名为Redis的节点"); RedisHelper.Init(options, showLogInfo: System.Diagnostics.Debugger.IsAttached); // 预热并验证动态脚本编译环境。 PreheatNatasha(); context.Services.AddHttpClient("forward", client => { client.Timeout = TimeSpan.FromSeconds(20); }) .ConfigurePrimaryHttpMessageHandler(_ => { var httpClientHandler = new HttpClientHandler(); // 确保支持较新的 SSL/TLS 版本 httpClientHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13; // 忽略 SSL/TLS 证书错误 httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; return httpClientHandler; }); } static void PreheatNatasha() { // aspnet 非 root 时 /app 不可写;k8s 已在 api.yaml 挂 emptyDir → /app/DynamicLibraryFolders Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "DynamicLibraryFolders")); NatashaManagement.Preheating(false, false); var context = new ForwardCoreContext(); NatashaScriptCompiler.CompileAction(""" obj.FormFieldList.Add(new InterfaceForward.Domain.Shared.Dtos.InterfaceFormFieldDto { Name = "_natasha_health", Value = "ok" }); """)(context); NatashaScriptCompiler.CompileAsyncFunc(""" //async await System.Threading.Tasks.Task.CompletedTask; obj.FormFieldList.Add(new InterfaceForward.Domain.Shared.Dtos.InterfaceFormFieldDto { Name = "_natasha_async_health", Value = "ok" }); """)(context).GetAwaiter().GetResult(); } }