WEB/API 的 C# 依赖注入
C# Dependency Injection with WEB/API
我和我的一个同事一直在做一个学校项目,老师建议我们使用依赖注入。但是,由于依赖注入,我们收到无法解决的错误。我们使用 React 构建了一个 C# API,并使用 Dotnet Core 3.1 和 EF-core 框架。我们使用工作单元和通用存储库模式构建应用程序。
我们的 IUnitOfWork 看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _Backend_Jelly_.Repositories
{
public interface IUnitOfWork : IDisposable
{
IUserRepository User { get; }
IProjectRepository Project { get; }
Task<int> Complete();
void Dispose();
}
}
并且是这样实现的:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _Backend_Jelly_.Repositories
{
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
public UnitOfWork(DbContext context)
{
_context = context;
User = new UserRepository(_context);
Project = new ProjectRepository(_context);
}
public IUserRepository User { get; private set; }
public IProjectRepository Project { get; private set; }
public async Task<int> Complete()
{
return await _context.SaveChangesAsync();
}
public async void Dispose() => await _context.DisposeAsync();
}
}
通用存储库应该像这样注入 DBContext:
protected DbContext _context;
protected readonly DbSet<TEntity> _set;
public Repository(DbContext context)
{
_context = context;
_set = _context.Set<TEntity>();
}
我们的控制器依赖于 UnitOfWork 的一个实例:
private readonly IUnitOfWork _unitOfWork;
public ProjectController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
然后在 Startup 中我们注入 DBContext 和我们的 UnitOfWork:
services.AddDbContext<JellyContext>(opts => opts.UseLazyLoadingProxies().UseMySql(Configuration["ConnectionString:JellyDB"]));
services.AddScoped<IUnitOfWork, UnitOfWork>();
应用程序抛出错误
完整错误消息:
System.AggregateException: 'Some services are not able to be
constructed (Error while validating the service descriptor
'ServiceType: _Backend_Jelly_.Repositories.IUnitOfWork Lifetime:
Scoped ImplementationType: _Backend_Jelly_.Repositories.UnitOfWork':
Unable to resolve service for type
'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate
'_Backend_Jelly_.Repositories.UnitOfWork'.)'
InvalidOperationException: Unable to resolve service for type
'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate
'_Backend_Jelly_.Repositories.UnitOfWork'.
我们不太清楚为什么。
问题很可能是容器不知道如何处理 DbContext
作为构造函数参数,因为只有 JellyContext
是注册的。
要么更新依赖项class 明确依赖于实际需要并注册到 IoC 容器中的内容
//...
private readonly JellyContext _context;
public UnitOfWork(JellyContext context) {
_context = context;
User = new UserRepository(_context);
Project = new ProjectRepository(_context);
}
//...
或相应地更新服务注册以便容器知道如何处理DbContext
显式注入
services.AddDbContext<DbContext, JellyContext>(opts =>
opts.UseLazyLoadingProxies().UseMySql(Configuration["ConnectionString:JellyDB"]));
我和我的一个同事一直在做一个学校项目,老师建议我们使用依赖注入。但是,由于依赖注入,我们收到无法解决的错误。我们使用 React 构建了一个 C# API,并使用 Dotnet Core 3.1 和 EF-core 框架。我们使用工作单元和通用存储库模式构建应用程序。
我们的 IUnitOfWork 看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _Backend_Jelly_.Repositories
{
public interface IUnitOfWork : IDisposable
{
IUserRepository User { get; }
IProjectRepository Project { get; }
Task<int> Complete();
void Dispose();
}
}
并且是这样实现的:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _Backend_Jelly_.Repositories
{
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
public UnitOfWork(DbContext context)
{
_context = context;
User = new UserRepository(_context);
Project = new ProjectRepository(_context);
}
public IUserRepository User { get; private set; }
public IProjectRepository Project { get; private set; }
public async Task<int> Complete()
{
return await _context.SaveChangesAsync();
}
public async void Dispose() => await _context.DisposeAsync();
}
}
通用存储库应该像这样注入 DBContext:
protected DbContext _context;
protected readonly DbSet<TEntity> _set;
public Repository(DbContext context)
{
_context = context;
_set = _context.Set<TEntity>();
}
我们的控制器依赖于 UnitOfWork 的一个实例:
private readonly IUnitOfWork _unitOfWork;
public ProjectController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
然后在 Startup 中我们注入 DBContext 和我们的 UnitOfWork:
services.AddDbContext<JellyContext>(opts => opts.UseLazyLoadingProxies().UseMySql(Configuration["ConnectionString:JellyDB"]));
services.AddScoped<IUnitOfWork, UnitOfWork>();
应用程序抛出错误
完整错误消息:
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: _Backend_Jelly_.Repositories.IUnitOfWork Lifetime: Scoped ImplementationType: _Backend_Jelly_.Repositories.UnitOfWork': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate '_Backend_Jelly_.Repositories.UnitOfWork'.)'
InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate '_Backend_Jelly_.Repositories.UnitOfWork'.
我们不太清楚为什么。
问题很可能是容器不知道如何处理 DbContext
作为构造函数参数,因为只有 JellyContext
是注册的。
要么更新依赖项class 明确依赖于实际需要并注册到 IoC 容器中的内容
//...
private readonly JellyContext _context;
public UnitOfWork(JellyContext context) {
_context = context;
User = new UserRepository(_context);
Project = new ProjectRepository(_context);
}
//...
或相应地更新服务注册以便容器知道如何处理DbContext
显式注入
services.AddDbContext<DbContext, JellyContext>(opts =>
opts.UseLazyLoadingProxies().UseMySql(Configuration["ConnectionString:JellyDB"]));