You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 6.0自定义异常处理报错:找不到ObjectResultExecutor构造方法

ASP.NET Core 6.0自定义异常处理模块MissingMethodException问题解决办法

问题描述

在ASP.NET Core 6.0开发自定义异常处理模块时,抛出以下错误:

System.MissingMethodException: 找不到方法:'Void Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor..ctor(Microsoft.AspNetCore.Mvc.Infrastructure.OutputFormatterSelector, Microsoft.AspNetCore.Mvc.Infrastructure.IHttpResponseStreamWriterFactory, Microsoft.Extensions.Logging.ILoggerFactory)'。

报错堆栈跟踪:

at JMCKnow.Domain.Results.RequestFailedResultExecutor..ctor(OutputFormatterSelector formatterSelector, IHttpResponseStreamWriterFactory writerFactory, ILoggerFactory loggerFactory)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
位于D:\localhost\JMC\jmc-know-api\JMCKnow\JMCKnow.Domain\Helpers\HttpContextExtensions.cs第24行的JMCKnow.Domain.Helpers.HttpContextExtensions.WriteResultAsync[TResult](HttpContext context, TResult result)
位于D:\localhost\JMC\jmc-know-api\JMCKnow\JMCKnow.Api\Middlewares\CustomExceptionMiddleware.cs第124行的JMCKnow.Api.Middlewares.CustomExceptionMiddleware.HandleNotFoundExceptionAsync(HttpContext context, NotFoundException exception)
位于D:\localhost\JMC\jmc-know-api\JMCKnow\JMCKnow.Api\Middlewares\CustomExceptionMiddleware.cs第60行的JMCKnow.Api.Middlewares.CustomExceptionMiddleware.InvokeAsync(HttpContext httpContext)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

原因分析

ASP.NET Core 6.0对ObjectResultExecutor的构造函数签名做了变更,新增了IProblemDetailsService参数,但自定义的RequestFailedResultExecutor仍沿用旧版本的构造函数参数列表,导致依赖注入无法找到匹配的构造方法,从而抛出缺失方法异常。

解决方案

  1. 更新自定义ResultExecutor构造函数
    修改RequestFailedResultExecutor的构造函数,添加IProblemDetailsService参数,与ASP.NET Core 6.0的ObjectResultExecutor构造函数保持一致:

    public class RequestFailedResultExecutor : ObjectResultExecutor
    {
        public RequestFailedResultExecutor(
            OutputFormatterSelector formatterSelector,
            IHttpResponseStreamWriterFactory writerFactory,
            ILoggerFactory loggerFactory,
            IProblemDetailsService problemDetailsService) // 新增参数
            : base(formatterSelector, writerFactory, loggerFactory, problemDetailsService)
        {
        }
    
        // 自定义业务逻辑代码
    }
    
  2. 统一ASP.NET Core相关包版本
    确保项目中所有Microsoft.AspNetCore.*系列NuGet包版本均为6.0.x,避免版本不一致引发的API兼容问题。可通过修改.csproj文件统一版本:

    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="6.0.25" />
        <!-- 其他相关包保持相同主版本号 -->
    </ItemGroup>
    
  3. 清理重建项目

    • 删除项目根目录下的bin和obj文件夹
    • 执行命令行指令清理并恢复依赖:
      dotnet clean
      dotnet restore
      
    • 重新编译并运行项目

额外提示

如果是在自定义中间件中调用ObjectResultExecutor,建议直接注入IResultExecutor<ObjectResult>服务,而非手动构造实例,以此规避版本变更带来的构造函数兼容问题。

内容的提问来源于stack exchange,提问作者Jaydeep Maru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 06:00:17