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

.NET 8迁移报错:ValidConstructorNotFoundException无有效构造函数

解决.NET 8重构中ValidConstructorNotFoundException异常

核心问题分析

异常指向ACES_Core.DeceptionAPI+Agent类缺少可被序列化/DI容器识别的有效构造函数,结合.NET 8环境和类库迁移场景,以下是具体解决步骤:

1. 修正Agent类的构造函数配置

  • 添加无参数公共构造函数:如果使用System.Text.Json或默认序列化机制,确保Agent类有public Agent()无参构造,这是大多数序列化框架的默认要求。
  • 标记参数化构造函数:如果必须使用参数化构造,给目标构造函数添加[JsonConstructor]特性(兼容System.Text.Json和Newtonsoft.Json),让序列化框架明确要使用的构造逻辑。

2. 检查访问修饰符

迁移类库后,确认Agent类及其构造函数的访问修饰符为public,避免因internal/private修饰导致序列化框架或DI容器无法访问构造函数。

3. 替换同步阻塞为异步调用

调用Task.Wait()会引发死锁风险,且不符合.NET 8的异步编程规范:

  • 将GetAgent方法改为异步签名(返回Task<Agent>)
  • 上游FetchFromAPI、Main等方法同步改为异步,用await替代Wait()

4. 验证DI容器注册

从栈跟踪看异常涉及DI容器解析:

  • 如果Agent类被注册到DI容器,检查其构造函数的所有依赖是否已在容器中注册
  • 区分序列化创建实例和DI创建实例的场景,确保两种场景下构造函数都能被正常访问

完整异常信息

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (No valid constructor could be found for ACES_Core.DeceptionAPI+Agent)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at ACES_Core.DeceptionAPI.GetAgent(HttpClient httpClient, String baseURI, String domain, String hostname, String dataset) in C:\Users\Administrator\Source\Repos\AC-EventServiceV2\ACES-Core\DeceptionAPI.cs:line 85
   at ACES_Core.MonitoredResources.FetchFromAPI(HttpClient httpClient, Config config, String agentDomain, String agentHostname) in C:\Users\Administrator\Source\Repos\AC-EventServiceV2\ACES-Core\MonitoredResources.cs:line 29
   at AC_EventService.ACESThread.Main() in C:\Users\Administrator\Source\Repos\AC-EventServiceV2\ACESThread.cs:line 95
   at AC_EventService.ACESThread..ctor() in C:\Users\Administrator\Source\Repos\AC-EventServiceV2\ACESThread.cs:line 60
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   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.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, 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(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__15.MoveNext()

  This exception was originally thrown at this call stack:
    [External Code]
    ACES_Core.DeceptionAPI.GetAgent.AnonymousMethod__0() in DeceptionAPI.cs

Inner Exception 1:
ValidConstructorNotFoundException: No valid constructor could be found for ACES_Core.DeceptionAPI+Agent

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 18:50:53