Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 Azure Website fails to connect to SQL Azure DB on some views due to role based [Authorize] attribute

We have an ASP.NET MVC 5 website, which works fine when we run it locally with Visual Studio. When run locally the website is connected to two SQL Express databases (the AspNetUsers & our Content DB). We connect to the content database using Entity Framework.

We deployed the website to Azure and migrated the 2 databases to SQL-Azure databases.

Here are the connectionstring within the web.config

<connectionStrings>    
<add name="AspNetUsersDatabase" connectionString="Data Source=tcp:ZZZ.database.windows.net,
1433;Initial Catalog=ZZZ;User ID=ZZZ@ZZZ;Password=ZZZ;" 
providerName="System.Data.SqlClient" />

<add name="Group5DatabaseConnectionString" connectionString="Data Source=YYY.database.windows.net,
1433;Initial Catalog=YYY;User ID=YYY@YYY;Password=YYY;" 
providerName="System.Data.SqlClient" />

<add name="Group5DatabaseEntities" connectionString="metadata=res://*/Models.Group5Model.csdl|
res://*/Models.Group5Model.ssdl|res://*/Models.Group5Model.msl;provider=System.Data.SqlClient;
provider connection string=&quot;data source=XXX.database.windows.net,1433;
InitialCatalog=XXX;UserID=XXX@XXX;Password=XXX;MultipleActiveResultSets=True;
App=EntityFramework&quot;"providerName="System.Data.EntityClient" />
</connectionStrings>

It appears that these are linked up to the website correctly, as the connection string in the web.config matches with the connection string in Azure correctly as described in this answer and as most views on the website work fine.

However when trying to open two of the views, the following error appears:

Server Error in '/' Application.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.

The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.

If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.

Sql Server Express must be installed on the machine. The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.

It seems as the following line might be most significant in the error message:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.

The two views that don't work are both instantiating the AccountController and use the following method within it:

[Authorize]
public class AccountController : Controller
{
    // ...
    public string FindUserArea(string id)
    {
        int locationId = FindUserLocationId(id);                
        Location location = db.Locations.FirstOrDefault(p => p.LocationID == locationId);

        return (string) (location.Suburb.SuburbName + " > "
            + location.Suburb.Region.RegionName + " > "
            + location.Suburb.Region.Province.ProvinceName);
    }
    // ...
}

We first suspected there to be an error within the AccountController, but another view which also uses the method above displays fine. We then thought there could still be the possibility of an error related to InitializeSimpleMembership as this came up in a few questions (we weren't 100% sure about the meaning/effect of SimpleMembership). We followed the lead in this question, but successfully adding InitializeSimpleMembership based on the suggestions in this article did not resolve the error.

Also, the same error appears at registration for one of two users, however the AspNetUser Azure DB still updates and once refreshing the site login with the new user is possible.

like image 452
Smile5.09 Avatar asked Oct 31 '22 17:10

Smile5.09


1 Answers

This does seem quite odd, I have seen other odd behaviour when SQL connections are made from a mix of Entity Framework context and standard connection, however if you just have 1 single database connection string that may not be the issue.

Something else I have come across, but mostly due to deadlock and timeout issues is EF DbExecutionStrategy which can re-try calls based on various errors including transient connection issue, this may be something to try, see these links:

http://www.codeproject.com/Tips/758469/Implementing-Connection-Resiliency-with-Entity-Fra

http://thedatafarm.com/data-access/ef6-connection-resiliency-for-sql-azure-when-does-it-actually-do-its-thing/

like image 196
Mark Redman Avatar answered Nov 11 '22 11:11

Mark Redman