Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type initializer for 'Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions' threw an exception.'

Tags:

c#

.net

.net-core

I am making an .NET Core application and I wanted to start an app but it says at this line of code:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
//...

it says this error:

System.TypeInitializationException: 'The type initializer for 'Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions' threw an exception.'

that is something new and I never seen that before while starting an application. How can I fix it?

Inner exception is:

Method not found: 'System.Action`4<Microsoft.Extensions.Logging.ILogger,!!0,!!1,System.Exception> Microsoft.Extensions.Logging.LoggerMessage.Define(Microsoft.Extensions.Logging.LogLevel, Microsoft.Extensions.Logging.EventId, System.String, Boolean)'.

Main.cs

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>();
            });
}

.NET Core 6.0 is the latest version

Identity is included into application.

like image 553
MatMar Avatar asked Sep 21 '25 11:09

MatMar


2 Answers

You need to update Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

Faced the same problem, created MVC Proj with razor template project in VS2022 was using 6.0.0-preview.5.21301.17 so I updated package to 6.0.0-preview.7.21378.6

enter image description here

like image 178
Chenna Avatar answered Sep 23 '25 00:09

Chenna


This can be linked to an compatebility issue on the Microsoft.AspNetCore.Authentication.Negotiate with the installed EF library.

Update the following libraries (using Nuget)

  1. Microsoft.EntityFrameworkCore to latest,
  2. Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

This will probably solve the issue

like image 29
Itzik Sharon Avatar answered Sep 23 '25 01:09

Itzik Sharon