帮助类代码

  1. using System;
  2. using System.CodeDom;
  3. using System.CodeDom.Compiler;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Web.Services.Description;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14.  
  15. namespace PTool.WebService
  16. {
  17. public class WSCollectionHelper
  18. {
  19. private static object _obj = new object();
  20. private static WSCollectionHelper _ws = null;
  21. private Dictionary<string,object> _wsClassInstanceList = null;
  22. private Type _wsClassType = null;
  23. private List<WSCollectionModel> _paramcollection = null;
  24. public string xmlurl = string.Empty;
  25.  
  26. private bool _lastConn = true;
  27. #region 事件
  28. public event Action<bool> WsConnectionHandle;
  29. #endregion
  30.  
  31. #region 单一实例
  32. private WSCollectionHelper()
  33. {
  34. xmlurl = ConfigurationManager.AppSettings["XmlUrl"].ToString();
  35. if (_paramcollection == null)
  36. {
  37. CreateXml();
  38. }
  39. }
  40.  
  41. private void CreateParaCollectionList(XmlNodeList nodelist)
  42. {
  43. if (nodelist != null)
  44. {
  45. if (_paramcollection == null)
  46. {
  47. _paramcollection = new List<WSCollectionModel>();
  48. }
  49. foreach (XmlNode item in nodelist)
  50. {
  51. WSCollectionModel model = new WSCollectionModel();
  52. if (!DBConvert.IsDBNull(item.Attributes["FunctionName"]))
  53. {
  54. model.Function_name = DBConvert.ToString(item.Attributes["FunctionName"].InnerText);
  55. }
  56. if (!DBConvert.IsDBNull(item.Attributes["ClassName"]))
  57. {
  58. model.Class_name = DBConvert.ToString(item.Attributes["ClassName"].InnerText);
  59. }
  60. if (!DBConvert.IsDBNull(item.Attributes["NameSpace"]))
  61. {
  62. model.Name_space = DBConvert.ToString(item.Attributes["NameSpace"].InnerText);
  63. }
  64. if (!DBConvert.IsDBNull(item.Attributes["WsUrl"]))
  65. {
  66. model.Ws_url = DBConvert.ToString(item.Attributes["WsUrl"].InnerText);
  67. }
  68. _paramcollection.Add(model);
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. /// <summary>
  76. /// 获取当前实例
  77. /// </summary>
  78. public static WSCollectionHelper CurrentInstance
  79. {
  80. get
  81. {
  82. if (_ws == null)
  83. {
  84. lock (_obj)
  85. {
  86. if (_ws == null)
  87. {
  88. _ws = new WSCollectionHelper();
  89. }
  90. }
  91. }
  92. return _ws;
  93. }
  94. }
  95. #endregion
  96.  
  97. /// <summary>
  98. /// 获取数据
  99. /// </summary>
  100. /// <param name="methodName"></param>
  101. /// <param name="param"></param>
  102. /// <returns></returns>
  103. public object GetData(string methodName, object[] param)
  104. {
  105. try
  106. {
  107. object item = null;
  108. if (_wsClassInstanceList == null)
  109. {
  110. _wsClassInstanceList = new Dictionary<string, object>();
  111. }
  112. WSCollectionModel selectmodel=_paramcollection.Find(x => x.Function_name == methodName);
  113. if (selectmodel != null)
  114. {
  115. object _wsClassInstance = null;
  116. if (!_wsClassInstanceList.ContainsKey(selectmodel.Ws_url))
  117. {
  118. _wsClassInstance = CreateClassInstance(selectmodel.Ws_url, selectmodel.Class_name);
  119. _wsClassInstanceList.Add(selectmodel.Ws_url, _wsClassInstance);
  120. }
  121. else
  122. {
  123. _wsClassInstance = _wsClassInstanceList[selectmodel.Ws_url];
  124. }
  125. System.Reflection.MethodInfo method = _wsClassType.GetMethod(methodName);
  126. item = method.Invoke(_wsClassInstance, param);
  127. if (_lastConn != true && null != WsConnectionHandle)
  128. {
  129. _lastConn = true;
  130. WsConnectionHandle.BeginInvoke(false, null, null);
  131. }
  132. }
  133. return item;
  134. }
  135. catch (Exception e)
  136. {
  137. if (_lastConn != false && null != WsConnectionHandle)
  138. {
  139. _lastConn = false;
  140. WsConnectionHandle.BeginInvoke(false, null, null);
  141. }
  142.  
  143. return null;
  144. }
  145. }
  146.  
  147. public void CreateXml()
  148. {
  149. XMLFileHelper xml = new XMLFileHelper(xmlurl);
  150. XmlNodeList nodelist = xml.GetNodeList("root/wsmodel");
  151. CreateParaCollectionList(nodelist);
  152. }
  153.  
  154. private object CreateClassInstance(string url,string classname)
  155. {
  156. try
  157. {
  158. object _wsClassInstance = null;
  159. // 1. 使用 WebClient 下载 WSDL 信息。
  160. WebClient web = new WebClient();
  161.  
  162. Stream stream = web.OpenRead(url);
  163. // 2. 创建和格式化 WSDL 文档。
  164. ServiceDescription description = ServiceDescription.Read(stream);
  165. // 3. 创建客户端代理代理类。
  166. ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
  167. // 指定访问协议。
  168. importer.ProtocolName = "Soap";
  169.  
  170. // 生成客户端代理。
  171. importer.Style = ServiceDescriptionImportStyle.Client;
  172. importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
  173. // 添加 WSDL 文档。
  174. importer.AddServiceDescription(description, null, null);
  175. // 4. 使用 CodeDom 编译客户端代理类。
  176. // 为代理类添加命名空间,缺省为全局空间。
  177. CodeNamespace nmspace = new CodeNamespace();
  178. CodeCompileUnit unit = new CodeCompileUnit();
  179. unit.Namespaces.Add(nmspace);
  180.  
  181. ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
  182. CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
  183.  
  184. CompilerParameters parameter = new CompilerParameters();
  185. parameter.GenerateExecutable = false;
  186. parameter.GenerateInMemory = true;
  187. parameter.ReferencedAssemblies.Add("System.dll");
  188. parameter.ReferencedAssemblies.Add("System.XML.dll");
  189. parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
  190. parameter.ReferencedAssemblies.Add("System.Data.dll");
  191.  
  192. CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
  193.  
  194. if (!result.Errors.HasErrors)
  195. {
  196. Assembly asm = result.CompiledAssembly;
  197. _wsClassType = asm.GetType(classname);
  198. _wsClassInstance = Activator.CreateInstance(_wsClassType);
  199. }
  200. return _wsClassInstance;
  201. }
  202. catch (Exception e)
  203. {
  204. //_wsClassInstance = null;
  205. //_wsClassType = null;
  206. throw e;
  207. }
  208.  
  209. }
  210.  
  211. #region IDisposable
  212. private bool disposed;
  213. public void Dispose()
  214. {
  215. Dispose(true);
  216. GC.SuppressFinalize(this);
  217. }
  218. private void Dispose(bool disposing)
  219. {
  220. if (!disposed)
  221. {
  222. if (disposing)
  223. {
  224. }
  225. disposed = true;
  226. }
  227. }
  228. ~WSCollectionHelper()
  229. {
  230. Dispose(false);
  231. }
  232. #endregion
  233. }
  234. }

xml实体WSCollectionModel

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace PTool.WebService
  7. {
  8. public class WSCollectionModel
  9. {
  10. private string _function_name = string.Empty;
  11. private string _name_space = string.Empty;
  12. private string _class_name = string.Empty;
  13. private string _ws_url = string.Empty;
  14.  
  15. public string Ws_url
  16. {
  17. get
  18. {
  19. return _ws_url;
  20. }
  21.  
  22. set
  23. {
  24. _ws_url = value;
  25. }
  26. }
  27.  
  28. public string Function_name
  29. {
  30. get
  31. {
  32. return _function_name;
  33. }
  34.  
  35. set
  36. {
  37. _function_name = value;
  38. }
  39. }
  40.  
  41. public string Name_space
  42. {
  43. get
  44. {
  45. return _name_space;
  46. }
  47.  
  48. set
  49. {
  50. _name_space = value;
  51. }
  52. }
  53.  
  54. public string Class_name
  55. {
  56. get
  57. {
  58. return _class_name;
  59. }
  60.  
  61. set
  62. {
  63. _class_name = value;
  64. }
  65. }
  66. }
  67. }

WSConfig.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <root>
  3. <wsmodel FunctionName="WorkRisk" ClassName="HookPlanService" NameSpace="http://tempuri.org/" WsUrl="http://192.168.2.247:8054/HookPlanService.asmx?wsdl"></wsmodel>
  4. <wsmodel FunctionName="WhatchSee" ClassName="HookPlanService" NameSpace="http://tempuri.org/" WsUrl="http://192.168.2.247:8054/HookPlanService.asmx?wsdl"></wsmodel>
  5. <wsmodel FunctionName="UserInfoAccess" ClassName="wsMonitor" NameSpace="http://tempuri.org/" WsUrl="http://10.169.153.40:8082/Access/wsMonitor.asmx?wsdl"></wsmodel>
  6. </root>

调用方式

  1. public static OtherRiskWs<WorkSee> GetWorkSeeInfo(string Date, string sta_name = "")
  2. {
  3. object[] obj = new object[];
  4. obj[] = Date;
  5. obj[] = sta_name;
  6. object o = WSCollectionHelper.CurrentInstance.GetData("WhatchSee", obj);
  7. if (o == null)
  8. {
  9. return null;
  10. }
  11. string strjson = o.ToString();
  12. OtherRiskWs<WorkSee> info = PTool.Ajax.JsonSerializer.Deserializer(strjson, typeof(OtherRiskWs<WorkSee>)) as OtherRiskWs<WorkSee>;
  13. if (info == null)
  14. {
  15. return null;
  16. }
  17. if (info.data == null)
  18. {
  19. return null;
  20. }
  21. return info;
  22. }

WebService帮助类改良版,支持多webservice的更多相关文章

  1. 解析利用wsdl.exe生成webservice代理类的详解

    利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部 ...

  2. 部署基于JDK的webservice服务类

    部署服务端 两个注解(@WebService @WebMethod).一个类(Endpoint) 首先新建JAVA工程ws-server 目录结构如下 在工程里新建一个接口,申明一个方法. packa ...

  3. WebService对跨域的支持

    WebService对跨域的支持 跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源, ...

  4. C# 利用VS自带的WSDL工具生成WebService服务类

    C# 利用VS自带的WSDL工具生成WebService服务类   WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲 ...

  5. C# 动态调用 webservice 的类

    封装这个类是为之后使用 webservice 不用添加各种引用了. using System; using System.Collections.Generic; using System.Compo ...

  6. .net 代理类(WebService代理类的详解 )

    http://hi.baidu.com/654085966/item/53ee8c0f108ad78202ce1b1d   -----------转自 客户端调用Web Service的方式我现在知道 ...

  7. 利用wsdl.exe生成webservice代理类

    通常要手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:\Proxy_UpdateService.cs  http://localhost:1101 ...

  8. 跨域以及WebService对跨域的支持

    无耻收藏该博主的成果啦!https://www.cnblogs.com/yangecnu/p/introduce-cross-domain.html 通过域验证访问WebService:https:/ ...

  9. 手动生成WebService代理类

    方式一: 手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebServic ...

随机推荐

  1. Metinfo5.1 /include/common.php 变量覆盖+SQL注入漏洞

  2. 【DSP开发】【图像处理】Gray与YUV之间的转换关系

    标准的V4L2 API http://v4l.videotechnology.com/dwg/v4l2.pdf 在例程/home/dvevm_1_20/demos/ImageGray中,涉及到图像采集 ...

  3. ajax提交表单包含文件

    需要用到  FormData. html: <form id="formPost"> name: <input name="name" /&g ...

  4. [转帖]看完这篇文章,我奶奶都懂了https的原理

    看完这篇文章,我奶奶都懂了https的原理 http://www.17coding.info/article/22 非对称算法 以及 CA证书 公钥 核心是 大的质数不一分解 还有 就是 椭圆曲线算法 ...

  5. python学习笔记四 (运算符重载和命名空间、类)

    从以上代码中应该了解到: obj.attribute  查找的顺序: 从对象,类组成的树中,从下到上,从左到右到查找最近到attribute属性值,因为rec中存在name的属性,所以x.name可以 ...

  6. 小菜鸟之Oracle数据库之事务

    Oracle数据库之事务 1. 什么是事务 在数据库中事务是工作的逻辑单元,一个事务是由一个或多个完成一组的相关行为的SQL语句组成,通过事务机制确保这一组SQL语句所作的操作要么都成功执行,完成整个 ...

  7. 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...

  8. python_0基础开始_day08

    第八节 1,文件操作 文件操作目的: 持久化,永久存储 (数据库之前 -- 文件操作就是代替数据库) 读 1,找到文件位 2,双击打开 3,进行一些操作 4,关闭文件 open() 打开,通过pyth ...

  9. selenium与页面交互

    selenium提供了许多API方法与页面进行交互,如点击.键盘输入.打开关闭网页.输入文字等. 一.webdriver对浏览器提供了很多属性来对浏览器进行操作,常用的如图: get(url).qui ...

  10. The Digits String

    https://ac.nowcoder.com/acm/contest/338/L 题解: 当n==1时,0-9填上的话,对4取余,分别是余数为0的3个,1的3个,2的2个,3的2个: 当n==2时, ...