You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

独立DLL项目中定义的Fast Endpoint无法调用及Swagger不显示问题

解决Fast Endpoints在外部DLL中无法被识别的问题

问题根源

FastEndpoints默认仅扫描**启动项目(SharePrompts.Web)**中的端点类,不会自动遍历引用的外部DLL,因此你的CreateEndpoint无法被注册到路由系统,也不会出现在Swagger中。

解决方案

1. 配置FastEndpoints扫描外部程序集

在主Web项目的Program.cs中,修改AddFastEndpoints的配置,明确指定要扫描的程序集:

builder.Services.AddFastEndpoints(config =>
{
    // 添加外部DLL的程序集到扫描列表
    config.SearchAssemblies(typeof(SharePrompts.Web.Cap.CreateEndpoint).Assembly);
    // 同时保留对启动项目自身程序集的扫描
    config.SearchAssemblies(typeof(Program).Assembly);
});

或者更简洁的写法,一次性添加多个程序集:

builder.Services.AddFastEndpoints(config =>
{
    config.Assemblies = new[]
    {
        typeof(Program).Assembly,
        typeof(SharePrompts.Web.Cap.CreateEndpoint).Assembly
    };
});

2. 确保Swagger包含外部DLL的端点

如果需要在Swagger中显示这些端点,需要让Swagger生成器扫描外部程序集的相关类型:

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "SharePrompts API", Version = "v1" });

    // 可选:添加外部DLL的XML注释(如果启用了注释生成)
    var externalXmlFile = $"{typeof(SharePrompts.Web.Cap.CreateEndpoint).Assembly.GetName().Name}.xml";
    var externalXmlPath = Path.Combine(AppContext.BaseDirectory, externalXmlFile);
    if (File.Exists(externalXmlPath))
    {
        c.IncludeXmlComments(externalXmlPath);
    }
});

同时确保FastEndpoints的Swagger集成已正确启用——默认情况下,只要添加了AddFastEndpoints和AddSwaggerGen,并在中间件中调用了UseSwagger()和UseSwaggerUI()即可。

3. 检查端点类的访问修饰符

确认CreateEndpoint类是public访问级别,如果是internal,扫描程序集时会无法发现该类。

4. 排查路由冲突

检查主Web项目中是否存在其他端点使用了相同的/posts路由路径,若有冲突,新端点会被覆盖导致404。可以暂时修改测试路由(比如/posts/test)验证是否能正常访问。


内容的提问来源于stack exchange,提问作者Dmitry Stepanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:12:46