WCF服务重试
问题描述:
我想弄清楚是否有一个简单的方法,当它发送错误时执行服务的重试。上述样品我怎样才能使可采取任何功能并调用它具有间隔3或4倍,通知该操作不能完成用户之前的通用RetryOperation类WCF服务重试
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
:让说对于例如。
我希望它可以使一个通用的方法,而不是有重复的代码在所有的服务通话功能
在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
感谢您的答复,我是有点糊涂了,是你通过dafult建议调用RepeatCall,然后发送异常?我想要的是客户端函数将调用服务函数,如果它引发异常,则只应调用RepeatCall。还是我完全误解了你的建议? – melspring
我更新了我的答案,上面看到,没有什么能够阻止您使用我的代码示例执行您在问题中描述的内容,我只是描述了我使用的一种方法。通过这种方式,您可以将重试逻辑放入RepeatCall方法中,而不是将其重复用于每次服务调用。 –
再次感谢,这是有道理的,并消除方法catch-throw。只是想知道RepeatCall是否可以在自己的线程上运行?你有什么建议吗? – melspring