Azure database works on localhost, but not when used with azure service app

0

So I've been trying to publish my first project to azure. I've got everything set-up, a service app and a sql database.

My initial page loads properly(It's the standard view for a .net core web application).

The first thing I need to do is register a new user. Whenever I try through my azure app (myapp.azurewebsites.net) it fails and the logs says it's db related.

However I try the same thing by running the application on my machine in production environment, again connected to the azure sql server and everything works perfectly. I can register users, I can create posts, I can edit them. The allow access to azure services option is turned on. This error is from the eventlogs. I have not included the stacktrace.

Category: Microsoft.EntityFrameworkCore.Query EventId: 10100 RequestId: 800001be-0000-ba00-b63f- 
84710c7967bb RequestPath: /Identity/Account/Register SpanId: |1e5a93ae-43f424904f38ea9f. TraceId: 
1e5a93ae-43f424904f38ea9f ParentId: ActionId: c3430236-e61c-4785-a3c3-4f60ba115b6e ActionName: 
/Account/Register An exception occurred while iterating over the results of a query for context type 
'MyApp.Data.ApplicationDbContext'. Microsoft.Data.SqlClient.SqlException (0x80131904): Server 
name cannot be determined. It must appear as the first segment of the server's dns name 
(servername.database.windows.net). Some libraries do not send the server name, in which case the 
server name must be included as part of the user name (username@servername). In addition, if both 
formats are used, the server names must match.

Those are the different ways I tried to add the connection string to the appsettings.json file. (Server name, catalog, user and password have been replaced, they are written correctly in the appsettings file)

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user@server;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;
entity-framework-core
azure-sql-database
azure-web-sites
asked on Stack Overflow Apr 22, 2020 by user13113158

1 Answer

0

Alright so after a day and a half, I finally managed to fix it. The solution is rather simple and it is most likely my newbie mistake, that caused so much trouble.

I was following a tutorial for setting up the application and database connection after that. In the tutorial, the connection string that was being used, was the default one, found in the "myApp -> Configuration -> Connection strings", the format was:

Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;

This one was working in the guide, but not for me. So what I did, was go to my "sqldb -> connection strings" and copied the one provided there. I then went back to the app configuration and added it as a new configuration string using SqlServer as the Type.

This string was in the format:

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

After that, the app started working properly.

answered on Stack Overflow Apr 22, 2020 by user13113158

User contributions licensed under CC BY-SA 3.0