I am implementing retry logic for WCF services on the client side.
I have multiple operations in WCF service with various input parameters and return types.
I created a wrapper that can make a call to these certain methods that have no return type(void) using Action delegate. Is there any way to call methods that have various input parameters and return type.
Or is there any logic to implement retry functionality on the client side that can handle multiple WCF services.
Class RetryPolicy<T>
{
public T ExecuteAction(Func<T> funcdelegate,int? pretrycount = null,bool? pexponenialbackoff = null)
{
try
{
var T = funcdelegate();
return T;
}
catch(Exception e)
{
if (enableRetryPolicy=="ON" && TransientExceptions.IsTransient(e))
{
int? rcount = pretrycount == null ? retrycount : pretrycount;
bool? exbackoff = pexponenialbackoff == null ? exponentialbackoff : pexponenialbackoff;
int rt = 0;
for (rt = 0; rt < rcount; rt++)
{
if (exponentialbackoff)
{
delayinms = getWaitTimeExp(rt);
}
System.Threading.Thread.Sleep(delayinms);
try
{
var T = funcdelegate();
return T;
}
catch(Exception ex)
{
if (TransientExceptions.IsTransient(ex))
{
int? rcount1 = pretrycount == null ? retrycount : pretrycount;
bool? exbackoff1 = pexponenialbackoff == null ? exponentialbackoff : pexponenialbackoff;
}
else
{
throw;
}
}
}
//throw exception back to caller if exceeded number of retries
if(rt == rcount)
{
throw;
}
}
else
{
throw;
}
}
return default(T);
}
}
I use above method and make a call
public string GetCancelNumber(string property, Guid uid)
{
RetryPolicy<string> rp = new RetryPolicy<string>();
return rp.ExecuteAction(()=>Channel.GetCancelNumber(property, uid, out datasetarray));
}
I keep getting error "cannot use ref or out parameters in anonymous delegate"
Here is an example of a simple Retry method:
bool Retry(int numberOfRetries, Action method)
{
if (numberOfRetries > 0)
{
try
{
method();
return true;
}
catch (Exception e)
{
// Log the exception
LogException(e);
// wait half a second before re-attempting.
// should be configurable, it's hard coded just for the example.
Thread.Sleep(500);
// retry
return Retry(--numberOfRetries, method);
}
}
return false;
}
It will return true if the method succeed at least once, and log any exception until then.
If the method fails on every retry, it will return false.
(Succeed means completed without throwing an exception in this case)
How to use:
Assuming sample Action (void method) and sample Func (a method with a return type)
void action(int param) {/* whatever implementation you want */}
int function(string param) {/* whatever implementation you want */}
Execute a function:
int retries = 3;
int result = 0;
var stringParam = "asdf";
if (!Retry(retries, () => result = function(stringParam)))
{
Console.WriteLine("Failed in all {0} attempts", retries);
}
else
{
Console.WriteLine(result.ToString());
}
Execute an action:
int retries = 7;
int number = 42;
if (!Retry(retries, () => action(number)))
{
Console.WriteLine("Failed in all {0} attempts", retries);
}
else
{
Console.WriteLine("Success");
}
Execute a function with an out parameter (int function(string param, out int num)):
int retries = 3;
int result = 0;
int num = 0;
var stringParam = "asdf";
if (!Retry(retries, () => result = function(stringParam, out num)))
{
Console.WriteLine("Failed in all {0} attempts", retries);
}
else
{
Console.WriteLine("{0} - {1}", result, num);
}
Related
This question already has an answer here:
Why won't a Func with a nullable return type fit into a Dictionary holding Funcs with object return types?
(1 answer)
Closed 9 months ago.
I am trying to create a dictionary to wrap all sql methods and perform retries regardless to the return type. However, making a Func<string, object> does not allow for bool. Is there perhaps a way to make this work?
public T RetryCommand<T>(string cmd)
{
Dictionary<Type, Func<string, object>> mapCmds = new Dictionary<Type, Func<string, object>>
{
{ typeof(DataTable), Query },
{ typeof(object), Scalar },
{ typeof(bool), Execute } /*this is the problem
error states 'bool DAL.Execute(string)' has the wrong return type*/
};
object sendBack = null;
for (int i = 0; i < 3; i++)
{
sendBack = mapCmds[typeof(T)](cmd);
//if this succeeds, break
//if there is an error, try again after a delay
}
return (T)sendBack;
}
Here is the high level definitions of the methods being called
public DataTable Query(string cmd)
{
//return DataTable from Adap.Fill()
}
public object Scalar(string cmd)
{
//return object from SQLCommand.ExecuteScalar
}
public bool Execute(string cmd)
{
//return bool from SQLCommand.ExecuteNonQuery success
}
I have changed a bit your code. But it should fulfit your expectation and it will work.
public T RetryCommand<T>(string cmd, Func<string, int, T> callingMethod, int retryCount = 0)
{
return callingMethod.Invoke(cmd, retryCount);
}
public DataTable Query(string cmd, int retryCount = 3)
{
try
{
return RetryCommand<DataTable>(cmd, Query, retryCount);
}
catch(Exception ex)
{
//Log exception
if(retryCount > 0)
{
return RetryCommand<DataTable>(cmd, Query, retryCount - 1);
}
throw;
}
}
public object Scalar(string cmd, int retryCount = 3)
{
try
{
return RetryCommand<object>(cmd, Scalar, retryCount);
}
catch (Exception ex)
{
//Log exception
if (retryCount > 0)
{
return RetryCommand<object>(cmd, Scalar, retryCount - 1);
}
throw;
}
}
public bool Execute(string cmd, int retryCount = 3)
{
try
{
return RetryCommand<bool>(cmd, Execute, retryCount);
}
catch (Exception ex)
{
//Log exception
if (retryCount > 0)
{
return RetryCommand<bool>(cmd, Execute, retryCount - 1);
}
throw;
}
}
I recommend you to use Polly so you won't need to duplicate same logic for each method but it will be handled by Polly.
I am trying out this piece of code below from:
Is there a way to check if a file is in use?
But, it gives error:
The type arguments for method TimeoutFileAction(Func) cannot be inferred from the usage.
Any idea how to fix this?
TimeoutFileAction(() => { System.IO.File.etc...; return null; } );
Reusable method that times out after 2 seconds
private T TimeoutFileAction<T>(Func<T> func)
{
var started = DateTime.UtcNow;
while ((DateTime.UtcNow - started).TotalMilliseconds < 2000)
{
try
{
return func();
}
catch (System.IO.IOException exception)
{
//ignore, or log somewhere if you want to
}
}
return default(T);
}
You MUST have an output other than Type of void.
When you do this: () => { System.IO.File.etc...; return null; } the output type is void and you cannot have that for a Func<T>. If you want a Void Type then use Action.
If you want both void and T, then just write an overflow method. Se Code below:
public static void Main()
{
var today = new DateTime(2021, 10, 25, 5, 40, 0);
Console.WriteLine(today.AddHours(7).AddMinutes(36));
TimeoutFileAction(() => { Test(); });
TimeoutFileAction(Test);
}
private static string Test() => "Test";
private static void TimeoutFileAction(Action func)
{
var started = DateTime.UtcNow;
while ((DateTime.UtcNow - started).TotalMilliseconds < 2000)
{
try
{
func();
}
catch (IOException exception)
{
//ignore, or log somewhere if you want to
}
}
}
private static T TimeoutFileAction<T>(Func<T> func)
{
var started = DateTime.UtcNow;
while ((DateTime.UtcNow - started).TotalMilliseconds < 2000)
{
try
{
return func();
}
catch (IOException exception)
{
//ignore, or log somewhere if you want to
}
}
return default(T);
}
In the following, I need to pass nextDB to the Lambda expression in Retry:
Retry.Do(() =>
{
string nextDB = dbList.Next();
using (DataBaseProxy repo = new DataBaseProxy(nextDB))
{
return repo.DoSomething();
}
});
How do I do that? Here is my Retry class:
public static class Retry
{
public static void Do(
Action action,
int retryCount = 3)
{
Do<object>(() =>
{
action();
return null;
}, retryCount);
}
public static T Do<T>(
Func<T> action,
int retryCount = 3)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++)
{
try
{
return action();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
}
I think you want to be using Action<T> here. For example:
public static void Do<T>(
Action<T> action,
T param,
int retryCount = 3)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++)
{
try
{
action(param);
return;
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
You would call this function like this:
Do(s => {
Console.WriteLine(s);
}, "test", 3);
Based on your comments, it seems that you want to pass in multiple databases and try each one in succession until you find one that works. One simple option would be to remove to retryCount and instead pass in your array.
public static void Do<T>(
Action<T> action,
IEnumerable<T> items)
{
var exceptions = new List<Exception>();
foreach(var item in items)
{
try
{
action(item);
return;
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
And now you call it something like this:
Do(s => {
Console.WriteLine(s);
}, new[] { "db1", "db2", "db3" });
The given function returns a KPI value, first it checks its cache, then it performs its logic, caches the result and returns a value, handling a failure condition.
How am I best to re-use the caching, error handling logic. What I essentially want to create is a function that just performs the necessary logic with boiler plate code abstracted away and re-used across multiple similar functions.
public static int CurrentEmployees()
{
if (HttpRuntime.Cache["CurrentEmployees"] == null)
{
try
{
int CurrentEmployees = Employee.Load().Count(x => x.DateFinished == null && !x.Contractor && x.DateStarted < DateTime.Now);
HttpRuntime.Cache.Insert("CurrentEmployees", CurrentEmployees, null, DateTime.Now.AddMinutes(20), new TimeSpan(0, 10, 0));
return CurrentEmployees;
}
catch(Exception e)
{
//TODO: Report this
return -1;
}
}
else
return (int)HttpRuntime.Cache["CurrentEmployees"];
}
As the boilerplate code is wrapped around the logic it is difficult for me to simply push these into other function calls.
Here's how you could create a generic method to cache whatever you want and reuse this logic.
public static T Cache<T>(string key, Func<T> loadFunction, Func<T> errorHandler)
{
if (HttpRuntime.Cache[key] == null)
{
try
{
T value = loadFunction();
HttpRuntime.Cache.Insert(key, value , null, DateTime.Now.AddMinutes(20), new TimeSpan(0, 10, 0));
return value;
}
catch(Exception e)
{
//TODO: Report this
return errorHandler();
}
}
else
return (T)HttpRuntime.Cache[key];
}
Usage:
public static int CurrentEmployees()
{
return Cache<int>("CurrentEmployees",
() => Employee.Load().Count(x => x.DateFinished == null && !x.Contractor && x.DateStarted < DateTime.Now),
() => -1);
}
Agree with answer from #DLeh but I would write it like this:
public static class HelperFunctions
{
public static Func<T> Cache<T>(this Func<T> inner, string cacheName)
{
return () =>
{
if (HttpRuntime.Cache[cacheName] == null)
{
var result = inner();
HttpRuntime.Cache.Insert(cacheName, inner(), null, DateTime.Now.AddMinutes(20), new TimeSpan(0, 10, 0));
return result;
}
return (T)HttpRuntime.Cache[cacheName];
};
}
public static Func<T> OnError<T>(this Func<T> inner, Func<Exception, T> onError)
{
return () =>
{
try
{
return inner();
}
catch (Exception e)
{
return onError(e);
}
};
}
}
Usage:
public static class Employees
{
public static int CurrentEmployees()
{
return (new Func<int>(() => Employee.Load().Count(x => x.DateFinished == null && !x.Contractor && x.DateStarted < DateTime.Now)))
.Cache("CurrentEmployees")
.OnError(e => -1)
();//TODO: log?
}
}
This way we separate caching logic from error handling (following single responsibility principle) and are able to reuse/compose each separately. So when you add another function like this you won't have to change Caching function.
I know the question sounds a bit wierd. Sorry for that, having difficulties trying to get where i want to and even explain it. To make it simple, i have a method with Func<T> argument. But i do not always pass a parameterless Action to that method, i need varying numbers of parameters and i'm trying to find a way to avoid overloading my method everytime number of parameters needed is increased
Here is my generic class, I need to overload GetInstance method:
public class MethodResult<T>
{
public T Result { get; set; }
public bool IsResulted { get; set; }
public Exception Error { get; set; }
private MethodResult() { }
public static MethodResult<T> GetInstance<T>(Func<T> method)
{
MethodResult<T> obj = new MethodResult<T>();
try
{
obj.Result = method();
obj.IsResulted = true;
obj.Error = null;
}
catch (Exception ex)
{
obj.Result = default(T);
obj.IsResulted = false;
obj.Error = ex;
}
return obj;
}
public static MethodResult<T> GetInstance<T, T1>(Func<T1, T> method, T1 param1)
{
MethodResult<T> obj = new MethodResult<T>();
try
{
obj.Result = method(param1);
obj.IsResulted = true;
obj.Error = null;
}
catch (Exception ex)
{
obj.Result = default(T);
obj.IsResulted = false;
obj.Error = ex;
}
return obj;
}
}
And here is sample showing how i want to make use of it:
public static void Main(string[] args)
{
var x = MethodResult<int>.GetInstance(IntResult, 5);
Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", x.Result, x.IsResulted, (x.Error == null ? "null" : x.Error.Message));
var y = MethodResult<string>.GetInstance(SayHello);
Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", y.Result, y.IsResulted, (y.Error == null ? "null" : y.Error.Message));
Console.Read();
}
public static int IntResult(int x) { return x + 1; }
public static int IntResult(int x, int y) { return x + y; }
public static string SayHello() { return "Hello world!"; }
In order to be able to use IntResult(int x, int y) i have to overload GetInstance method with signiture:
public static MethodResult<T> GetInstance<T, T1, T2>(Func<T1, T2, T> method, T1 param1, T2 param2)
It's obvious that this will become very time consuming as it's already been annoying. Is there a way to avoid that much overloading ?
Instead of passing the function to call and the parameters along with it, you can pass a parameterless anonymous delegate calling the method you want to point to with parameters you want. Just remove any overloads of GetInstance just keep:
public static MethodResult<T> GetInstance(Func<T> method)
{
MethodResult<T> obj = new MethodResult<T>();
try
{
obj.Result = method();
obj.IsResulted = true;
obj.Error = null;
}
catch (Exception ex)
{
obj.Result = default(T);
obj.IsResulted = false;
obj.Error = ex;
}
return obj;
}
And then call it as following:
int n = 1;
var x = MethodResult<string>.GetInstance(() => SayHello());
var y = MethodResult<string>.GetInstance(() => IntResult(2));
var z = MethodResult<int>.GetInstance(() => IntResult(n, 9));