c# - 带有 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;
}
解决方案
推荐阅读
- linq - 实体框架核心对象循环
- android - ANDROID_SDK_ROOT 环境变量在系统重启后总是需要 source ~/.bash_profile
- amazon-web-services - AWS lambda函数为具有相同时间戳的同一事件多次触发
- python - 通过多行在字符串和数字之间打印
- javascript - 不知道如何在我的代码中将栅格应用于每个星球
- python - 解包和测试嵌套字典值python
- javascript - Document.GetElementById(VARIABLE) 不起作用,仅适用于字符串
- python - 如何从 Tkinter 框架中正确删除图形
- python - 找到形状的确切边缘
- c# - NetworkInterface.GetIPv4Statistics() 是否计算到本地主机的内部流量?