Connection 获取连接失败后,如何连接到WCF?
我有一个运行在服务器上的WCF服务和一个Windows应用程序,它有一个计时器,每30秒由WCF检查数据库中的一些新闻值 一切都很顺利,但如果我的服务器(在WCF运行时)由于某种原因脱机或退出,则会出现异常System.Reflection.TargetInitiationException或System.Net.WebExceptionStatus.ConnectFailure 我想要的是,我的计时器将每30秒检查一次,我希望在服务器返回时重新建立连接。实际上,唯一的方法是关闭并打开WinForm应用程序 在不关闭应用程序的情况下,如何检查连接是否已恢复或重新连接Connection 获取连接失败后,如何连接到WCF?,connection,targetinvocationexception,Connection,Targetinvocationexception,我有一个运行在服务器上的WCF服务和一个Windows应用程序,它有一个计时器,每30秒由WCF检查数据库中的一些新闻值 一切都很顺利,但如果我的服务器(在WCF运行时)由于某种原因脱机或退出,则会出现异常System.Reflection.TargetInitiationException或System.Net.WebExceptionStatus.ConnectFailure 我想要的是,我的计时器将每30秒检查一次,我希望在服务器返回时重新建立连接。实际上,唯一的方法是关闭并打开WinFo
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
}
}
}
publicmyclass()
{
proxy=new TaskService.Service1Client();
proxy.GetTarefasCompleted+=新事件处理程序
(委托书已完成);
timer=newsystem.Threading.timer(回调,null,0,30000);
}
私有无效回调(对象状态)
{
定时器。更改(30000,超时。无限);
gettarefasync(Environment.UserDomainName,Environment.UserName);
}
无效代理\u GetTarefasCompleted(对象发送方,GetTarefasCompletedEventArgs e)
{
尝试
{
tarefas=e.Result.OrderByDescending(t=>t.Id).ToList();
//代码
}
捕获(例外情况除外)
{
if(例如GetType().ToString()=“System.Net.WebExceptionStatus.ConnectFailure”||
例如:GetType().ToString()=“System.Reflection.TargetInvocationException”)
{
//对待错误
}
}
}
if(proxy.State!=System.ServiceModel.CommunicationState.Opened)
{
proxy.Abort();
proxy.Open();
}
我相信这应该能奏效