我一直在使用 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 上打开某种日志记录,但这似乎没有任何区别。任何人,有任何关于我可能做错了什么的线索吗?
最佳答案
根据你的描述,你会根据环境动态选择连接字符串,所以我测试了一下,这里是主要步骤,请引用。
- 在 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"
}
}
}
注意:您也可以将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/