private void KeepAlive(object obj) { List <KeyValuePair <string, string> > kList = obj as List <KeyValuePair <string, string> >; string userCode = kList.Where(p => p.Key == "UserCode").FirstOrDefault().Value; string urlPort = kList.Where(p => p.Key == "UrlPort").FirstOrDefault().Value; var client = new WCFClient(); client.WcfMessageEvent += client_WcfMessage; var ctx = new InstanceContext(client); EndpointAddress address = new EndpointAddress("net.tcp://" + urlPort + "/JAJ.WinServer"); System.ServiceModel.Channels.Binding binding = new NetTcpBinding("netTcpExpenseService_ForSupplier"); var proxy = new WcfSvc.MessageServiceClient(ctx, binding, address); while (true) { Thread.Sleep(3000); try { proxy.Update(userCode); } catch { try { client = new WCFClient(); client.WcfMessageEvent += client_WcfMessage; ctx = new InstanceContext(client); proxy = new WcfSvc.MessageServiceClient(ctx, binding, address); proxy.Login(userCode); } catch (Exception ex) { MyLog.LogError("重连异常", ex); } } } }
private bool login(string userCode, ref bool correctVersion) { string versionNo = ConfigurationManager.AppSettings["Version"]; string messageUrlPort = string.Empty; using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString())) { var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault(); if (currVersion == null) { this.Invoke(new MethodInvoker(() => { MessageBox.Show("数据库中系统版本不存,在请系统联系管理员!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); return(false); } if (versionNo != currVersion.VersionNo) { this.Invoke(new MethodInvoker(() => { MessageBox.Show("版本已经升级,请点击确定后下载最新版客户端!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); correctVersion = false; return(false); } var user = zentity.SYS_User.Where(p => p.UserCode == userCode).FirstOrDefault(); if (user == null) { this.Invoke(new MethodInvoker(() => { MessageBox.Show("当前用户不存在,请联系管理员!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); return(false); } else { UserInfo userInfo = UserInfo.GetInstence(); userInfo.UserCode = user.UserCode; userInfo.UserName = user.UserName; userInfo.Tel = user.Tel; userInfo.QQ = user.QQ; userInfo.LoginTime = DateTime.Now; userInfo.DefaultProjectCode = user.DefaultProjectCode; messageUrlPort = user.UrlPort; } } try { //urlPort = "localhost:9119"; var client = new WCFClient(); client.WcfMessageEvent += client_WcfMessage; var ctx = new InstanceContext(client); EndpointAddress address = new EndpointAddress("net.tcp://" + messageUrlPort + "/JAJ.WinServer"); System.ServiceModel.Channels.Binding binding = new NetTcpBinding("netTcpExpenseService_ForSupplier"); var proxy = new WcfSvc.MessageServiceClient(ctx, binding, address); proxy.Login(userCode); } catch (Exception ex) { this.Invoke(new MethodInvoker(() => { MessageBox.Show("消息服务启动失败!\r\n问题跟踪功能可正常使用,请点击确定后继续使用系统"); })); MyLog.LogError("消息服务启动失败", ex); } finally { #region 开启新的线程保持连接 List <KeyValuePair <string, string> > kList = new List <KeyValuePair <string, string> >(); kList.Add(new KeyValuePair <string, string>("UserCode", userCode)); kList.Add(new KeyValuePair <string, string>("UrlPort", messageUrlPort)); _thread = new Thread(KeepAlive); _thread.IsBackground = true; _thread.Start(kList); #endregion } return(true); }