首页 > 解决方案 > 在 Azure 上打开注册页面时出现“502 - Web 服务器在充当网关或代理服务器时收到无效响应”错误

问题描述

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

我可以将网站发布到天蓝色:

https://mytrade20180517093224.azurewebsites.net/

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

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

我得到:

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

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

我拥有的身份验证类型是“个人用户帐户”之一,我已经在我引用的 azure db 上为此重新创建了表。在startup.cs中,如果环境是开发环境,我将调用“DefaultConnection”字符串,如果不是(我猜Azure默认不会是)调用另一个连接字符串,天蓝色的:

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

天蓝色是否需要做一些不同的事情来选择连接字符串?我试图在 azure 上打开某种登录,但这似乎没有任何区别。任何一个,关于我可能做错什么的任何线索?

标签: c#azureasp.net-core.net-coreentity-framework-core

解决方案


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

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

在此处输入图像描述

2.跟随ASP.NET Core MVC with Entity Framework Core开始。

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

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

注意:您也可以将门户数据库中的连接字符串设置为此处,然后您可以在本地对其进行测试,并可以使用调试进行故障排除。

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

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();
        }

如果您仍然有任何问题,您可以在门户上启用诊断日志并参考本文进行故障排除。


推荐阅读