ASP.NET Core 3.1 API 远程IIS部署报500.30 ANCM启动失败问题咨询
问题场景
通过Visual Studio 2019的Web Deploy功能,将ASP.NET Core 3.1 API项目部署到本地IIS服务器时,所有接口均可正常调用;将项目发布到远程服务器后调用接口时,返回HTTP Error 500.30 - ANCM In-Process Start Failure错误。
已完成的初步排查动作:
- 校验过应用配置JSON文件的语法合法性
- 已从官方渠道安装对应版本的Web Hosting Bundle
错误根因定位
开启站点stdout日志后,捕获到的异常堆栈可直接定位问题根源:
应用启动过程中加载JSON配置文件时解析失败,具体报错为:
'S' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 21 | BytePositionInLine: 61
也就是JSON配置文件的字符串值中存在未正确转义的特殊字符,触发了解析器报错。
排查方向与解决方法
- 优先核查远程服务器上的实际部署配置文件内容
本地校验JSON语法合法,但远程加载失败,本质是远程站点部署目录下的配置文件(通常为appsettings.json或自定义引入的JSON配置)与本地发布版本不一致。Web Deploy默认不会强制覆盖远程服务器上已存在的同名配置文件,容易导致旧版本配置残留,最终出现本地语法合法、远程文件存在语法错误的问题。
直接跳转到日志提示的第21行,定位该行第61字节附近的内容即可定位问题点:最常见的触发场景是配置中写入了Windows格式的本地路径(例如C:\Sample\Settings),反斜杠未做转义处理,JSON解析器会将反斜杠后的字符识别为转义序列,碰到S开头的路径字段(如\Settings、\System)时,就会触发这类非法转义字符的报错。 - 快速验证方法
将远程站点部署目录下的所有JSON配置文件备份后删除,手动上传本地发布目录下经过语法校验的完整配置文件,给站点目录的IIS应用程序池运行账号补全读写权限后重启站点,确认错误是否消失。 - 长期优化规避方案
- 调整VS发布配置的文件覆盖规则,设置为发布时强制覆盖远程服务器上的所有配置文件,避免旧版本文件残留
- 编写JSON配置中的文件路径时,将单反斜杠替换为双反斜杠(如
C:\\Sample\\Settings),或直接用正斜杠作为路径分隔符(如C:/Sample/Settings),从根源上避免非法转义字符问题
附参考代码与日志
项目接口代码(ProjectController.cs)
[HttpGet] [Route("api/Testing")] public async Task<IActionResult> GetUserList() { try { List<PatientName> lstPAtientName = new List<PatientName>(); lstPAtientName.Add(new PatientName() { PName = "ABC" }); lstPAtientName.Add(new PatientName() { PName = "XYZ" }); var dict = new Dictionary<string, object>(); dict.Add("PatientList", lstPAtientName); return Ok(dict); } catch (Exception ex) { throw ex; } }
服务启动配置代码(Startup.cs)
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(); // 配置CORS策略,允许其他应用跨域调用当前API services.AddCors(option => option.AddPolicy("MyBlogPolicy", builder => { builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); })); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddScoped<ProjectRepository>(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); // 应用CORS策略 app.UseCors("MyBlogPolicy"); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
站点stdout错误日志堆栈
Unhandled exception. System.FormatException: Could not parse the JSON file. ---> System.Text.Json.JsonReaderException: 'S' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 21 | BytePositionInLine: 61. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes) at System.Text.Json.Utf8JsonReader.ConsumeStringAndValidate(ReadOnlySpan`1 data, Int32 idx) at System.Text.Json.Utf8JsonReader.ConsumeString() at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker) at System.Text.Json.Utf8JsonReader.ReadSingleSegment() at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, Utf8JsonReader reader, MetadataDb& database, StackRowStack& stack) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedBytes) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options) at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options) at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(Stream input) at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream) --- End of inner exception stack trace --- at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) --- End of stack trace from previous location where exception was thrown --- 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.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors) at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Website.Program.Main(String[] args) in E:Website\Program.cs:line 12
内容的提问来源于stack exchange,提问作者Hemant15
相关产品推荐
相关产品推荐

