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

Blazor类库服务DI注入报错:如何注册依赖DbContext工厂的仓储?

问题解决:Blazor类库服务DI注入失败

错误根源

你遇到的错误看似指向SomeService未注册,但实际是依赖链中的IGPSFixRepository接口与实现类未在DI容器中注册。DI容器在创建SomeService实例时,无法找到IGPSFixRepository的对应实现,进而抛出异常。

解决步骤

在主应用的Program.cs中,添加IGPSFixRepository与GPSFixRepository的注册代码即可:

// 已有的注册代码
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionStringIdentity));
builder.Services.AddScoped<ISomeService, SomeService>();

// 新增:注册仓储接口与实现
builder.Services.AddScoped<IGPSFixRepository, GPSFixRepository>();

说明

  • GPSFixRepository的构造函数依赖IDbContextFactory<ApplicationDbContext>,而该工厂已经在你的代码中完成注册,DI容器会自动解析并注入这个依赖。
  • 确保Program.cs中已添加类库的命名空间引用,否则会找不到IGPSFixRepository和GPSFixRepository类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 19:10:59