Search code examples
c#.netnancyclass-constructors

.NET 8 - ValidConstructorNotFoundException: No valid constructor could be found


I am refactoring a classic .NET Framework App to .NET 8, and when I moved some classes into their own library I started getting a "No valid constructor could be found" error.

The error is being thrown on line 91 when WebTask.Wait() is called.

Any help is greatly appreciated!

Full details are as follows;

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

Solution

  • The custom ValidConstructorNotFoundException seems to be thrown on this line:

    return _serializer.Deserialize<Agent[]>(response)[0];
    

    As far as I have found out in the source code of Nancy.Json, the exception is thrown when a constructor cannot be found. The logic for it I found

    var hasDefaultConstructor =
                t.GetTypeInfo().DeclaredConstructors.Any(ctor => !ctor.GetParameters().Any());
    

    This always returns false for a struct that has not defined explicitly a parameterless constructor such as our Agent (which btw you now can under .NET 8)

    Maybe you have changed Agent from class to struct?