运行时加载Microsoft.Extensions.Logging.Abstractions程序集失败求助
针对你遇到的程序集加载错误,尝试以下排查步骤:
统一NuGet包版本
Azure Functions对依赖版本一致性要求严格,打开NuGet包管理器,检查Microsoft.Extensions.Logging.Abstractions、Microsoft.EntityFrameworkCore.SqlServer、Microsoft.Azure.Functions.Extensions等相关包的版本,确保所有包对齐到同一大版本(比如Functions v4对应.NET 6/7/8,相关包使用6.x/7.x/8.x版本),避免跨版本依赖冲突。清理重建项目
- 删除项目根目录下的
bin和obj文件夹 - 在Visual Studio中执行
Build -> Clean Solution,再执行Build -> Rebuild Solution - 清除NuGet本地缓存:运行命令
dotnet nuget locals all --clear,或在NuGet包管理器设置中手动清空
- 删除项目根目录下的
优化Startup配置代码
将AddDbContext的写法简化为常规扩展方法调用,避免潜在版本兼容问题:builder.Services.AddDbContext<ApplicationDbContext>( options => options.UseSqlServer(connectionString));同时确认
ApplicationDbContext的构造函数正确接收DbContextOptions<ApplicationDbContext>参数:public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } // 你的DbSet和其他配置 }检查Functions运行时版本
打开项目的local.settings.json,确保FUNCTIONS_EXTENSION_VERSION设置正确,比如v4版本对应.NET 6+:{ "IsEncrypted": false, "Values": { "FUNCTIONS_EXTENSION_VERSION": "~4", // 其他配置项 } }重装相关NuGet包
先卸载Microsoft.Extensions.Logging.Abstractions、Microsoft.EntityFrameworkCore.SqlServer、Microsoft.Azure.Functions.Extensions这些包,再重新安装对应版本的包,确保安装过程无报错。
原问题中的Startup代码:
[assembly: FunctionsStartup(typeof(HiQFoodCourt.Functions.StartUp))] namespace HiQFoodCourt.Functions { public class StartUp : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; builder.Services.AddDbContext<ApplicationDbContext>( options => SqlServerDbContextOptionsExtensions.UseSqlServer(options, connectionString)); var mapperConfig = new MapperConfiguration(x => { x.AddProfile(new MappingProfile()); }); IMapper mapper = mapperConfig.CreateMapper(); builder.Services.AddSingleton(mapper); } } }
错误截图:
内容的提问来源于stack exchange,提问作者LordAhune

