MatMar
MatMar

Reputation: 21

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

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.

Upvotes: 4

Views: 5059

Answers (7)

Itzik Sharon
Itzik Sharon

Reputation: 11

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

Upvotes: 1

marc_8819
marc_8819

Reputation: 1

I had no problem with .net 6 and the Microsoft.AspCore libs ALL at 5.0.12. If I updated to 6.0.0 I had this problem. I continued development and I encounter this same issue as soon as I tried to integrate Auth0. My ONLY solution to resolve this exception was to downgrade then entire app to .net 5.0. The libs remained unchanged at 5.0.12.

Upvotes: 0

MBeckius
MBeckius

Reputation: 1527

Getting the exact same error when I try to upgrade my Blazor WASM Asp.net Hosted project from 5.0 to 6.0.0. I am using Serilog, but am not sure if that is a factor.

Upvotes: 0

Vlad Kisly
Vlad Kisly

Reputation: 360

I faced the same problem. For the experiment, I created a new project and added the latest version of Microsoft.Extensions.Logging to it (currently 6.0.0-preview.7.21377.19). The project crashes with the same error as yours. The problem is that there is no four-argument overload in LoggerMessage.Define. Downgrading the package to version 6.0.0-preview.6.21352.12 helped me.

Upvotes: 0

Chenna
Chenna

Reputation: 2623

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

Upvotes: 3

BattlFrog
BattlFrog

Reputation: 3397

I am having this same issue, but dont have the option of switching from .NET CORE 6 Preview.

Here is my csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <Description>Application description goes here</Description>
    <!-- NuGet Version Number (no pre-release identifiers)-->
    <VersionPrefix>1.0.0</VersionPrefix>
    <Authors>Your name here</Authors>
    <TargetFramework>net6.0</TargetFramework>
    <PackageProjectUrl>--redacted--</PackageProjectUrl>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
    <!-- Should be major.minor.0.0-->
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <!-- Should be major.minor.version.0-->
    <FileVersion>1.0.0.0</FileVersion>
    <TypeScriptToolsVersion>3.3</TypeScriptToolsVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
    <PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0-preview.7.21378.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.7.21378.4" />
    <PackageReference Include="Microsoft.jQuery.Unobtrusive.Ajax" Version="3.2.6" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0-preview.7.21413.1" />
    <PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
  </ItemGroup>

  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="wwwroot\lib\" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyProject.Data\\MyProject.Data.csproj" />
    <ProjectReference Include="..\\MyProject.Services\\MyProject.Services.csproj" />
  </ItemGroup>
</Project>

Upvotes: 0

MatMar
MatMar

Reputation: 21

Well, when I tried to add package Microsoft.Extensions.Logging, it says that .NET Core 6.0 is version that is not supported.

I transferred from VS 2022 Preview and .NET CORE 6 Preview to VS 2019 and .NET Core 5 and now everything works.

So it was a clash between the new preview versions (or maybe a new Microsoft bug?) :D.

Upvotes: 0

Related Questions