/// According to the specified information, call the remote WebService method /// </summary> /// <param name="url">WebService's http address</param> /// <param name="nameSpace">Namespace of the WebService to be called</param> /// <param name="classname">The class name of the WebService to be called (excluding the namespace prefix)</param> /// <param name="methodname">The method name of the WebService to be called</param> /// <param name="args">Parameter list</param> /// <returns>WebService execution result</returns> private object InvokeWebservice(string url, string nameSpace, string classname, string methodname, object[] args) { try { //1. Use WebClient to download WSDL information WebClient wc = new WebClient(); Stream stream = wc.OpenRead(url + "?WSDL"); //2. Create and format the WSDL document ServiceDescription srvDesc = ServiceDescription.Read(stream); //3. Create client proxy proxy class ServiceDescriptionImporter srvDescInporter = new ServiceDescriptionImporter(); srvDescInporter.ProtocolName = "Soap";//Specify the access protocol srvDescInporter.Style = ServiceDescriptionImportStyle.Client;//Generate client proxy, default. srvDescInporter.AddServiceDescription(srvDesc, "", ""); //Add WSDL document. //4. Use CodeDom to compile the client proxy class. CodeNamespace codeNamespce = new CodeNamespace(nameSpace); CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); codeCompileUnit.Namespaces.Add(codeNamespce); srvDescInporter.Import(codeNamespce, codeCompileUnit); CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); // Represents the parameters used to invoke the compiler. System.CodeDom.Compiler.CompilerParameters parameter = new System.CodeDom.Compiler.CompilerParameters(); parameter.GenerateExecutable = false; //Set whether to generate executable files. parameter.GenerateInMemory = true; //Set whether to generate output in memory. parameter.ReferencedAssemblies.Add("System.dll"); //ReferencedAssemblies Get the assemblies referenced by the current project. parameter.ReferencedAssemblies.Add("System.XML.dll"); parameter.ReferencedAssemblies.Add("System.Web.Services.dll"); parameter.ReferencedAssemblies.Add("System.Data.dll"); //Get the compilation result returned from the compiler. System.CodeDom.Compiler.CompilerResults cr = provider.CompileAssemblyFromDom(parameter, codeCompileUnit); 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()); } // Get the compiled assembly, then make a dynamic call via reflection. System.Reflection.Assembly assembly = cr.CompiledAssembly; Type t = assembly.GetType(nameSpace + "." + classname, true, true); // If you added a namespace to the proxy class earlier, you need to add the namespace to the type here. object obj = Activator.CreateInstance(t); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return mi.Invoke(obj, args); } catch (Exception ex) { return ex.Message; } }
C# dynamically call webservice
Due to project requirements, multiple webservices need to be called, so service references cannot be used. Find a dynamic calling code on the Internet, the test is available, and keep it here for reuse in the future
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326474082&siteId=291194637
Recommended
Ranking