I know this has been asked several times before, but I thought I may have a different solution; however, I need some help to get it to work.
The idea: Business Layer calls a Data layer function. The data layer function wraps the call to the database context's stored procedure function in a retry policy. Essentially, I want the LINQ tool to import and manage the actual call to the stored procedure, but I wish it would wrap the logic with some retry policy for retry-able errors.
Much of this concept is taken from What is good C# coding style for catching SQLException and retrying, however, this appears to only work for LINQ to SQL commands, not calling stored procedure functions generated in the DBML.
Old method:
Sub BLFunctionWithoutRetry()
Using DB as CustDataContext
DB.sp_GetCustomers()
End Using
End Sub
New Logic with Retry:
Sub BLFunctionWithRetry()
GetCustomers()
End Sub
Function GetCustomers() As List(Of Customer)
Return Retry(Of List(Of Customer))(sp_GetCustomers())
End Function
Function Retry(Of T)(FunctionToCall As Func(Of T)) As T
Dim Attempt As Integer = 0
While True
Try
Using DB as MyDataContext
DB.FunctionToCall()
End Using
Catch ex as SQLException
If Retryable() Then Attempt += 1
If Attempt >= Max Or Not Retryable() Then Throw
End Try
End While
Function Retryable() As Boolean
Return True
End Function
This is the general idea; however, I need help writing the Retry function above. I am getting the obvious error FunctionToCall() is not a member of 'MyDataContext'
. Additaionlly, I dont know how to write this so it will work for any stored procedure with any length of input parameters.