using Forks.JsonRpc.Client; using Forks.JsonRpc.Client.Data; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; namespace BO.Utils { public class LoginRpcUtil { public static string GetUserNameByCode(string code, out string error) { try { error = string.Empty; const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserName"; return RpcFacade.Call(wpfUserMethod, code); } catch (Exception ex) { error = ex.ToString(); } return string.Empty; } public static void FillUserEmpInfo(string name, LoginUserInfo userInfo) { const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserEmpInfo"; var obj = RpcFacade.Call(wpfUserMethod, name); if (obj != null) { userInfo.ID = obj.Get("User_ID"); userInfo.UserName = obj.Get("User_Name"); userInfo.Domain_ID = obj.Get("Domain_ID"); userInfo.AccountingUnit_ID = obj.Get("AccountingUnit_ID"); userInfo.AccountingUnit_Name = obj.Get("AccountingUnit_Name"); userInfo.Department_ID = obj.Get("Department_ID"); userInfo.Department_Name = obj.Get("Department_Name"); userInfo.Employee_ID = obj.Get("Employee_ID"); userInfo.Employee_Name = obj.Get("Employee_Name"); userInfo.Role = obj.Get("Role"); } } public static bool TestConnection() { var url = ButcherAppContext.Context.UrlConfig.ServerUrl; if (string.IsNullOrEmpty(url)) return false; //try //{ //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); var uri = new Uri(url); return TestConnection(uri.Host, uri.Port, 50); // HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); // return resp.StatusCode == HttpStatusCode.OK; //} //catch //{ // return false; //} } //public static bool TestConnection() //{ // try // { // Dns.GetHostEntry(ButcherAppContext.Context.UrlConfig.ServerUrl); //using System.Net; // return true; // } // catch (System.Net.Sockets.SocketException ex) // { // return false; // } // //try // //{ // // Ping objPingSender = new Ping(); // // PingOptions objPinOptions = new PingOptions(); // // objPinOptions.DontFragment = true; // // string data = ""; // // byte[] buffer = Encoding.UTF8.GetBytes(data); // // int intTimeout = 120; // // PingReply objPinReply = objPingSender.Send(ButcherAppContext.Context.UrlConfig.ServerUrl, intTimeout, buffer, objPinOptions); // // string strInfo = objPinReply.Status.ToString(); // // if (strInfo == "Success") // // { // // return true; // // } // // else // // { // // return false; // // } // //} // //catch (Exception) // //{ // // return false; // //} //} public static bool TestConnection(string host, int port, int millisecondsTimeout) { var client = new System.Net.Sockets.TcpClient(); try { var ar = client.BeginConnect(host, port, null, null); ar.AsyncWaitHandle.WaitOne(millisecondsTimeout); return client.Connected; } catch (Exception) { return false; } finally { client.Close(); } } } }