/* * 调用实例 * object[] args = new object[1]; * args[0] = planid; * object result = WebservicesHelper.InvokeWebService("http://c-wuxts01:8888/PRM_API/PRM_API.asmx", "Return_PlanData", args); * return (DataSet)result; */ /// < summary> /// 动态调用web服务 /// < /summary> /// < param name="url">WSDL服务地址< /param> /// < param name="methodname">方法名< /param> /// < param name="args">参数< /param> /// < returns>< /returns> public static object InvokeWebService(string url, string methodname, object[] args) { try { return(WebservicesHelper.InvokeWebService(url, null, methodname, args)); } catch (Exception ex) { LogHelp.WriteLog("InvokeWebService异常,url:" + url + "methodname" + methodname, ex); return(null); } }
/// <summary> /// 动态调用web服务 /// </summary> /// <param name="url">WSDL服务地址</param> /// <param name="classname">服务接口类名</param> /// <param name="methodname">方法名</param> /// <param name="args">参数值</param> /// <returns></returns> public static object InvokeWebService(string url, string classname, string methodname, object[] args) { string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; if ((classname == null) || (classname == "")) { classname = WebservicesHelper.GetWsClassName(url); } try { //获取WSDL WebClient wc = new WebClient(); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream); //注意classname一定要赋值获取 classname = sd.Services[0].Name; ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider icc = new CSharpCodeProvider(); //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInMemory = true; cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); if (true == cr.Errors.HasErrors) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //生成代理实例,并调用方法 System.Reflection.Assembly assembly = cr.CompiledAssembly; Type t = assembly.GetType(@namespace + "." + classname, true, true); object obj = Activator.CreateInstance(t); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return(mi.Invoke(obj, args)); } catch (Exception ex) { LogHelp.WriteLog("InvokeWebService异常", ex); return(null); } }
private void AutoUpdate() { //调用函数 //参数集合 try { string version = MyConfig.Version; if (version != null) { //去掉version中的非数字字符 string lastch = version.Substring(version.Length - 1, 1); while (string.CompareOrdinal(lastch, "0") < 0 || string.CompareOrdinal(lastch, "9") > 0) { version = version.Remove(version.Length - 1); lastch = version.Substring(version.Length - 1, 1); } ////去掉version中的最后一个".",如将3.2.0变为3.20,不然更新服务器不识别版本 by Yunxiao Lin 20180526 //string[] versionNumbers = version.Split('.'); //if (versionNumbers.Count() == 3) // version = versionNumbers[0] + "." + versionNumbers[1] + versionNumbers[2]; } object[] args = new object[4]; args[0] = version; if (Registration.IsSuperUser()) { args[1] = "All"; //注意All必须第一个字母大写,后面的小写,不然不更新。 } else { args[1] = Registration.GetRegionCode(); } args[2] = "VRF"; string file = "PatchUpdate.exe"; Configuration config = ConfigurationManager.OpenExeConfiguration(Application.StartupPath + @"\" + file); if (config.AppSettings.Settings["FileType"] == null) { return; } args[3] = config.AppSettings.Settings["FileType"].Value.ToString(); //20180628 更新服务接口配置文件结构变更 20180628 by Yunxiao Lin var kWSRoot = config.AppSettings.Settings["WSPath"]; var kPatchPath = config.AppSettings.Settings["RootPath"]; var kIps = config.AppSettings.Settings["IpList"]; var kTimeOut = config.AppSettings.Settings["TimeOut"]; if (kWSRoot == null || kPatchPath == null || kIps == null || kTimeOut == null) { return; } string WSRoot = kWSRoot.Value.ToString(); //webservics地址 string PatchPath = kPatchPath.Value.ToString(); //补丁下载路径 string Ips = kIps.Value.ToString(); //ip集合 int TimeOut = int.Parse(kTimeOut.Value.ToString()); //链接超时 List <string> IpList = Ips.ToString().Split(';').ToList(); bool HasConifgrue = false; //在连接之前先ping foreach (string ip in IpList) { if (ip != "" && Ping(ip, TimeOut)) { WSRoot = string.Format(WSRoot, ip); PatchPath = string.Format(PatchPath, ip); HasConifgrue = true; break; } } //如果都ping不通则退出更新 if (!HasConifgrue) { return; } //设置参数 string pargs = args[0] + " " + args[1] + " " + args[2]; //添加代理,不然在外网会连不上服务 System.Net.WebProxy webProxy = new System.Net.WebProxy(WSRoot, true); webProxy.Credentials = new System.Net.NetworkCredential("1", "1"); System.Net.WebRequest.DefaultWebProxy = webProxy; //连接服务 object result = WebservicesHelper.InvokeWebService(WSRoot, "HasPatch", args); if (result != null) { bool HasPatch = (bool)result; if (HasPatch) { //启动自动更新客户端 //MessageBox.Show("AutoUpdate"); Process.Start(Application.StartupPath + @"\" + file, pargs); //Process.Start(@"C:\SVN\GVRF\DesktopVRF\PatchUpdate\bin\Debug\"+file, pargs); //Process.Start(@"C:\SVN\GVRF\DesktopVRF\PatchUpdate\bin\Debug\" + file); } } } catch (Exception) { return; } }