c# - 在 Azure 上打开注册页面时出现 "502 - Web server received an invalid response while acting as a gateway or proxy server"错误

标签 c# azure asp.net-core .net-core entity-framework-core

我一直在使用 Azure,并希望在那里发布我的 .net core 2 应用程序。到目前为止,这在我的本地机器上构建得很好。我可以注册为用户,所以我知道本地一切都很好。我可以看到用户,甚至设法注册具有某些声明的某些用户。

我可以将网站发布到 azure:

https://mytrade20180517093224.azurewebsites.net/

我还可以使用我在 appsettings.json 中提供的相同凭据从 vs2017 登录到 azure 数据库。然而,我遇到的问题是当你去注册时我的 azure 网站崩溃了:

https://mytrade20180517093224.azurewebsites.net/Account/Register

我得到:

502 - Web 服务器在充当网关或代理服务器时收到无效响应。

您正在查找的页面有问题,无法显示。当 Web 服务器(充当网关或代理)联系上游内容服务器时,它从内容服务器收到无效响应。

我的身份验证类型是“个人用户帐户”,我已在我引用的 Azure 数据库上为此重新创建了表。在startup.cs中,如果环境是开发环境,我将调用“DefaultConnection”字符串,如果不是(我猜测Azure默认情况下不会),则调用另一个连接字符串,即 azure 的连接字符串:

if (HostingEnvironment.IsDevelopment())
{
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
else
{
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("azure")));
}   

azure 是否需要执行不同的操作来选择连接字符串?我尝试在 azure 上打开某种日志记录,但这似乎没有任何区别。任何人,有任何关于我可能做错了什么的线索吗?

最佳答案

根据你的描述,你会根据环境动态选择连接字符串,所以我测试了一下,这里是主要步骤,请引用。

  1. 在 webapp>property>debug 中将 ASPNETCORE_ENVIRONMENT 值设置为 azure。

enter image description here

2.关注ASP.NET Core MVC with Entity Framework Core开始吧。

3.使用您的两个连接字符串设置appsetting.json。

{
  "ConnectionStrings": {
    "DefaultConnection": "connectiondefault",
    "azure": "connectionazure"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

注意:您也可以将portal数据库中的连接字符串设置到这里,然后您可以在本地测试它,并可以使用debug来排除故障。

此外,您可以尝试使用一个连接字符串进行测试,以确保连接到数据库没有问题。

4.通过在启动类中使用 app.UseDeveloperExceptionPage();app.UseExceptionHandler 方法来启用开发人员异常页面,这将显示错误。

        public Startup(IHostingEnvironment env)
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .Build();

            HostingEnvironment = env;
        }

        public IConfigurationRoot Configuration { get; }
        public IHostingEnvironment HostingEnvironment { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            if (HostingEnvironment.IsDevelopment())
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            }
            else
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("azure")));
            }
            services.AddMvc();
        }

如果仍有问题,您可以在门户上启用诊断日志并引用此article进行故障排除。

关于c# - 在 Azure 上打开注册页面时出现 "502 - Web server received an invalid response while acting as a gateway or proxy server"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50401449/

相关文章:

c# - 使用 ObjectResult 将 EF6 转换为 EF 核心

azure - 如何使自定义角色的范围成为 Azure 中的资源组?

python - 消费计划下的 Azure 逻辑应用程序可以调用具有 VNet 集成的应用程序服务计划下的 azure 函数吗

c# - Azure 函数 - 在 Azure 存储中找不到方法

.net - Couchbase 存储桶的命名参数

c# - 这段代码中的范围有什么问题?

c# - LINQ:如何将内部列表中的项目放入一个列表中?

c# - 在特定时间后执行 Action ,但如果手动调用,则重置计时器

javascript - 在 ASP Net CORE MVC 元素中使用 Google Material UI

sqlite - 在单元测试期间避免在 EF Core 2.2 中使用 HasData Seed DbContext