zoukankan      html  css  js  c++  java
  • WebService远程调用(代码调用)

    在做多个系统集成的时候,由于各系统厂商采用不同的架构,在项目实施前期,各业务对业务理解不够深入,系统接口可能会有较多变化,

    在此背景下,动态调用webserivce就变得灵活了,降低了系统集成的耦合度。

    下面介绍动态调用的具体步骤:

    具体步骤:

    1. 从目标 URL 下载 WSDL 数据。
    2. 使用 ServiceDescription 创建和格式化 WSDL 文档文件。
    3. 使用 ServiceDescriptionImporter 创建客户端代理类。
    4. 使用 CodeDom 动态创建客户端代理类程序集。
    5. 利用反射调用相关 WebService 方法。

    其实与手工创建添加引用步骤一样,只是在这里把手动变成了自动而已,动态生成代理类,利用反射动态调用了方法。

    下面看代码:代码也是摘自博友的,只是作了一些小的修改,

    /// < summary>
            /// 动态调用web服务
            /// < /summary>
            /// < param name="url">WSDL服务地址< /param>
            /// < param name="classname">类名< /param>
            /// < param name="methodname">方法名< /param> 
            /// < param name="args">参数< /param> 
            /// < returns>< /returns> 
            public object InvokeWebService(string url, string classname, string methodname, object[] args)
            {
                string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
                if ((classname == null) || (classname == ""))
                {
                    classname = CommonServiceHelper.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 icc = new CSharpCodeProvider();
                    //设定编译参数
                    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 obj = Activator.CreateInstance(t);
                    System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                    return mi.Invoke(obj, args);
                    /*        
                     * PropertyInfo propertyInfo = type.GetProperty(propertyname);     
                     * return propertyInfo.GetValue(obj, null);      
                     * */
                }
                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];
            }
    /// <summary>
            ///InvokeWebService 的测试
            ///</summary>
            [TestMethod()]
            public void InvokeWebServiceTest()
            {
                CommonServiceHelper target = new CommonServiceHelper(); // TODO: 初始化为适当的值
                string url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"; // TODO: 初始化为适当的值
                string classname = string.Empty; // TODO: 初始化为适当的值
                string methodname = "getWeather"; // TODO: 初始化为适当的值
                string[] a = new string[2] { "bj","" };
                object[] args = a; // TODO: 初始化为适当的值
                object expected = null; // TODO: 初始化为适当的值
                object actual;
                actual = target.InvokeWebService(url, classname, methodname, args);
                Assert.AreEqual(expected, actual);
                Assert.Inconclusive("验证此测试方法的正确性。");
            }

    转自http://www.cnblogs.com/langhua/p/3344784.html

  • 相关阅读:
    GDB命令行最基本操作
    mysql待整理
    python生成二维数组
    python2.7执行shell命令
    使用matplot做图--sin图像
    python--Numpy简单实用实例
    python多线程的使用
    pyv8使用总结
    QDialog:输入对话框、颜色对话框、字体对话框、文件对话框
    pyqt重写键盘事件+获取信号发送对象
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3481477.html
Copyright © 2011-2022 走看看