I have a WCF service running in a server and a Windows Application which has a timer each 30 seconds checking by WCF some news values from database.
Everything is going well but if my server (when WCF is running) get offline or out for some reason, I get the Exception System.Reflection.TargetInvocationException or System.Net.WebExceptionStatus.ConnectFailure.
What I want is, well, my timer will check every 30 seconds and I want reestablish the connection when my server come back. Actually the only way to do it is close and open the WinForm app.
How can I check if the connection is back or reconnect without close my app?
public MyClass()
{
proxy = new TaskService.Service1Client();
proxy.GetTarefasCompleted += new EventHandler<GetTarefasCompletedEventArgs>
(proxy_GetTarefasCompleted);
timer = new System.Threading.Timer(Callback, null, 0, 30000);
}
private void Callback(Object state)
{
timer.Change(30000, Timeout.Infinite);
proxy.GetTarefasAsync(Environment.UserDomainName, Environment.UserName);
}
void proxy_GetTarefasCompleted(object sender, GetTarefasCompletedEventArgs e)
{
try
{
tarefas = e.Result.OrderByDescending(t => t.Id).ToList();
//code code code
}
catch (Exception ex)
{
if (ex.GetType().ToString() == "System.Net.WebExceptionStatus.ConnectFailure" ||
ex.GetType().ToString() == "System.Reflection.TargetInvocationException")
{
//treat the error
}
}
}