由於公司項目需要研究webservice由來已久,webservice的效率問題一直是個讓人頭疼的事情.
N就前,鄙人研究了動態反射形式的動態形webservice,也大規模應用在了項目裡,怎奈效率卻低下到一定程度.
具體代碼鄙人也貼出來,也就是網上流行的那個動態調用方法
///<summary> ///動態Web服務代理方法 ///</summary> ///<param name="@namespace">傳入空間命名</param> ///<param name="url">傳入URL參數</param> ///<param name="classname">傳入類名</param> ///<param name="methodname">傳入方法名</param> ///<param name="args">傳入參數,本參數為順序參數</param> public object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args) { try { System.Net.WebClient wc = new System.Net.WebClient(); System.IO.Stream stream = wc.OpenRead(url + "?WSDL"); System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace); System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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"); System.CodeDom.Compiler.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) { throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); } }
動態是被我動態上了,可惜效率卻太不行了,今天實在感覺到不行了,就測試了靜態.
不測不知道,一測試嚇一跳.
動態方法
用了 1.29秒才加載完畢
然後我直接用靜態測試後
只用了0.43秒
差距實在太大了.
不知道各位有沒有動態調用的方法,最起碼要實現URL是動態的.
如有知道麻煩告之,謝謝!