System.InvalidOperationException: No constructor for type can be instantiated using services from the service container and default values

  .net-5, azure, msix, visual-studio, windows

Question:

How do I debug and resolve the error being displayed in the Event Viewer?

Context:

We have a .NET 5 WinForms application that runs fine in debug mode. When we create an MSIX installer and deploy to Azure we are able to download and install it successfully. However, when the application launches, nothing happens. In the event viewer, we get the following error:

Exception Info: System.InvalidOperationException: No constructor for
type TypeName can be instantiated using services from the service container
and default values.

Event Viewer Error

Per the stack trace, the constructor in question is an Entities file that implements DbContext and there are only two constructors, which are as follows:

  1. A constructor that takes a user-defined SQL ConnectionString as an argument.
  2. A default constructor that takes an AzureInterceptor and IConfiguration as an argument, the latter of which is used for retrieving the Azure SQL ConnectionString from the appsettings.json file*

*There are various appsettings.<Env>.json files implemented, based on the build environment. Their implementation seems to be automatically handled. In other words, if I add them to the builder, I get a connection error.

Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
    .ConfigureAppConfiguration((context, builder) =>
    {
        // Add other configuration files.
        builder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            ;
    })

Source: Windows Questions

LEAVE A COMMENT