IServiceCollection不存在AddIdentity定义的问题排查
问题描述
我在Infrastructure项目的扩展方法中尝试注册Identity服务时,遇到了AddIdentity方法找不到的错误,但在启动项目的Program.cs里调用该方法却完全正常。
扩展方法代码:
using Domain.Entities; using Infrastructure.Persistence; using Infrastructure.Seeders; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Infrastructure.Extensions; /// <summary> /// 提供用于注册基础设施服务的扩展方法。 /// </summary> public static class ServiceCollectionExtensions { public static void AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { #region DB Connection string? connectionString = configuration.GetConnectionString("DummyDbName"); services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(connectionString)); #endregion #region Register Services services.AddScoped<IRoleSeeder, RoleSeeder>(); #endregion // 此处报错:找不到AddIdentity方法 services.AddIdentity<AppUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); } }
启动项目Program.cs片段:
using Infrastructure.Extensions; using Infrastructure.Seeders; var builder = WebApplication.CreateBuilder(args); { #region Service Collection Extensions builder.Services.AddInfrastructure(builder.Configuration); #endregion builder.Services.AddControllers(); // 此处调用AddIdentity完全正常 //builder.Services.AddIdentity<AppUser, IdentityRole>(); } var app = builder.Build(); { // 创建作用域,运行种子数据 var scope = app.Services.CreateScope(); var roleSeeder = scope.ServiceProvider.GetRequiredService<IRoleSeeder>(); await roleSeeder.SeedRolesAsync(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); }
项目配置:
Infrastructure项目Csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.8" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Application\Application.csproj" /> </ItemGroup> <ItemGroup> <Folder Include="Migrations\" /> </ItemGroup> </Project>
启动项目Csproj
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Application\Application.csproj" /> <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" /> </ItemGroup> <ItemGroup> <Folder Include="Controllers\" /> </ItemGroup> </Project>
问题原因
核心差异在于项目使用的SDK类型:
- 启动项目用的是
Microsoft.NET.Sdk.Web,这个SDK会自动引入Microsoft.AspNetCore.App元包,包含所有ASP.NET Core核心服务扩展(包括AddIdentity所在的类库)。 - Infrastructure项目用的是通用类库SDK
Microsoft.NET.Sdk,不会自动引入ASP.NET Core相关扩展,即使安装了Microsoft.AspNetCore.Identity.EntityFrameworkCore(仅提供EF Core存储实现),也缺少AddIdentity方法所在的扩展类依赖。
解决方案
有两种可行的修复方式:
方式1:给Infrastructure项目添加对应包引用
在Infrastructure项目的Csproj中添加Microsoft.AspNetCore.Identity包,版本和现有ASP.NET Core包保持一致:
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="8.0.10" />
方式2:修改Infrastructure项目的SDK类型
如果该项目专为ASP.NET Core服务设计,直接将项目SDK改为Microsoft.NET.Sdk.Web,这样会自动引入所有ASP.NET Core核心依赖:
修改Infrastructure项目Csproj的第一行:
<Project Sdk="Microsoft.NET.Sdk.Web">
验证
修改后重新编译项目,扩展方法中的AddIdentity即可正常识别和使用。
内容的提问来源于stack exchange,提问作者Jez
相关产品推荐
相关产品推荐

