.NET Core 3.1 CreateHostBuilder无法解析JSON文件

编程入门 行业动态 更新时间:2024-10-28 20:25:31
本文介绍了.NET Core 3.1 CreateHostBuilder无法解析JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试运行ASP.Net Core 3.1项目时遇到错误.错误发生在 Program.cs

I am experiencing an error when trying to run my ASP.Net Core 3.1 project. The error is at CreateHostBuilder within Program.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>(); }); }

尚未从Visual Studio 2019的默认ASP.NET Core Web应用程序模板中进行编辑.它给出了例外情况

which has not been edited from the default ASP.NET Core Web application template from Visual Studio 2019. It gives the exception

System.FormatException:'无法解析JSON文件.'

System.FormatException: 'Could not parse the JSON file.'

但是,它并不完全指代未能解析的JSON文件.这是一个 pastebin,其中包含可能正在解析的所有3个现有JSON文件.

It does not however refer exactly to what JSON file it's failing to parse. Here is a pastebin with all 3 existing JSON files it could be parsing.

这是完整的错误堆栈:

HResult=0x80131537 Message=Could not parse the JSON file. Source=Microsoft.Extensions.Configuration.Json StackTrace: at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() at Microsoft.Extensions.Hosting.HostBuilder.Build() at <filename>.Main(String[] args) in <filename>:line 13 This exception was originally thrown at this call stack: System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref System.Text.Json.Utf8JsonReader, System.Text.Json.ExceptionResource, byte, System.ReadOnlySpan<byte>) System.Text.Json.Utf8JsonReader.ReadSingleSegment() System.Text.Json.Utf8JsonReader.Read() System.Text.Json.JsonDocument.Parse(System.ReadOnlySpan<byte>, System.Text.Json.Utf8JsonReader, ref System.Text.Json.JsonDocument.MetadataDb, ref System.Text.Json.JsonDocument.StackRowStack) System.Text.Json.JsonDocument.Parse(System.ReadOnlyMemory<byte>, System.Text.Json.JsonReaderOptions, byte[]) System.Text.Json.JsonDocument.Parse(System.ReadOnlyMemory<char>, System.Text.Json.JsonDocumentOptions) System.Text.Json.JsonDocument.Parse(string, System.Text.Json.JsonDocumentOptions) Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(System.IO.Stream) Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(System.IO.Stream) Inner Exception 1: JsonReaderException: '{' is an invalid start of a property name. Expected a '"'. LineNumber: 2 | BytePositionInLine: 4

这开始于我开始使用VS Code而不是通常使用的Visual Studio.但是,该项目可以在仅运行Visual Studio的另一台Windows计算机上成功运行.我尝试了几件事,但没有主意.这是我尝试过的步骤:

It began when I started to use VS Code instead of Visual Studio which I normally use. The project however can run successfully on another Windows machine that's only ever run Visual Studio. I've tried a few things and I'm out of ideas. Here are the steps I have tried:

  • 与其他2位同事确认, appsettings.json , appsettings.Development.json 和 launchSettings.json 没有解决的语法错误在此线程
  • 确认它们均按照此线程
  • 关闭Visual Studio 2019,删除.vs文件夹,打开然后再次运行
  • 关闭Visual Studio代码,删除.vscode文件夹,打开然后再次运行
  • 切换分支和先前的提交.
  • 确认 .csproj
  • 中没有用户机密配置
  • 在本地删除回购文件,然后再次克隆
  • 完全重启PC
  • 在Visual Studio中创建新的模板化ASP.NET Core Web应用程序.运行成功.
  • 确认 args 值是大小为[1]的字符串数组.使用[0]位置并包含字符串%LAUNCHER_ARGS%.环境变量不会出现在 app.config 或Windows中.我确认 args 仍然相同,并且可以与测试纯模板项目一起使用.
  • Confirmed with 2 other colleagues that appsettings.json, appsettings.Development.json and launchSettings.json do not have syntax errors as resolved in this thread
  • Confirmed they are all encoded with UTF-8 without BOM as suggested in this thread
  • Closing Visual Studio 2019, deleting the .vs folder, opening and then running again
  • Closing Visual Studio Code, deleting the .vscode folder, opening and then running again
  • Switched branches and previous commits.
  • Confirmed there is no user secrets configuration within .csproj
  • Deleting the repo files locally and cloning it again
  • Restarting the PC entirely
  • Creating a new templated ASP.NET Core Web Application within Visual Studio. It ran successfully.
  • Confirmed that args value is a string array of size [1]. The [0] position is used and contains the string %LAUNCHER_ARGS%. The environment variable does not appear in app.config or within Windows. I confirmed args is still the same and functions with the test pure template project.
  • 推荐答案

    听起来不像是破记录,但是任何面临此问题的人,请确保您的appsettings.json格式正确.

    Not to sound like a broken record, but anyone facing this issue, please ensure that your appsettings.json is properly formatted.

    我发现在我的情况下,我的连接字符串中没有正确设置转义字符.我没有.\\ MSSQLSERVER,而是拥有.\ MSSQLSERVER.

    I found that in my case, escape characters were not properly set in my connection string. Instead of having .\\MSSQLSERVER, I had .\MSSQLSERVER.

    如果您的本地计算机上有一个测试实例,请取出云的连接字符串,并用本地计算机的连接字符串替换它(应该更简单),这应该会带您正确的方向.

    If you have a test instance on your local machine, kindly take out the connection string for the cloud and replace it with that of the local machine (which should be simpler) and that should lead you in the right direction.

    更多推荐

    .NET Core 3.1 CreateHostBuilder无法解析JSON文件

    本文发布于:2023-11-09 17:00:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1572928.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件   Core   NET   JSON   CreateHostBuilder

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!