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

ABP Framework 6.0(C#)使用AddRepositories时依赖注入失败问题

ABP Framework独立模块仓储注册异常及解决方法

我在基于ABP Framework的独立模块「Freeswitch」中尝试注册仓储接口IFSExtensionRepository,最初使用ABP内置的DbContext仓储注册方式时出现依赖注入异常,改用直接注册Transient服务后问题解决。

初始仓储注册代码

context.Services.AddAbpDbContext<FreeswitchDBContext>(options =>
{
    options.AddRepository<FSExtension, EfCoreExtensionRepository>();
});

Freeswitch数据库上下文代码

[ConnectionStringName(FreeswitchDbProperties.ConnectionStringName)]
public class FreeswitchDbContext : AbpDbContext<FreeswitchDbContext>, IFreeswitchDbContext
{
    /* Add DbSet for each Aggregate Root here. Example:
     * public DbSet<Question> Questions { get; set; }
     */
    public DbSet<FSExtension> Extension { get; set; }

    public FreeswitchDbContext(DbContextOptions<FreeswitchDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.ConfigureFreeswitch();
    }
}

仓储实现代码

public class ExtensionRepositoryEf : EfCoreRepository<IFreeswitchDbContext, FSExtension, Guid>, IFSExtensionRepository
{
    private readonly ICurrentTenant CurrentTenant;
    public ExtensionRepositoryEf(IDbContextProvider<IFreeswitchDbContext> dbContextProvider, ICurrentTenant currentTenant) : base(dbContextProvider)
    {
        CurrentTenant = currentTenant;
    }

    public virtual async Task<FSExtension> FindByNameAsync(string extensionName, CancellationToken canceltoken = default)
    {
        return await (await GetDbSetAsync())
        .OrderBy(t => t.Id)
        .FirstOrDefaultAsync(t => t.ExtensionNumber.ToLower() == extensionName.ToLower() && t.TenantId == CurrentTenant.Id, GetCancellationToken(canceltoken));
    }
}

异常信息

当注入IFSExtensionRepository的类被调用时,抛出依赖注入异常,异常链如下:

---------- Exception Data ----------
ActivatorChain = Castle.Proxies.RinglurUserServiceProxy -> RinglurNet.Ringlur.RinglurUserManager -> Freeswitch.Service.ExtensionManager

解决方法

改用直接注册Transient服务的方式,问题得以解决:

context.Services.AddTransient<IFSExtensionRepository, ExtensionRepositoryEf>();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:15:40