项目升级到.Net8.0 Autofac引发诡异的问题

前两天把项目升级到.Net8.0了,把.Net框架升级了,其他一些第三方库升级了一部分,升级完以后项目跑不起来了,报如下错误:

复制代码
An unhandled exception occurred while processing the request.
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider' can be invoked with the available services and parameters:
Cannot resolve parameter 'Microsoft.Extensions.DependencyInjection.IServiceProviderIsService serviceProviderIsService' of constructor 'Void .ctor(Microsoft.AspNetCore.Routing.EndpointDataSource, Microsoft.Extensions.Hosting.IHostEnvironment, Microsoft.AspNetCore.Routing.ParameterPolicyFactory, Microsoft.Extensions.DependencyInjection.IServiceProviderIsService)'.
Autofac.Core.Activators.Reflection.ReflectionActivator.GetAllBindings(ConstructorBinder[] availableConstructors, IComponentContext context, IEnumerable<Parameter> parameters)

DependencyResolutionException: An exception was thrown while activating Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator -> Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider -> λ:Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider[] -> Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.

看着是依赖注入的问题,后面去网上找了一下,找到博客园的一篇文章,提到是vs版本的问题,我现在用的vs版本是17.11.5,看完以后心里想vs应该不会和影响到代码,我想着通过命令行 dotnet xxx.dll 能不能启动,奇怪了命令行可以启动;接着我又用vscode试了一下,也可以启动,这就很奇怪了。

我把项目.Net6.0的代码签出项目可以跑起来,看来还是升级的问题,和依赖注入有关,后面想到了可能是autofac没有升级,把autofac升级到10.0就没问题了。

问题虽然解决了,但还是很纳闷,怎么不升级autofac,vs会导致项目跑不起来了,但是通过命令行和vscode又可以。

相关推荐
geovindu1 天前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
驰骋工作流1 天前
请假流程:六款.NET工作流引擎实现方式对比
.net·ccflow·工作流引擎二开·.net工作流引擎对比
WarPigs1 天前
.NET项目app.Config笔记
c#·.net
海盗12342 天前
微软技术周报——2026-07-22
microsoft·c#·.net
海盗12342 天前
微软技术日报 ——2026-07-21
microsoft·c#·.net
半亩码田2 天前
【.NET新特性·第8篇】.NET 9 AI 构建基块:Microsoft.Extensions.AI
人工智能·microsoft·.net
Ctrl+Z侠3 天前
.NET WebApi Windows / Linux Docker 全链路压测与瓶颈定位 0 到 1 教程
linux·windows·.net
慧都小妮子3 天前
Aspose.CAD for .NET 26.6 更新发布
pdf·.net·3d渲染·cad·pdf导出·ifc编辑
夜莺悠吟3 天前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net