Could anyone guide me how to implement a retry policy with EF to SQL Azure, please.
Entity Framework 6 has added connection resiliency as a new feature to help address this, quoting from Microsoft: "enables automatic recovery from transient connection failures". Here is the Connection Resiliency Spec for EF6 if you want to learn more.
EF6 at NuGet
The problem with using the Transient Fault Handling library according to most of the documentation out there is that it forces you to wrap every database call in your application.
If you use Entity Framework 6 (currently in alpha) then there is some new in-built support for transient retries with Azure SQL Database (with a little bit of configuration): here is the link
I've created a library which allows you to configure Entity Framework to retry using the Fault Handling block without needing to change every database call - generally you will only need to change your config file and possibly one or two lines of code.
This allows you to use it for Entity Framework or Linq To Sql, here is the link
This Azure Forum thread has some links to good resources that cover this topic. There doesn't seem to be anything 'official' quite yet. But there are some open source projects that give you a pretty good start.
http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/3a9ed384-5374-438e-a8a4-ff4bd8000738/#27b5251a-bff5-4282-980c-ad43fdd85591
From the answer:
http://blogs.msdn.com/b/appfabriccat/archive/2010/10/28/best-practices-for-handling-transient-conditions-in-sql-azure-client-applications.aspx
I personally didn't use the library the blog mentions. Instead I was able to get away with a simple WHILE LOOP with a TRY/CATCH that watched for the specific SQL EXCEPTION Error Numbers which were safe to retry. There is also a counter which basically prevents it from 'retrying' forever.
I am using the Transiet Fault Handling Framework, provided in lue of a better solution by EF team.
var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(
10,
TimeSpan.FromSeconds(0.5),
TimeSpan.FromSeconds(2)
) { FastFirstRetry = true };
context
. using(var context = new ... )
{
...//Maybe you do something to the database...
retryPolicy.ExecuteAction(() => context.SaveChanges());
}
The Windows Server AppFabric Customer Advisory Team have provided some fairly detailed guidance around retries in this blog post.
Basically, they've got a number of different ways of using the Transient Fault Handling Framework (which has been since superseded by the Transient Fault Handling Application Block, which is similar) in order to provide retries.