简体   繁体   English

“Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions”的类型初始值设定项引发异常。

[英]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:我正在制作一个 .NET Core 应用程序,我想启动一个应用程序,但它在这行代码中说:

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.' System.TypeInitializationException: 'Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions' 的类型初始值设定项引发异常。

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)'.方法未找到:'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 .NET Core 6.0 是最新版本

Identity is included into application.身份包含在应用程序中。

Well, when I tried to add package Microsoft.Extensions.Logging, it says that .NET Core 6.0 is version that is not supported.好吧,当我尝试添加包 Microsoft.Extensions.Logging 时,它说 .NET Core 6.0 是不受支持的版本。

I transferred from VS 2022 Preview and .NET CORE 6 Preview to VS 2019 and .NET Core 5 and now everything works.我从 VS 2022 Preview 和 .NET CORE 6 Preview 转移到 VS 2019 和 .NET Core 5,现在一切正常。

So it was a clash between the new preview versions (or maybe a new Microsoft bug?) :D.所以这是新预览版本之间的冲突(或者可能是新的微软错误?):D。

I am having this same issue, but dont have the option of switching from .NET CORE 6 Preview.我遇到了同样的问题,但没有从 .NET CORE 6 Preview 切换的选项。

Here is my csproj:这是我的 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>

You need to update Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation您需要更新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面临同样的问题,在 VS2022 中使用 razor 模板项目创建的 MVC Proj 使用的是6.0.0-preview.5.21301.17所以我将包更新为6.0.0-preview.7.21378.6

在此处输入图片说明

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).为了实验,我创建了一个新项目,并在其中添加了最新版本的 Microsoft.Extensions.Logging(目前为 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.问题在于 LoggerMessage.Define 中没有四参数重载。 Downgrading the package to version 6.0.0-preview.6.21352.12 helped me.将软件包降级到 6.0.0-preview.6.21352.12 版对我有帮助。

Getting the exact same error when I try to upgrade my Blazor WASM Asp.net Hosted project from 5.0 to 6.0.0.当我尝试将 Blazor WASM Asp.net 托管项目从 5.0 升级到 6.0.0 时出现完全相同的错误。 I am using Serilog, but am not sure if that is a factor.我正在使用 Serilog,但不确定这是否是一个因素。

I had no problem with .net 6 and the Microsoft.AspCore libs ALL at 5.0.12.我对 .net 6 和 5.0.12 的 Microsoft.AspCore 库都没有问题。 If I updated to 6.0.0 I had this problem.如果我更新到 6.0.0 我有这个问题。 I continued development and I encounter this same issue as soon as I tried to integrate Auth0.我继续开发,并在尝试集成 Auth0 时遇到了同样的问题。 My ONLY solution to resolve this exception was to downgrade then entire app to .net 5.0.我解决此异常的唯一解决方案是将整个应用程序降级到 .net 5.0。 The libs remained unchanged at 5.0.12.库在 5.0.12 保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ Microsoft.PointOfService.Management.Explorer”的类型初始值设定项引发了异常。 C#打印POS - The type initializer for 'Microsoft.PointOfService.Management.Explorer' threw an exception. C# printing POS Web 部署任务失败。 (“Microsoft.Web.Deployment.DeploymentManager”的类型初始化程序引发了异常。) - Web deployment task failed. (The type initializer for 'Microsoft.Web.Deployment.DeploymentManager' threw an exception.) System.TypeInitializationException:&#39;类型初始值设定项引发了异常。 - System.TypeInitializationException: 'The type initializer threw an exception.' System.TypeInitializationException: &#39;Bid&#39; 的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.' 如何调试“WindowsFormsAutoClick.Form1的类型初始化程序”引发异常。“? - How to debug “Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception.”? 为什么Class不断抛出“ MyClass的类型初始值设定项引发异常”。 - Why Class keeps throwing 'The type initializer for 'MyClass' threw an exception.' “Azure.Core.Pipeline.HttpClientTransport”的类型初始化程序引发了异常。 - The type initializer for “Azure.Core.Pipeline.HttpClientTransport” threw an exception.' 我得到“'Microsoft.Cct.CctProjectNode'的类型初始化程序引发异常。”在安装Azure SDK 2.9后打开ccproj文件时 - I get “The type initializer for 'Microsoft.Cct.CctProjectNode' threw an exception.” when opening ccproj files after installing Azure SDK 2.9 类型初始值设定项引发了异常 - The type initializer threw an exception &#39;&#39;的类型初始值设定项引发了异常 - The type initializer for '' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM