动态调用webservice的函数

来源:互联网 发布:江恩轮中轮软件 编辑:程序博客网 时间:2024/05/17 01:19
        /// <summary>        /// call webmethod dynamically        /// </summary>        /// <param name="url">the url of the webservice</param>        /// <param name="myNamespace">the namespace which the class belongs to </param>        /// <param name="className">the class which the webmothed belongs to</param>        /// <param name="methodName">the method which you are calling</param>        /// <param name="args">the parameters which the method takes</param>        /// <returns>the object which the method you called returns</returns>        protected object GetService(string url, string myNamespace, string className, string methodName, object[] args)        {            //1. 使用 WebClient 下载 WSDL 信息。            WebClient web = new WebClient();            Stream stream = web.OpenRead(url + "?WSDL");            //2. 创建和格式化 WSDL 文档。            ServiceDescription description = ServiceDescription.Read(stream);            //3. 创建客户端代理代理类。            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();            importer.ProtocolName = "Soap";            importer.Style = ServiceDescriptionImportStyle.Client;            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;            importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。            //4. 使用 CodeDom 编译客户端代理类。            CodeNamespace nmspace = new CodeNamespace(myNamespace);            CodeCompileUnit unit = new CodeCompileUnit();            unit.Namespaces.Add(nmspace);            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");            CompilerParameters parameter = new CompilerParameters();            parameter.GenerateExecutable = false;            parameter.GenerateInMemory = true;            parameter.ReferencedAssemblies.Add("System.dll");            parameter.ReferencedAssemblies.Add("System.XML.dll");            parameter.ReferencedAssemblies.Add("System.Web.Services.dll");            parameter.ReferencedAssemblies.Add("System.Data.dll");            CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);            //5使用 Reflection 调用 WebService。            if (!result.Errors.HasErrors)            {                Assembly asm = result.CompiledAssembly;                Type t = asm.GetType(myNamespace + "." + className);                object o = Activator.CreateInstance(t);                MethodInfo method = t.GetMethod(methodName);                object tempObj = (method.Invoke(o, args));                return tempObj;            }            else            {                System.Text.StringBuilder sb = new System.Text.StringBuilder();                foreach (System.CodeDom.Compiler.CompilerError ce in result.Errors)                {                    sb.Append(ce.ToString());                    sb.Append(System.Environment.NewLine);                }                throw new Exception(sb.ToString());            }        }