ASP.NET Core api 项目 3.1 在 IIS 上发布

Posted

技术标签:

【中文标题】ASP.NET Core api 项目 3.1 在 IIS 上发布【英文标题】:ASP.NET Core api project 3.1 publish on IIS 【发布时间】:2021-04-27 02:09:00 【问题描述】:

我正在尝试使用 Visual Studio 2019 中的 Web 部署功能在我的电脑本地 IIS 服务器中部署我的 ASP.NET Core API 项目。

我可以从本地 IIS 服务器成功调用所有 api。

当我发布到远程服务器并调用 api 时,它显示 HTTP Error 500.30 - ANCM In-Process Start Failure 错误。

    我检查了 applications.json 文件中的无效属性。 我已经从 https://dotnet.microsoft.com/download/dotnet-core/3.1

api call from web browser

在 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; 

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    
        services.AddControllers().AddNewtonsoftJson();

        //Configure CORS also to make this API enable for other application
        services.AddCors(option => option.AddPolicy("MyBlogPolicy", builder =>
        
            builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
        
        ));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        services.AddScoped<ProjectRepository>();
    

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    
        if (env.IsDevelopment())
        
            app.UseDeveloperExceptionPage();
        

        app.UseHttpsRedirection();

        //Configure CORS also to make this API enable for other application
        app.UseCors("MyBlogPolicy");

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        
            endpoints.MapControllers();
        );
    



我已设置 stdoutLogEnabled="true" 。网站/日志显示如下。 **

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

**

【问题讨论】:

你有多个 appSettings.json 用于不同的环境吗?检查那些。您似乎无法解析某些 JSON 文件。 'S' 是 JSON 字符串中的无效可转义字符。该字符串应正确转义。行号:21 从错误中可以明显看出JSON文件中有字符存在转义问题,在文件的第21行,如果您仍然无法解决问题,请发布您的json文件并我会帮你解决问题的。 【参考方案1】:

你添加了吗

<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

在您的.csproj 文件中?

在这里查看资源:https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-3.1

【讨论】:

是的,我在 .csproj 文件中添加了 AspNetCoreHostingModel 标签。但它显示相同的错误。还尝试在已发布文件的 program.cs 中添加 .UseIISIntegration()。 能否将您的 appsettings.json 添加到您的问题中?

以上是关于ASP.NET Core api 项目 3.1 在 IIS 上发布的主要内容,如果未能解决你的问题,请参考以下文章