首页 > 编程语言 > C# 动态调用WebService的示例
2020
11-06

C# 动态调用WebService的示例

WebServiceHelper代码:

using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web.Services.Description;
using System.Xml.Serialization;

namespace SunCreate.HaiKang8600WebService.Utils
{
  /// <summary>
  /// 动态调用WebService
  /// </summary>
  public static class WebServiceHelper
  {
    /// <summary>
    /// 动态调用WebService
    /// </summary>
    /// <param name="url">WebService地址</param>
    /// <param name="strNamespace">命名空间</param>
    /// <param name="className">类名</param>
    /// <param name="methodName">方法名(模块名)</param>
    /// <param name="args">参数列表</param>
    public static object InvokeWebService(string url, string strNamespace, string className, string methodName, object[] args)
    {
      try
      {
        WebClient webClient = new WebClient();
        Stream stream = webClient.OpenRead(url); //获取服务描述语言(WSDL)

        ServiceDescription serviceDescription = ServiceDescription.Read(stream); //通过直接从 Stream实例加载 XML 来初始化ServiceDescription类的实例。
        ServiceDescriptionImporter serviceDescriptionImporter = new ServiceDescriptionImporter();
        serviceDescriptionImporter.AddServiceDescription(serviceDescription, null, null);

        //生成客户端代理类代码
        CodeNamespace codeNamespace = new CodeNamespace(strNamespace); //CodeNamespace表示命名空间声明。
        CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
        codeCompileUnit.Namespaces.Add(codeNamespace);
        serviceDescriptionImporter.Import(codeNamespace, codeCompileUnit);

        CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider();
        ICodeCompiler iCodeCompiler = csharpCodeProvider.CreateCompiler();//取得C#程式码编译器的执行个体

        //设定编译器的参数
        CompilerParameters compilerParameters = new CompilerParameters();//创建编译器的参数实例
        compilerParameters.GenerateExecutable = false;
        compilerParameters.GenerateInMemory = true;
        compilerParameters.ReferencedAssemblies.Add("System.dll");
        compilerParameters.ReferencedAssemblies.Add("System.XML.dll");
        compilerParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
        compilerParameters.ReferencedAssemblies.Add("System.Data.dll");

        //编译代理类
        CompilerResults compilerResults = iCodeCompiler.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);
        if (true == compilerResults.Errors.HasErrors)
        {
          StringBuilder sb = new StringBuilder();
          foreach (CompilerError ce in compilerResults.Errors)
          {
            sb.Append(ce.ToString());
            sb.Append(System.Environment.NewLine);
          }
          throw new Exception(sb.ToString());
        }

        //生成代理实例,并调用方法
        System.Reflection.Assembly assembly = compilerResults.CompiledAssembly;
        Type type = assembly.GetType(strNamespace + "." + className, true, true);
        object obj = Activator.CreateInstance(type);
        System.Reflection.MethodInfo methodInfo = type.GetMethod(methodName); //MethodInfo 的实例可以通过调用GetMethods或者Type对象或派生自Type的对象的GetMethod方法来获取,还可以通过调用表示泛型方法定义的 MethodInfo 的MakeGenericMethod方法来获取。
        return methodInfo.Invoke(obj, args);
      }
      catch (Exception ex)
      {
        LogUtil.LogError(ex, "动态调用WebService 错误");
        return null;
      }
    }

  }
}

使用示例:

string url = "http://172.16.36.26:8080/attachment/services/AttachmentService?wsdl";
object[] args = new object[2];
args[0] = "1";
args[1] = "1";
object str = WebServiceHelper.InvokeWebService(url, "service.webservice", "AttachmentService", "checkGrade", args);
string sstr = str.ToString();

以上就是C# 动态调用WebService的示例的详细内容,更多关于C# 动态调用WebService的资料请关注自学编程网其它相关文章!

编程技巧