zoukankan      html  css  js  c++  java
  • C#动态调用WebService

              

    /// <summary>
    /// 动态调用web服务
    /// </summary>
    /// <param name="url">url</param>
    /// <param name="methodname">方法</param>
    /// <param name="args">参数</param>
    /// <returns>object基类对象</returns>
    public static object InvokeWebService(string url, string methodname, object[] args)
    {
    object result = null;
    if (string.IsNullOrWhiteSpace(url))
    return result;
    try
    {
    System.Net.WebClient wc = new System.Net.WebClient();
    System.IO.Stream stream = wc.OpenRead(url);

    System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
    string className = sd.Services[0].Name;
    System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
    sdi.AddServiceDescription(sd, "", "");
    System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace("");
    System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
    ccu.Namespaces.Add(cn);
    sdi.Import(cn, ccu);

    System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

    System.CodeDom.Compiler.CodeDomProvider provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
    System.CodeDom.Compiler.CompilerResults cr = provider.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(className, true, true);

    object obj = Activator.CreateInstance(t);
    System.Reflection.MethodInfo mi = t.GetMethod(methodname);
    result = mi.Invoke(obj, args);
    }
    catch (Exception ex)
    {
    result = null;
    System.Windows.Forms.MessageBox.Show(ex.Message + " " + ex.StackTrace);
    }
    return result;
    }

  • 相关阅读:
    软件技术发展的几个阶段
    MOOONscheduler核心设计图(初稿)
    Write Read Writeln Readln console
    Win32Check对Windows操作 注销 重新启动 关闭计算机_Win32Check
    WM_nclButtonDblClk响应标题栏事件_message
    使用 “+”号实现多个字符串的连接
    TRichEdit_控制TRichEdit组件滚动
    取得字符串中指定的字符str[]
    undo RichEdit1
    使Memo 原有的右键功能失效 _OnContextPopup
  • 原文地址:https://www.cnblogs.com/txsblog/p/14137007.html
Copyright © 2011-2022 走看看