启动容器时出现错误:Microsoft.Extensions.Hosting.Internal.Host[11] 托管无法启动

问题描述 投票:0回答:1

我基于 mcr.microsoft.com/dotnet/aspnet:8.0.2-alpine3.18-amd64 b

创建了一个映像

现在,当我尝试通过命令运行它时

docker run -p 5000:80 personalfinance-api:1.0.0

我收到以下错误:

2024-02-27 16:14:22 fail: Microsoft.Extensions.Hosting.Internal.Host[11]
2024-02-27 16:14:22       Hosting failed to start
2024-02-27 16:14:22       System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
2024-02-27 16:14:22       To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
2024-02-27 16:14:22       For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

同时,该案例所示的解决方案无法配置HTTPS端点。没有指定服务器证书,找不到默认的开发者证书对我不起作用。

这是我的启动设置:

{
  "profiles": {
    "http": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5214"
    },
    "https": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7145"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "environmentVariables": {
        "ASPNETCORE_URLS": "https://+:443;http://+:80"
      },
      "publishAllPorts": true,
      "useSSL": true
    }
  },
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:26413",
      "sslPort": 44363
    }
  }
}

这是我的Program.cs:

using PersonalFinance.Api;
using PersonalFinance.Services;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers().AddJsonOptions(
    x=>
    {
        x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    }
    );
 //Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var conf = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddUserSecrets<Program>() 
    .Build();

var fxRatesProviderSettings = conf.GetSection("fxRatesProviderSettings");
var fxRatesConnectionStrings = fxRatesProviderSettings.GetSection("FxRatesConnectionStrings");

var fxRatesProviderResolver = new FxRatesProviderResolver();
fxRatesProviderResolver.Add("CSV", new CsvRateProvider(fxRatesConnectionStrings["CsvFilePath"], ';', 30000));
fxRatesProviderResolver.Add("MySql", new MySqlRateProvider(fxRatesConnectionStrings["MySqlConnectionString"]));
fxRatesProviderResolver.Add("MSSQL", new SqlServerRateProvider(fxRatesConnectionStrings["SqlServerConnectionString"]));

builder.Services.AddSingleton(fxRatesProviderResolver);

builder.Services.AddTransient<CurrencyValidator>();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

//app.MapGet("api/server/ping", ()=> "pong");

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

这里是我的 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Https": {
        "Url": "https://localhost:7145"
      }
    }
  },
  "FxRatesProviderSettings": {
    "FxRateProvider": "MySql",
    "FxRatesConnectionStrings": {
      "MySqlConnectionString": "ToBeReplaced",
      "SqlServerConnectionString": "ToBeReplaced"
    }
  }
}

我做错了什么?

c# asp.net docker visual-studio
1个回答
0
投票

你明白了吗?我有一个非常相似的问题。我有一个 ASP.Net Core WebApp 在我的开发计算机上运行良好,但是当我将其发布到另一台计算机时,我遇到了相同的异常。

Microsoft.Extensions.Hosting.Internal.Host 事件编号:11 托管无法启动 无法配置 HTTPS 端点。未指定服务器证书,默认开发者证书找不到或已过期。

我在目标机器上运行了 dotnet dev-certs https --trust ,但没有任何变化。

我不确定如何部署/发布我的开发证书。

任何建议表示赞赏。

© www.soinside.com 2019 - 2024. All rights reserved.