首页 > 解决方案 > 带有 Docker 连接的 ASP.NET Core 在首次加载时被拒绝

问题描述

我正在开发一个具有基于 docker 的微服务架构的 ASP.NET Core 应用程序。当我开始使用 IIS Express 调试我的应用程序时,我在第一次加载时不断收到套接字错误。刷新页面后一切正常。我的直觉告诉我,问题是我试图在 docker 容器启动之前向服务发出 http 请求,但我的 docker_compose 文件中有depends_on 子句。任何帮助将非常感激。

这是引发错误的部分(在 Home.Index 操作中调用):

HttpResponseMessage response = await _httpClient.GetAsync(_appSettings.Value.ListingsAPIUrl + "/Listings");

这是我的码头工人撰写:

services:
  sql.data:
    image: microsoft/mssql-server-linux:2017-latest

  listings.api:
    image: ${REGISTRY:-listings}/listings.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Listings/Listings.API/Dockerfile
    depends_on:
      - sql.data

  webmvc:
    image: ${DOCKER:-listings}/webmvc:${TAG:-latest}
    build:
      context: .
      dockerfile: src/WebApps/WebMVC/Dockerfile
    depends_on:
      - listings.api

这是我的 Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

这是我的 Startup.cs 的相关部分:

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddCustomMVC(Configuration)
            .AddAppSettings(Configuration)
            .AddCustomHttpClients(Configuration);
            //.AddCustomAuthentication(Configuration, Environment);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        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.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

自定义启动扩展:

    public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        }); 

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        return services;
    }

    public static IServiceCollection AddAppSettings(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddOptions();
        services.Configure<AppSettings>(configuration);

        return services;
    }

    public static IServiceCollection AddCustomHttpClients(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddHttpClient();

        return services;
    }

标签: c#dockerasp.net-core.net-coremicroservices

解决方案


推荐阅读