【问题标题】:Docker container fail to run ASP.NET Core app in SSL due to IApplicationBuilder.UsePathBase() error由于 IApplicationBuilder.UsePathBase() 错误,Docker 容器无法在 SSL 中运行 ASP.NET Core 应用程序
【发布时间】:2021-02-06 17:46:39
【问题描述】:

我在 Docker 容器中运行基本 ASP.NET Core Web 应用程序时遇到问题。这是 Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /src
COPY ["PSN.Test/PSN.Test.csproj", "PSN.Test/"]
RUN dotnet restore "PSN.Test/PSN.Test.csproj"
COPY . .
WORKDIR "/src/PSN.Test"
RUN dotnet build "PSN.Test.csproj" -c Development -o /app/build

FROM build AS publish
RUN dotnet publish "PSN.Test.csproj" -c Development -o /app/publish

FROM base AS final
WORKDIR /app
EXPOSE 80
EXPOSE 443

COPY --from=publish /app/publish .
COPY PSN.Test.pfx /root/.aspnet/https/PSN.Test.pfx

ENTRYPOINT ["dotnet", "PSN.Test.dll"]

Docker 容器镜像创建得很好。如果我使用以下命令运行它,我可以通过 http://localhost 访问网站:

docker run -p 80:80 -p 443:443 -e ASPNETCORE_ENVIRONMENT=Development --name psn-test psn-test:latest

但是,当我使用以下命令在启用 SSL 的情况下运行 Kestrel 时,事情会发生故障:

docker run -p 80:80 -p 443:443 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_HTTPS_PORT=443 -e ASPNETCORE_URLS=http://localhost:80,https://localhost:443 -e ASPNETCORE_Kestrel__Certificates__Default__Password=test123 -e ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.aspnet/https/PSN.Test.pfx --name psn-test psn-test:latest

这是我运行它时的错误消息:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {67e83ec3-ba9b-4144-95ee-b26935280f1e} may be persisted to storage in unencrypted form.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to start Kestrel.
System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.ParseAddress(String address, Boolean& https)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
Unhandled exception. System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.ParseAddress(String address, Boolean& https)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at PSN.Test.Program.Main(String[] args) in /src/PSN.Test/Program.cs:line 16

我能找到的最明显的错误是:

System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().

根据我能找到的资源,他们的解决方案指向 1) 更新/更正 launchSettings.json 和/或 2) 在 startup.cs 文件中使用 app.UsePathBase()。

这是我的 launchSettings.json 文件:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:57851",
      "sslPort": 44345
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "PSN.Test": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

这对我来说是正确的(一旦它被发布到网络服务器,它甚至会被使用吗???)。

这是我的 startup.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace PSN.Test
{
    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.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UsePathBase(PathString.Empty);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

就像我说的,这是标准票价,只是我在 Configure() 方法中添加了一行以查看是否可以解决问题:

app.UsePathBase(PathString.Empty);

我在这里缺少什么?我还可以调查什么来解决此问题?

【问题讨论】:

    标签: docker asp.net-core https alpine kestrel-http-server


    【解决方案1】:

    这就是问题所在:

    -e ASPNETCORE_URLS=http://localhost:80,https://localhost:443
    

    docker run 命令中删除它可以防止错误。

    【讨论】:

    猜你喜欢
    • 2014-10-11
    • 2020-08-03
    • 2019-11-05
    • 2020-04-19
    • 2014-09-02
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多