Created
February 28, 2013 14:42
-
-
Save bobbychopra/5057209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Reflection; | |
using System.ServiceModel; | |
using log4net; | |
namespace Bobby.Chopra | |
{ | |
static class SomeService | |
{ | |
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
private static ISomeService Svc | |
{ | |
get | |
{ | |
lock (sync) | |
if (_svc == null) | |
_svc = new ChannelFactory<ISomeService>("*").CreateChannel(); | |
return _svc; | |
} | |
} | |
static readonly object sync = new object(); | |
static ISomeService _svc; | |
internal static T Retry<T>(Func<T> func, string msg, bool throwException = false) | |
{ | |
T val = default(T); | |
var noOfRetries = int.Parse(ConfigurationManager.AppSettings["NoOfRetriesForNtfService"]); | |
var success = false; | |
for (var tries = 0; tries < noOfRetries && !success; tries++) | |
{ | |
System.Threading.Thread.Sleep(tries * 1000); | |
try | |
{ | |
val = func(); | |
success = true; | |
} | |
catch (Exception ex) | |
{ | |
Logger.WarnFormat("No Of Tries: {0} \r\nMessage:{1} \r\nException:{2}", tries, msg, ex); | |
} | |
} | |
if (!success && throwException) | |
{ | |
Logger.Error("Could not get " + msg); | |
throw new Exception("Could not get " + msg); | |
} | |
return val; | |
} | |
public static double TotalReturn(string ticker, DateTime startDate, DateTime endDate) | |
{ | |
var msg = String.Format("Total Return for ticker:{0}, start:{1}, end:{2}", ticker, startDate, endDate); | |
return Retry(() => _TotalReturn(ticker, startDate, endDate), msg, true); | |
} | |
private static double _TotalReturn(string ticker, DateTime startDate, DateTime endDate) | |
{ | |
return var s = Svc.GetData( ... ); | |
} | |
public static double? MethodB(string ticker, DateTime date) | |
{ | |
var msg = String.Format("MethodB for ticker:{0}, date:{1}", ticker, date); | |
return Retry(() => _MethodB(ticker, date), msg); | |
} | |
private static double? _MethodB(string ticker, DateTime date) | |
{ | |
var s = Svc.GetData( ... ); | |
return string.IsNullOrWhiteSpace(s) ? null : new double?(double.Parse(s)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment