代码之家  ›  专栏  ›  技术社区  ›  Martin Cooke

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

  •  0
  • Martin Cooke  · 技术社区  · 8 年前

    我一直在玩Azure,希望能发布我的。net核心2应用程序。到目前为止,这是建立在我的本地机器上的。我可以注册为用户,所以我知道本地一切正常。我可以看到用户,甚至成功地注册了具有特定声明的特定用户。

    azure我可以发布网站:

    https://mytrade20180517093224.azurewebsites.net/

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

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

    我得到:

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

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

    我的身份验证类型是“个人用户帐户”,我已经在我引用的azure db上为此重新创建了表。在初创公司。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上的某种登录,但这似乎没有任何影响。任何人,任何关于我可能做错什么的线索?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Joey Cai    8 年前

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

    1. 在webapp中将ASPNETCORE_环境值设置为azure>房地产>调试。

    enter image description here

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

    3.设置应用程序设置。json和两个连接字符串。

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

    笔记 :您还可以将门户上数据库中的connectionstring设置为此处,然后可以在本地对其进行测试,并可以使用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 排除故障。

        2
  •  1
  •   Ikram Shah    8 年前

    是的,在azure上,您需要以不同的方式提供连接字符串 登录 门户。蔚蓝色的通用域名格式 去你的办公室 网络应用->设置->应用程序设置->连结字符串 在这里,您可以在案例中添加带有名称的新连接字符串 蔚蓝色的

    **Azure connection settings**