c# - 整洁架构和 Asp.Net 核心标识

标签 c# asp.net-core entity-framework-core asp.net-core-webapi clean-architecture

我正在尝试从我的应用程序中提取 Asp.Net Core Identity 以尊重 清洁架构 .
目前,我的项目分为 4 个项目:WebApi、Infrastructure、Application 和 Core。我希望将 Asp.Net EF Core 和 Asp.Net Core Identity 的所有配置封装到 Infrastructure 项目中。通过应用程序项目中定义的一些接口(interface)(例如 IApplicationDbcontextIUserServiceICurrentUserService),这两个服务都将暴露给 WebApi 项目。
不幸的是,我无法使用包管理器命令创建迁移:Add-Migration -Project src\Infrastructure -StartupProject src\WebApi -OutputDir Persistence\Migrations "SmartCollaborationDb_V1" .
错误:Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 .
你能帮助我吗?
解决方案结构
Solution Structure
src\WebApi\Startup.cs

public class Startup {

        public IConfiguration Configuration { get; }


        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }


        public void ConfigureServices(IServiceCollection services) {
            services.AddApplication(Configuration);
            services.AddInfrastructure(Configuration);

services.AddHttpContextAccessor();
            ...

            services.AddScoped<ICurrentUserService, CurrentUserService>();
        }


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
           ...
        }
    }
src\Infrastructure\DependencyInjection.cs
public static class DependencyInjection {

        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration config) {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    config.GetConnectionString("DefaultConnection"),
                    context => context.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName)));

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
            services.AddTransient<IDateTimeService, DateTimeService>();
            services.AddTransient<IUserService, UserService>();

            return services;
        }
    }
src\Infrastructure\Persistence\ApplicationDbContext.cs
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>, IApplicationDbContext {
        private readonly ICurrentUserService currentUserService;
        private readonly IDateTimeService dateTimeService;

        public DbSet<Student> Students { get; set; }
        public DbSet<Group> Groups { get; set; }
        public DbSet<Course> Courses { get; set; }

        public ApplicationDbContext(
            DbContextOptions options,
            ICurrentUserService currentUserService,
            IDateTimeService dateTimeService) :
            base(options) {
            this.currentUserService = currentUserService;
            this.dateTimeService = dateTimeService;
        }

        protected override void OnModelCreating(ModelBuilder builder) {
            builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

            base.OnModelCreating(builder);
        }


        public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) {
            UpdateAuditableEntities();

            return base.SaveChangesAsync(cancellationToken);
        }


        private void UpdateAuditableEntities() {
            foreach (var entry in ChangeTracker.Entries<AuditableEntity>()) {
                switch (entry.State) {
                    case EntityState.Added:
                        entry.Entity.CreatedBy = currentUserService.UserId.ToString();
                        entry.Entity.Created = dateTimeService.Now;
                        break;

                    case EntityState.Modified:
                        entry.Entity.LastModifiedBy = currentUserService.UserId.ToString();
                        entry.Entity.LastModified = dateTimeService.Now;
                        break;
                }
            }
        }
    }
编辑#01
src\WebApi\Services\CurrentUserService.cs
    public class CurrentUserService : ICurrentUserService {
        public Guid UserId { get; }
        public bool IsAuthenticated { get; }
    
        public CurrentUserService(IHttpContextAccessor httpContextAccessor) {
            var claim = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
    
            IsAuthenticated = claim != null;
            UserId = IsAuthenticated ? Guid.Parse(claim) : Guid.Empty;
        }
    }

最佳答案

您的代码应该(并且确实)通常可以正常工作,并且不需要 IDesignTimeDbContextFactory<DbContext>派生类。
我上传了 minimal project到 GitHub,它模仿您的设计,并且可以使用以下包管理器控制台命令毫无问题地创建迁移:Add-Migration -Project "Infrastructure" -StartupProject "WebApi" -OutputDir Persistence\Migrations "Initial"从这往哪儿走
先来看看Design-time DbContext Creation , 了解 EF Core 如何寻找您的 DbContext派生类。
然后把 Debugger.Launch() (和 Debugger.Break() )指令,在执行 Add-Migration 时触发 JIT 调试器命令。
最后,单步执行您的代码。确保您的 DependencyInjection.AddInfrastructure() , ApplicationDbContext.ApplicationDbContext() , ApplicationDbContext.OnModelCreating()等方法按预期调用。
您可能还希望在调试时让您的 IDE 中断任何引发的异常。
您的问题可能与与 EF Core 完全无关的事情有关,在可以实例化上下文之前就出错了。好像不是CurrentUserService构造函数,但它可能是 IDateTimeService 的构造函数实现类或在初始化过程中运行的其他东西。
您应该能够在单步抛出代码时找出答案。
更新:问题和解决方案
正如预期的那样,该问题与 EF Core 无关。 AddFluentValidation()方法抛出以下异常:

System.NotSupportedException: The invoked member is not supported in a dynamic assembly.
  at at System.Reflection.Emit.InternalAssemblyBuilder.GetExportedTypes()
  at FluentValidation.AssemblyScanner.FindValidatorsInAssembly(Assembly assembly) in /home/jskinner/code/FluentValidation/src/FluentValidation/AssemblyScanner.cs:49
  at FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssembly(IServiceCollection services, Assembly assembly, ServiceLifetime lifetime) in /home/jskinner/code/FluentValidation/src/FluentValidation.DependencyInjectionExtensions/ServiceCollectionExtensions.cs:48
  at FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssemblies(IServiceCollection services, IEnumerable`1 assemblies, ServiceLifetime lifetime) in /home/jskinner/code/FluentValidation/src/FluentValidation.DependencyInjectionExtensions/ServiceCollectionExtensions.cs:35
  at FluentValidation.AspNetCore.FluentValidationMvcExtensions.AddFluentValidation(IMvcBuilder mvcBuilder, Action`1 configurationExpression) in /home/jskinner/code/FluentValidation/src/FluentValidation.AspNetCore/FluentValidationMvcExtensions.cs:72
  at WebApi.Startup.ConfigureServices(IServiceCollection services) in E:\Sources\SmartCollaboration\WebApi\Startup.cs:52
  at at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
  at at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  at at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
  at at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
  at at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
  at at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
  at at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
  at at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
  at at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
  at at Microsoft.Extensions.Hosting.HostBuilder.Build()
处理此问题的一种方法是仅检测是否从 EF Core 工具调用代码,并仅设置必要的服务,如果是这样的话:
public void ConfigureServices(IServiceCollection services)
{
    Debugger.Launch(); // <-- Remove this after debugging! 

    services.AddApplication(Configuration);
    services.AddInfrastructure(Configuration);

    services.AddScoped<ICurrentUserService, CurrentUserService>();

    if (new StackTrace()
        .GetFrames()
        .Any(f => f?.GetMethod()?.DeclaringType?.Namespace == "Microsoft.EntityFrameworkCore.Tools"))
    {
        // Called by EF Core design-time tools.
        // No need to initialize further.
        return;
    }

    services.AddSwaggerGen(options => {
        options.SwaggerDoc("v1", new OpenApiInfo {
            Version = "v1",
            Title = "SmartCollaboration API"
        });
        options.AddFluentValidationRules();
    });

    services.AddHttpContextAccessor();

    services.AddControllers().AddFluentValidation(options =>
        options.RegisterValidatorsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies()));
}

关于c# - 整洁架构和 Asp.Net 核心标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865530/

相关文章:

dependency-injection - 是否可以通过约定在.NET Core中注册依赖项?

asp.net - 部署阶段/生产应用程序设置 Asp.net Core

entity-framework - EF7 (EFCore) 支持枚举吗?

entity-framework-core - 使用 Entity.CurrentValues.SetValues 更新导航属性

c# - 如何使用 EF Core 更新 ASP.NET 5 中的 dbcontext 脚手架?

c# - Unity 监听器/事件,例如 GameObject 属性的 OnValueChanged

c# - 对象上下文、存储库和事务

c# - 将固定长度文件中的数据读入类对象

c# - 对象引用未设置为对象的实例EDIT

c# - Json 无法解析 ajax post 请求