【超细完整版】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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方