0

I have been playing around with Azure and am looking to publish my .net core 2 application there. So far, this builds on my local machine fine. I can register as a user, so I know everything is okay, locally. I can see users and have even managed to register certain users with certain claims.

I can publish the site to azure:

https://mytrade20180517093224.azurewebsites.net/

I can also log into the azure database from vs2017 using the same credentials I supplied in my appsettings.json. However, the problem I'm getting is my azure site then falls over when you go to register:

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

I get:

502 - Web server received an invalid response while acting as a gateway or proxy server.

There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

The authentication type I have is the "individual user accounts" one, I have re-created the tables for this on the azure db I'm referencing. In the startup.cs I'm calling the "DefaultConnection" string if the environment is development and if its not (which I'm guessing Azure wont be by default) calling another connection string, the azure one:

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

Does azure require something different to be done to pick the connection string? I tried to switch on the some kind of logging on azure but this didn't seem to make any difference. Any one, any clues as to what I could be potentially doing wrong?

ekad
  • 14,436
  • 26
  • 44
  • 46
Martin Cooke
  • 526
  • 1
  • 4
  • 18

2 Answers2

1

Yes, on azure you need to provide connection string differently Login on portal.azure.com Go to your Web App -> Settings -> Application Settings -> Connection Strings Here you add new connection string with name in your case azure

**Azure connection settings**

Ikram Shah
  • 1,206
  • 1
  • 11
  • 12
  • How does azure know to use this connection string? Also I can copy the connection string from online in the azure portal, it includes text as {username~} and {password} am I actually filling my username and password in these parts or does azure know to automatically replace these with my details? – Martin Cooke May 23 '18 at 22:10
  • Turns out this problem the sqlcon text "connection Timeout" not having a space!!! ffs. Thanks for your help thou :) – Martin Cooke May 23 '18 at 23:09
1

From your description, you would dynamiclly choose a connection string based on Environment, so I test and here is main steps, please refer to it.

  1. Set the ASPNETCORE_ENVIRONMENT value to azure in webapp>property>debug.

enter image description here

2.Follow ASP.NET Core MVC with Entity Framework Core to get started.

3.Set the appsetting.json with your two connection string.

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

Note:You could also set the connectionstring in database on portal to here, then you could test it in local and could use debug to troubleshooting.

Also, you could try to test with one connectionstring to ensure you have no problem with connecting to database.

4.Enable Developer exception page by using app.UseDeveloperExceptionPage(); and the app.UseExceptionHandler methods in your startup class which would display the errors.

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

If you still have any problem, you could enable Diagnostics logs on portal and refer to this article to troubleshooting.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Many thanks for your help so far, I'm still pulling my hair out regarding this. I have be able to use a bit of kit called 'Cloud Explorer' which has helped as you can attach a process to debug. The answer below details adding a connection string inside the azure portal but your answer didn't just wondered why? – Martin Cooke May 23 '18 at 22:06
  • Turns out this problem the sqlcon text "connection Timeout" not having a space!!! ffs. Thanks for your help thou :) – Martin Cooke May 23 '18 at 23:09
  • Ikram may want to quote the connection on portal. However, as I test, I could not achieve in local so I am not clear why he do that. – Joey Cai May 24 '18 at 02:35
  • @MartinCooke, you could try to [troubleshoot connectivity issues with Microsoft Azure SQL Database](https://support.microsoft.com/en-us/help/10085/troubleshooting-connectivity-issues-with-microsoft-azure-sql-database) – Joey Cai May 24 '18 at 02:37