问题描述
嗨,我正在.Net核心应用程序中实现断路器。我正在使用.net sdk调用azure数据工厂。我正在关注https://github.com/bmaluijb/CloudDesignPatterns/tree/master/CircuitBreakerPattern
这个想法是,如果我无法使用.net代码调用adf,并且如果连续发生五次,我想破坏它。所以下面是我的实现。
下面是断路器的实现。
public class CircuitBreaker
{
private readonly ICircuitBreakerStateStore stateStore
= new CircuitBreakerStateStore();
private TimeSpan OpenTimeOutTime = new TimeSpan(0,10);
public void ExecuteAction(Action action)
{
if (!stateStore.IsClosed)
{
if (stateStore.LastChangedDateUtc + OpenTimeOutTime < DateTime.UtcNow)
{
try
{
stateStore.HalfOpen();
action();
stateStore.Reset();
return;
}
catch (Exception ex)
{
stateStore.Trip(ex);
throw;
}
}
throw new CircuitBreakerOpenException(stateStore.LastException);
}
try
{
action();
}
catch (Exception ex)
{
TrackException(ex);
throw;
}
}
private void TrackException(Exception ex)
{
stateStore.Trip(ex);
}
}
下面是我在某些类中的方法。
circuitBreaker.ExecuteAction(() =>
{
adfRunId = azureDataFactoryRepository.RunADFPipeline(dataFactoryManagementClient,parameters,_ADFClient,ApplicationConstants.ProcessMapUploadPipeLine);
});
}
catch (CircuitBreakerOpenException ex)
{
// The circuit breaker is open,so the call wasn't made
Console.WriteLine(DateTime.UtcNow.ToLongTimeString()
+ " CallMe not called " + ex.InnerException.Message);
}
catch (Exception ex)
{
//The call was made and failed
Console.WriteLine(DateTime.UtcNow.ToLongTimeString()
+ " CallMe failed " + ex.Message);
}
下面是我的方法,它调用了Azure数据工厂。
private static int failureCount = 0;
public string RunADFPipeline(DataFactoryManagementClient dataFactoryManagementClient,Dictionary<string,object> keyValuePairs,ADFClient aDFClient,string pieplineName)
{
CreateRunResponse runResponse = dataFactoryManagementClient.Pipelines.CreateRunWithHttpMessagesAsync(aDFClient.ResourceGroupName,aDFClient.DataFactoryName,pieplineName,parameters: keyValuePairs).Result.Body;
//if(error) then failureCount ++ // I want to increment count
return runResponse.RunId;
}
我通过传递错误的管道名称使上述代码失败,并且返回错误。每当它返回错误时,我要增加failureCount。我的问题是执行时遇到的错误直接在CircuitBreaker.cs中捕获异常
catch (Exception ex)
{
TrackException(ex);
}
因此发生异常后,我没有机会增加计数器。所以在我下面的方法中
CreateRunResponse runResponse = dataFactoryManagementClient.Pipelines.CreateRunWithHttpMessagesAsync(aDFClient.ResourceGroupName,parameters: keyValuePairs).Result.Body;
发生异常时如何增加计数器。一旦上面的代码中发生异常,我想如何增加计数器。那我在哪里可以做呢?例如,我在下面看到了一些东西
if (testNumber % 2 == 0 && failureCount < 5)
{
failureCount++;
}
在我的情况下,如何添加上述逻辑来增加计数器?有人可以帮我吗?任何帮助将不胜感激。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)