Reputation: 35
I've been having an issue with my Entity Framework based website after hosting it on Microsoft Azure. If no calls have been made to the server within about 30 minutes, the server stops responding completely and returns Internal Server Errors (500) for all requests.
This was not an issue when I was running the site locally (I could leave it running for hours with nothing happening) and it would still work when I came back to it. I managed to fix the issue by running a loop that pings the server every 15 minutes, but that's obviously not a good solution; so my question is: where and how can I configure the connection properties for my server to prevent it from dying when not in use?
I know very little about connection authentication and I have no idea how to troubleshoot this when the only error I get is an Internal Server Error (which tells me nothing).
Here is the code on Github: https://github.com/synthc/UMD.git
Upvotes: 1
Views: 451
Reputation: 5401
A number of things came to mind that I would like to share with you - one of which already pointed out by Basic.
Always On
So first off - the reason why it may seem that your web app 'goes to sleep' is because of a feature in Azure Web Apps that can be turned off:
You can read more about this feature here.
I suspect that something goes wrong while trying to get your web app back up, namely something with your database connection - which causes the 500s.
Datacontext lifetime management
So, indeed, as Basic pointed out - I would recommend using dependency injection ( DI ) for your Entity Framework ( EF ) context into your controller. This will give the EF datacontext the lifetime of a single request, which greatly reduces the context to enter a faulty state.
Ninject was the DI container that I used most often, and here's a write-up of how to get that to work with ASP.NET MVC: http://www.davepaquette.com/archive/2013/03/27/managing-entity-framework-dbcontext-lifetime-in-asp-net-mvc.aspx
Entity Framework execution strategy
Finally - EF offers different execution strategies for the database. And it offers a special one for connections on Azure. You wan to look into enabling the SqlAzureExecutionStrategy
as described here: https://msdn.microsoft.com/en-us/data/dn456835.aspx . This will deal better with transient fault handling which might occur in the cloud.
I'm pretty sure that if you use all this - you should be fine and you 500s should be gone.
Upvotes: 1