【发布时间】:2020-02-11 10:41:50
【问题描述】:
我在尝试运行 ASP.Net Core 3.1 项目时遇到错误。错误在CreateHostBuilder 内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 应用程序模板编辑。它给出了异常
System.FormatException: '无法解析 JSON 文件。'
然而,它并没有准确地引用它无法解析的 JSON 文件。这是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 机器上成功运行。我已经尝试了一些东西,但我没有想法。以下是我尝试过的步骤:
- 与其他 2 位同事确认
appsettings.json、appsettings.Development.json和launchSettings.json没有在 this thread 中解决的语法错误 - 已确认它们都使用 UTF-8 编码,没有 BOM,如 this thread 中所建议的那样
- 关闭 Visual Studio 2019,删除 .vs 文件夹,打开然后再次运行
- 关闭 Visual Studio Code,删除 .vscode 文件夹,打开然后再次运行
- 切换的分支和以前的提交。
- 已确认
.csproj中没有用户机密配置 - 在本地删除 repo 文件并再次克隆
- 完全重启电脑
- 在 Visual Studio 中创建新的模板化 ASP.NET Core Web 应用程序。运行成功。
- 确认
args值是一个大小为[1]的字符串数组。使用 [0] 位置并包含字符串%LAUNCHER_ARGS%。环境变量不会出现在app.config或 Windows 中。我确认args仍然与测试纯模板项目相同且功能相同。
【问题讨论】:
-
请告诉我们
args的值!听起来 JSON 无效/格式不正确。 -
这可能是指你的appsettings,你有appsettings文件吗?更重要的是它是validjson吗?
-
args是一个字符串数组,大小为 [1],[0] 值为"%LAUNCHER_ARGS%"。我是 .net 核心的新手,所以我不确定这个字符串值是从哪里来的。使用该字符串搜索项目没有结果。 @phuzi -
@MarkDavies 我可以确认我已经与其他 2 位同事检查了
appsettings.json。没有语法错误,但我会在这里发布(带有审查的数据库名称)以确认它。{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=<azure-db>;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" } -
你能告诉我们你对
CreateHostBuilder的实现吗?
标签: c# visual-studio visual-studio-code asp.net-core-mvc