服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 动态调用WebService的示例

C# 动态调用WebService的示例

2022-10-14 11:54秋荷雨翔 C#

这篇文章主要介绍了C# 动态调用WebService的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

WebServiceHelper代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
      }
    }
 
  }
}

使用示例:

?
1
2
3
4
5
6
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的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/s0611163/p/10096745.html

延伸 · 阅读

精彩推荐
  • C#解决安装VS2008无法更改默认路径的问题

    解决安装VS2008无法更改默认路径的问题

    这篇文章主要介绍了安装VS2008无法更改默认路径的解决方法,需要的朋友可以参考下。...

    贺臣10712021-11-29
  • C#C#利用Label标签控件模拟窗体标题的移动及窗体颜色不断变换效果

    C#利用Label标签控件模拟窗体标题的移动及窗体颜色不断变换效果

    Label标签控件相信对大家来说都不陌生,下面这篇文章主要给大家介绍了关于C#利用Label标签控件模拟窗体标题的移动及窗体颜色不断变换效果的相关资料,...

    cnc11532022-02-16
  • C#c#定期删除文件的实操方法

    c#定期删除文件的实操方法

    在本篇文章里小编给大家分享了关于c#定期删除文件的方法和步骤,有需要的朋友们可以学习下。...

    C#教程网12042022-07-07
  • C#C#强制转换和尝试转换的方法

    C#强制转换和尝试转换的方法

    这篇文章主要为大家详细介绍了C#强制转换和尝试转换的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    龙宫丿8432022-01-22
  • C#C#实现的三种模拟自动登录和提交POST信息的方法

    C#实现的三种模拟自动登录和提交POST信息的方法

    这篇文章主要介绍了C#实现的三种模拟自动登录和提交POST信息的方法,分别列举了WebBrowser、WebClient及HttpWebRequest实现自动登录及提交POST的相关实现技巧,具有...

    宁静.致远5912021-11-02
  • C#C#编程实现连接SQL SERVER数据库实例详解

    C#编程实现连接SQL SERVER数据库实例详解

    这篇文章主要介绍了C#编程实现连接SQL SERVER数据库的方法,以实例形式较为详细的分析了C#连接SQL SERVER数据库的相关步骤与具体实现技巧,需要的朋友可以参...

    期待秋天的叶9172021-11-05
  • C#C#实现Ruby的负数索引器

    C#实现Ruby的负数索引器

    这篇文章主要介绍了C#实现Ruby的负数索引器的相关代码和使用方法,非常简单实用,需要的朋友可以参考下...

    C#教程网5822021-12-02
  • C#C# 6.0 的知识梳理

    C# 6.0 的知识梳理

    目前最新的版本是C# 7.0,VS的最新版本为Visual Studio 2017 RC,两者都尚未进入正式阶段。C# 6.0虽说出了一段时间,但是似乎有许多人对这一块知识并不了解。...

    反骨仔(二五仔)10482021-12-16