ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 动态调用web服务

动态调用web服务

原创 Linux操作系统 作者:tclywork 时间:2019-05-10 17:54:06 0 删除 编辑

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.ApplicationServices;
using System.Net;
using System.IO;
using System.Web.Services.Description;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Xml;
using System.CodeDom;
using System.Windows.Forms;

namespace TEWeb2.CommonClass
{
    public class ServiceClass
    {
        #region 动态调用web服务

        public static object InvokeWebService(string url, string methodname, object[] args)
        {
            return ServiceClass.InvokeWebService(url, null, methodname, args);
        }

        public static object InvokeWebService(string url, string classname, string methodname, object[] args)
        {
            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((classname == null) || (classname == ""))
            {
                classname = ServiceClass.GetWsClassName(url);
            }

            try
            {
                //获取WSDL
                WebClient wc = new WebClient();
                Stream stream = wc.OpenRead(url + "?WSDL");
                ServiceDescription sd = ServiceDescription.Read(stream);
                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 csc = new CSharpCodeProvider();
                ICodeCompiler icc = csc.CreateCompiler();

                //设定编译参数
                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 bj = 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));
            }
        }

        private static string GetWsClassName(string wsUrl)
        {
            string[] parts = wsUrl.Split('/');
            string[] pps = parts[parts.Length - 1].Split('.');

            return pps[0];
        }
        ///


        /// 引用实例
        ///

        ///
        private void TestService()
        {
            string xmlPath = Application.StartupPath + @"\MiddleDB.xml";    //从XML中读取
            XmlDocument xd = new XmlDocument();
            xd.Load(xmlPath);
            XmlNode xn = xd.SelectSingleNode("//DBOperation");
            if (xn == null) return;
            XmlNode xnWebServers = xn.SelectSingleNode("WebServers");
            string url = xnWebServers.Attributes["url"].Value;
            string methodName = xnWebServers.Attributes["methodName"].Value;
            //string url = "http://www.webservicex.net/globalweather.asmx";//methodName
            //string[] args = new string [ 2 ];
            //args [ 0 ] = "chengdu";
            //args [ 1 ] = "China";
            string[] args = new string[1];
            args[0] = "5101";
            object result = ServiceClass.InvokeWebService(url, methodName, args);
            string sTmp = result.ToString();
            MessageBox.Show(sTmp, "天气信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        #endregion
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/54654/viewspace-608341/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2003-08-09

  • 博文量
    129
  • 访问量
    95683