• 在C#中动态调用webService


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 using System.Web;
     7 using System.Net;
     8 using System.IO;
     9 using System.Web.Services.Description;
    10 using System.CodeDom;
    11 using Microsoft.CSharp;
    12 using System.CodeDom.Compiler;
    13 using System.Reflection;
    14 
    15 namespace GEB7.Core.Util
    16 {
    17     public class WSHelper
    18     {
    19 
    20         ///    /// < summary>          
    21         /// 动态调用web服务 
    22         /// < /summary>          
    23         /// < param name="url">WSDL服务地址< /param>
    24         /// < param name="classname">类名< /param>  
    25         /// < param name="methodname">方法名< /param>  
    26         /// < param name="args">参数< /param> 
    27         /// < returns>< /returns>
    28         public static object InvokeWebService(string url, string classname, string methodname, object[] args)
    29         {
    30             string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
    31             if ((classname == null) || (classname == ""))
    32             {
    33                 classname = WSHelper.GetWsClassName(url);
    34             }
    35             try
    36             {                   //获取WSDL   
    37                 WebClient wc = new WebClient();
    38                 Stream stream = wc.OpenRead(url + "?WSDL");
    39                 ServiceDescription sd = ServiceDescription.Read(stream);
    40                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
    41                 sdi.AddServiceDescription(sd, "", "");
    42                 CodeNamespace cn = new CodeNamespace(@namespace);
    43                 //生成客户端代理类代码          
    44                 CodeCompileUnit ccu = new CodeCompileUnit();
    45                 ccu.Namespaces.Add(cn);
    46                 sdi.Import(cn, ccu);
    47                 CSharpCodeProvider icc = new CSharpCodeProvider();
    48                 //设定编译参数                 
    49                 CompilerParameters cplist = new CompilerParameters();
    50                 cplist.GenerateExecutable = false;
    51                 cplist.GenerateInMemory = true;
    52                 cplist.ReferencedAssemblies.Add("System.dll");
    53                 cplist.ReferencedAssemblies.Add("System.XML.dll");
    54                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
    55                 cplist.ReferencedAssemblies.Add("System.Data.dll");
    56                 //编译代理类                 
    57                 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
    58                 if (true == cr.Errors.HasErrors)
    59                 {
    60                     System.Text.StringBuilder sb = new System.Text.StringBuilder();
    61                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
    62                     {
    63                         sb.Append(ce.ToString());
    64                         sb.Append(System.Environment.NewLine);
    65                     }
    66                     throw new Exception(sb.ToString());
    67                 }
    68                 //生成代理实例,并调用方法   
    69                 System.Reflection.Assembly assembly = cr.CompiledAssembly;
    70                 Type t = assembly.GetType(@namespace + "." + classname, true, true);
    71                 object obj = Activator.CreateInstance(t);
    72                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
    73                 return mi.Invoke(obj, args);
    74             }
    75             catch (Exception ex)
    76             {
    77                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
    78             }
    79         }
    80         private static string GetWsClassName(string wsUrl)
    81         {
    82             string[] parts = wsUrl.Split('/');
    83             string[] pps = parts[parts.Length - 1].Split('.');
    84             return pps[0];
    85         }
    86     }
    87 }
  • 相关阅读:
    黑客是如何知道我们常用的密码的
    一个核物理学霸为何两次收到BlackHat的邀请
    透过大数据剖析漫画何去何从
    SJF(最短作业优先)
    RR(轮转调度算法)
    hrrf(最高响应比)
    fcfs
    Process 2(完成版)
    进程2
    进程1
  • 原文地址:https://www.cnblogs.com/haofaner/p/8779863.html
Copyright © 2020-2023  润新知