【超细完整版】C# 获取WebService所有方法并调用 【调用篇】

本文主要是介绍【超细完整版】C# 获取WebService所有方法并调用 【调用篇】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注意:该文章涉及到的调用方法若找不到 请移步第一部分内容查找


C# 生成wsdl和dll教程请移步
【超细完整版】C# WebService 通过URL生成WSDL文件和DLL文件> 【生成篇】


开始

首先实现一个类,用于实现对URL的验证等

public class InputFormatVerification
{/// <summary>/// 是否合法Url地址(统一资源定位)/// </summary>/// <param name="strValue">url地址</param>/// <returns>成功返回true 失败返回false</returns>public static bool IsUrl(string strValue){string RegexStr = string.Empty;RegexStr = @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";return formatChecks(RegexStr, strValue);}/// <summary>/// 检测串值是否为合法的格式/// </summary>/// <param name="strRegex">正则表达式</param>/// <param name="strValue">要检测的String值</param>/// <returns>成功返回true 失败返回false</returns>public static bool formatChecks(string strRegex, string strValue){if (string.IsNullOrWhiteSpace(strValue)){return false;}Regex re = new Regex(strRegex);return re.IsMatch(strValue);}
}

实现Invoke

获取WebService中的所有web 方法
#region 获取web方法
/// <summary>
/// 获取WebService接口的所有WebMethod方法
/// 通过WebService方法的特性为【System.Web.Services.Protocols.SoapDocumentMethodAttribute】
/// 根据特性SoapDocumentMethodAttribute来筛选出所有WebMethod方法
/// </summary>
/// <param name="url"></param>
public static List<MethodInfo> GetAllWebMethodsFromLink(string url, out string className)
{className = GetClassNameFromUrl(url);CompilerResults result = UrlToDllFile(url);Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);return GetWebMethods(type);
}
/// <summary>
/// 获取WebService接口的所有WebMethod方法
/// 通过WebService方法的特性为【System.Web.Services.Protocols.SoapDocumentMethodAttribute】
/// 根据特性SoapDocumentMethodAttribute来筛选出所有WebMethod方法
/// </summary>
/// <param name="wsdlFilePath"></param>
public static List<MethodInfo> GetAllWebMethodsFromWsdl(string wsdlFilePath, out string className)
{className = GetClassNameFromWsdl(wsdlFilePath);CompilerResults result = WsdlToDll(wsdlFilePath);Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);return GetWebMethods(type);
}private static List<MethodInfo> GetWebMethods(Type type)
{List<MethodInfo> methodInfoList = new List<MethodInfo>();if (type == null){return methodInfoList;}MethodInfo[] methodInfos = type.GetMethods();for (int i = 0; i < methodInfos.Length; i++){MethodInfo methodInfo = methodInfos[i];//WebMethod方法的特性为:System.Web.Services.Protocols.SoapDocumentMethodAttribute Attribute attribute = methodInfo.GetCustomAttribute(typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute));if (methodInfo.MemberType == MemberTypes.Method && attribute != null){methodInfoList.Add(methodInfo);}}return methodInfoList;
}
#endregion
通过wsdl或url进行调用
/// <summary>
/// 调用WebService
/// </summary>
/// <param name="address">WebService地址</param>
/// <param name="methodName">方法名称</param>
/// <param name="args">参数列表</param>
/// <param name="timeOut"></param>
/// <returns>返回调用结果</returns>
/// <exception cref="Exception"></exception>
private static object InvokeWebService(string address, string methodName, object[] args, string timeOut = "")
{try{string className = string.Empty;CompilerResults result = null;//支持直接URL或wsdl类型文件的调用if (InputFormatVerification.IsUrl(address)){className = GetClassNameFromUrl(address);result = UrlToDllFile(address);}else{className = GetClassNameFromWsdl(address);result = WsdlToDll(address);}Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);FieldInfo[] arry = type.GetFields();//实例类型对象   object obj = Activator.CreateInstance(type);System.Reflection.MethodInfo mi = type.GetMethod(methodName);//添加超时时间if (!string.IsNullOrEmpty(timeOut)){int timeout = 0;int.TryParse(timeOut, out timeout);if (timeout == 0) timeout = 1200;//设置超时时间((System.Web.Services.Protocols.WebClientProtocol)(obj)).Timeout = timeout * 1000;//毫秒s,timeOut超时时间设置为分钟}var res = mi.Invoke(obj, args);return res;}catch (Exception ex){throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));}
}

老规矩!😄

这篇关于【超细完整版】C# 获取WebService所有方法并调用 【调用篇】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826038

相关文章

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文解析C#中的StringSplitOptions枚举

《一文解析C#中的StringSplitOptions枚举》StringSplitOptions是C#中的一个枚举类型,用于控制string.Split()方法分割字符串时的行为,核心作用是处理分割后... 目录C#的StringSplitOptions枚举1.StringSplitOptions枚举的常用

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

SpringBoot中ResponseEntity的使用方法举例详解

《SpringBoot中ResponseEntity的使用方法举例详解》ResponseEntity是Spring的一个用于表示HTTP响应的全功能对象,它可以包含响应的状态码、头信息及响应体内容,下... 目录一、ResponseEntity概述基本特点:二、ResponseEntity的基本用法1. 创