我有下面的映射器 json 文件,它告诉我给定的异常,我想使用哪个错误处理策略。
{
"ExceptionMappers": [
{
"ExceptionName": "TimeoutException",
"ExceptionType": "Transient",
"Policy": "WaitAndRetry",
"WaitType": "Linear"
},
{
"ExceptionName": "DivideByZeroException",
"ExceptionType": "Permanent",
"Policy": "CircuitBreaker"
},
{
"ExceptionName": "StackOverflowException",
"ExceptionType": "LogOnly",
"Policy": "WaitAndRetry",
"WaitType": "Linear"
}
]
}
然后使用下面的代码,我试图获取异常类型并应用可以调用操作的策略。但是在这里我被卡住了,如何从字符串名称中获取异常类型。
public void Execute(Action action, string exceptionName)
{
var filePath = @"appsetting.json";
var exceptionMapperJson = System.IO.File.ReadAllText(filePath);
var rootNode = JsonConvert.DeserializeObject<RootObject>(exceptionMapperJson);
var exceptionNode = rootNode.ExceptionMappers.FirstOrDefault(e => e.ExceptionName.Equals(exceptionName));
var exceptionObject = Type.GetType(exceptionName);
if (exceptionNode != null)
{
// Here I need the exception from the string value
Policy.Handle<TimeoutException>().Retry().Execute(action);
}
else
{
// No Policy applied
Policy.NoOp().Execute(action);
}
}