[英]WCF Service Retry
我试图找出在发送故障时是否有一种简单的方法来实现对服务的重试。 比如说:
Private Function SaveEmployee(emp as Empoyee)
Try
...
returnval = service.SaveEmployee(emp)
Catch ex as exception
'if exception requires retry eg:end point not found or database is not responding then
'call retry func/class
RetryOperation(...)
End Try
End Function
在上面的示例中,如何创建一个通用的RetryOperation类,该类可以接受任何函数并在通知用户无法完成操作之前以一定间隔调用它3或4次。
我希望可以制作一个通用方法,而不是在所有服务调用函数中都有重复的代码
C#或vb.net中的任何样本都将非常感激。
谢谢
如果是一个你可能想要重复的呼叫,如果它失败了,为什么不立即使用重复功能,如果服务呼叫成功,它只会被调用一次,如果服务调用失败,它将重试x次,如果它在x上失败:它会抛出异常
这样的事情怎么样,请注意这是大大简化了,你需要添加错误处理等等:
像这样创建你的重复方法:
private void RepeatCall(int numberOfCalls, Action unitOfWork)
{
for (int i = 1; i <= numberOfCalls; i++)
{
try
{
unitOfWork();
}
catch (...)
{
// decide which exceptions/faults should be retried and
// which should be thrown
// and always throw when i == numberOfCalls
}
}
}
像这样使用它
try
{
RepeatCall(3, () =>
{
MyServiceCall();
});
}
catch(....)
{
// You'll catch here same as before since on the last try if the call
// still fails you'll get the exception
}
VB.NET中也是如此
Private Sub RepeatCall(ByVal numberOfCalls As Integer, ByVal unitOfWork As Action)
For i = 1 To numberOfCalls
Try
unitOfWork()
Catch ex As Exception
End Try
Next
End Sub
用它:
Try
RepeatCall(3, Sub()
MyServiceCall()
End Sub)
Catch ex As Exception
End Try
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.