自上周以来,我遇到了以下问题 - 应用程序服务重新启动,但无法启动并出现以下错误:InvalidDataException:无法从文件“C:\home\site\diagnostics\settings.json”加载配置。
内部异常是FormatException: JsonReaderException:输入不包含任何 JSON 令牌。 当 isFinalBlock 为 true 时,预计输入以有效的 JSON 令牌开头。行号: 0 |字节位置内联:0。
此文件超出了应用程序范围,当我查看它时,它包含有效的 JSON。
{"AzureDriveEnabled":true,"AzureDriveTraceLevel":"Information"}
尝试访问它的代码的堆栈跟踪:
Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(bool reload)
Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList<IConfigurationProvider> providers)
Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
Microsoft.Extensions.Logging.AzureAppServices.SiteConfigurationProvider.GetAzureLoggingConfiguration(IWebAppContext context)
Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder, IWebAppContext context, Action<AzureBlobLoggerOptions> configureBlobLoggerOptions)
Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder)
Microsoft.AspNetCore.Hosting.AppServicesWebHostBuilderExtensions+<>c.<UseAzureAppServices>b__0_0(ILoggingBuilder builder)
Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action<ILoggingBuilder> configure)
Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions+<>c__DisplayClass8_0.<ConfigureLogging>b__0(IServiceCollection collection)
Microsoft.AspNetCore.Hosting.HostingStartupWebHostBuilder+<>c__DisplayClass6_0.<ConfigureServices>b__0(WebHostBuilderContext context, IServiceCollection services)
System.Action<T1, T2>.Invoke(T1 arg1, T2 arg2)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<.ctor>b__6_2(HostBuilderContext context, IServiceCollection services)
Microsoft.AspNetCore.Hosting.BootstrapHostBuilder.RunDefaultCallbacks(ConfigurationManager configuration, HostBuilder innerBuilder)
Microsoft.AspNetCore.Builder.WebApplicationBuilder..ctor(WebApplicationOptions options, Action<IHostBuilder> configureDefaults)
Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(string[] args)
*******.Program.Main(string[] args) in Program.cs
AppService 运行在 .NET6 上,重启 2-3 次后问题消失。 重新部署应用程序并没有解决问题。
当配置文件暂时无法访问或在完全可用之前正在读取配置文件时,就会发生您遇到的错误。这可能是由于重新启动期间加载缓慢所致。
Polly
库来定义重试策略。在
program.cs
文件中添加以下行
var retryPolicy = Policy.Handle<Exception>()
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(exception, timeSpan, context) =>
{
logger.LogError($"Retrying configuration load due to error: {exception.Message}");
});
我的program.cs文件:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Polly;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddAzureWebAppDiagnostics();
});
var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
var retryPolicy = Policy.Handle<Exception>()
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(exception, timeSpan, context) =>
{
logger.LogError($"Retrying configuration load due to error: {exception.Message}");
});
retryPolicy.Execute(() =>
{ builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile("settings.json", optional: true, reloadOnChange: true);
});
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
确保在 Azure 应用服务的配置部分启用“始终开启”。这将在应用程序开始接受请求之前对其进行预热。
部署后输出没有任何问题。