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

如何默认禁用AppService所有API,仅启用指定接口?

实现ABP AppService默认禁用所有API,仅开放指定接口

你提到的重写AbpServiceConvention的ConfigureSelector方法完全可行,这正是ABP框架预留的扩展点,用来自定义远程服务的选择逻辑。下面是具体实现方案:

1. 自定义服务约定类

创建继承自AbpServiceConvention的类,重写ConfigureSelector方法调整远程服务选择规则:

using Volo.Abp.DependencyInjection;
using Volo.Abp.RemoteServices;
using System.Reflection;

public class CustomServiceConvention : AbpServiceConvention
{
    protected override void ConfigureSelector(ServiceConventionSelectorConfiguration context)
    {
        base.ConfigureSelector(context);

        // 添加自定义选择逻辑,优先级高于默认规则
        context.Selectors.Add(methodInfo =>
        {
            // 获取方法上的RemoteService特性
            var methodAttr = methodInfo.GetCustomAttribute<RemoteServiceAttribute>();
            // 获取类上的RemoteService特性
            var classAttr = methodInfo.DeclaringType?.GetCustomAttribute<RemoteServiceAttribute>();

            // 规则:方法上的特性优先,明确设置则按方法配置执行
            if (methodAttr != null)
            {
                return methodAttr.IsEnabled;
            }

            // 方法无标记时,继承类的配置;类也无标记时保持框架默认(启用所有API)
            return classAttr?.IsEnabled ?? true;
        });
    }
}

2. 替换默认服务约定

在你的模块类中,将默认的IAbpServiceConvention替换为自定义实现:

using Volo.Abp.Modularity;
using Volo.Abp.RemoteServices;

[DependsOn(typeof(AbpAspNetCoreModule))]
public class YourApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        // 替换默认服务约定
        context.Services.Replace(ServiceDescriptor.Transient<IAbpServiceConvention, CustomServiceConvention>());
    }
}

3. 按期望方式使用

现在可以直接在AppService类上标记[RemoteService(false)],仅给需要开放的方法添加[RemoteService(true)]:

[RemoteService(false)]
public class SampleAppService : ApplicationService
{
    [RemoteService(true)]
    public string GetStringNeedEnable()
    {
        return "1";
    }

    public string GetADisEnable()
    {
        return "1";
    }

    public string GetBDisEnable()
    {
        return "1";
    }

    // 其他无需开放的方法无需额外标记
}

逻辑说明

  • 方法上的RemoteService特性优先级最高,明确设置则按方法配置执行
  • 方法无标记时,继承类上的RemoteService配置
  • 类也无标记时,保持ABP默认行为(启用所有API)

这样就实现了批量禁用、按需启用的效果,无需再给每个不需要开放的方法逐个添加特性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 22:38:23