azure-sql-database.net-8.0azure-webapps

Connecting Azure SQL Database from Azure Web App


I have an instance of Azure Sql DB, there is no issue when I run the web app locally. The connection to Azure SQL DB is made successfully locally and I get the data as well. Though when I publish the web app to Azure, I get 500 Internal server error (details of the error below). Can someone help on suggesting some right steps. Web App is running on .Net8.0

2024-04-18 13:11:22.251 +00:00 [Error] Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Connection ID "17221764975870083192", Request ID "40000079-0000-ef00-b63f-84710c7967bb": An unhandled exception was thrown by the application.System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when sending the request to the server. (provider: Session Provider, error: 19 - Physical connection is not usable)at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)at System.Data.SqlClient.SqlConnection.Open()at AnFramework.Data.Core.ConnectionManager.GetConnection(String databaseName)at AnFramework.Data.Core.ConnectionManager.PerformDatabaseOperation(Int32 opCode, IConnectionManaged targetObject, DbOperationOptions options)at


Solution

  • The error connectivity issue between Azure Web App and Azure SQL Database occurs when there are no added service dependencies and Configuration details in Azure.

    public class Todo
    {
        public int ID { get; set; }
        public string? Description { get; set; }
    
        [DisplayName("Created Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime CreatedDate { get; set; }
    }
    
    

    Server=tcp:myserver.database.windows.net,1433;Initial Catalog=myDataBase;User Id=mylogin@myserver;Password=myPassword;Encrypt=True;Connection Timeout=30;

    enter image description here

    Add the service dependencies and deploy the application to the Azure Web App.

    Azure: enter image description here

    For Azure SQL, refer to this documentation for Creating and Deploying .NET Minimal API and SQL Database on Azure