1
chenhaozhe
2026-11-11 e8e5bda49946c89411f2231c2466c10597a7c7fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Microsoft.Extensions.Hosting.WindowsServices;
using APIGetway.Filter;
 
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    Args = args,
    // IIS 进程内托管时工作目录不是站点目录,固定为程序目录才能读到 appsettings.json
    ContentRootPath = AppContext.BaseDirectory
});
 
builder.Services.AddWindowsService(options => options.ServiceName = "APIGetway");
 
// YARP 反向代理
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
 
var app = builder.Build();
 
// 拦截对 DLL/LOG 等敏感后缀的访问,直接返回 406(需在静态文件与反向代理之前)
app.UseFileExtensionFilter();
 
app.UseUpstreamErrorPageFilter();
 
app.UseDefaultFiles();
app.UseStaticFiles();
 
// YARP 转发:按 appsettings.json 中的 Routes 规则转发到后端
app.MapReverseProxy();
 
app.Run();