微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动态调用webservice的函数

        /// <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());

            }

        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐