I was attempting to create a migration for an MVC ASP.NET Core 6 project using Visual Studio 2022. I encountered two errors: "Error occurred while accessing Microsoft.Extensions.HostingServices. Could not parse JSON file" and this issue indicates a problem with parsing a JSON configuration file in our .NET Core application.

You need to investigate the JSON file mentioned in the error message. This file is typically one of the configuration files used by our application, such as appsettings.json or appsettings.{Environment}.json.

We need to ensure that the JSON file is well-formed and does not contain any syntax errors. We can use online JSON validators or IDEs with built-in JSON validation tools to check for any errors.

In my case it was appsettings.json , my appsettings.json was missing a closing curly brace in json. 

Once we've confirmed that the JSON file is valid, we need to ensure that it contains the correct configuration settings required by our application. Check any keys or values that might be missing or incorrectly formatted.

Here's an example of how an appsettings.json file:


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
    

1

If encountering the error "An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider," while running migration command,then you have come to the right place in this post we will discuess the possible solution for that error.

Ensure that you application's startup process is correctly configured. One common reason for this error is missing or incorrect configuration in the Program.cs file.


public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
    

In above  example, we ensure that the CreateHostBuilder method correctly configures the application's services and sets up the web host with the specified Startup class.

Additionally, check for any missing dependencies or configuration issues in the Startup class itself. Make sure that the ConfigureServices method in the Startup class properly configures the application's services and dependencies.


2

To address this, we need to set the ASPNETCORE_ENVIRONMENT variable to "Development" in PowerShell:

$env:ASPNETCORE_ENVIRONMENT = "Development"

After setting the environment variable, we can proceed with adding migrations and updating the database:

Add-Migration IntializeDb
Update-Database

If these commands do not work for you, there's a simple solution by overriding OnConfiguring in your DbContext:


public class AppDbContext : DbContext
{        
    public AppDbContext() {
        
    }
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {            
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("dbconnectionsrting");
        }
    }
}
    


3

I tried to create a simple CRUD operation in .NET Core MVC with a basic model containing a few fields and then started getting error "An error occurred while accessing the Microsoft.Extensions.Hosting services" .

Initially, made sure that the required package, Microsoft.EntityFrameworkCore.Tools, was installed. If not, we installed it.

If you again getting error with Entity Framework (EF) calling CreateWebHostBuilder or BuildWebHost without running Main, resulting in IConfiguration being null.

Try to create a new class that inherited from IDesignTimeDbContextFactory:


public class AppDbContext : DbContext
{
    // DbContext implementation
}

public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    public AppDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
        optionsBuilder.UseSqlServer("databaseconnectionstring");

        return new AppDbContext(optionsBuilder.Options);
    }
}
    

Since we were using a newer version of .NET Core EF which employs IHostBuilder (unlike older versions that use IWebHostBuilder), we needed to ensure that the tools could obtain the service provider. This was done by invoking Program.CreateHostBuilder(), calling Build(), and then accessing the Services property.

Above solution works effectively in scenarios where Entity Framework is unable to access IConfiguration due to the absence of a running Main method. By implementing IDesignTimeDbContextFactory and providing the necessary DbContext options, we ensure that EF can create instances of the DbContext during design-time operations, such as migrations.