WebService帮助类改良版,支持多webservice
帮助类代码
- using System;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Web.Services.Description;
- using System.Xml;
- using System.Xml.Serialization;
- namespace PTool.WebService
- {
- public class WSCollectionHelper
- {
- private static object _obj = new object();
- private static WSCollectionHelper _ws = null;
- private Dictionary<string,object> _wsClassInstanceList = null;
- private Type _wsClassType = null;
- private List<WSCollectionModel> _paramcollection = null;
- public string xmlurl = string.Empty;
- private bool _lastConn = true;
- #region 事件
- public event Action<bool> WsConnectionHandle;
- #endregion
- #region 单一实例
- private WSCollectionHelper()
- {
- xmlurl = ConfigurationManager.AppSettings["XmlUrl"].ToString();
- if (_paramcollection == null)
- {
- CreateXml();
- }
- }
- private void CreateParaCollectionList(XmlNodeList nodelist)
- {
- if (nodelist != null)
- {
- if (_paramcollection == null)
- {
- _paramcollection = new List<WSCollectionModel>();
- }
- foreach (XmlNode item in nodelist)
- {
- WSCollectionModel model = new WSCollectionModel();
- if (!DBConvert.IsDBNull(item.Attributes["FunctionName"]))
- {
- model.Function_name = DBConvert.ToString(item.Attributes["FunctionName"].InnerText);
- }
- if (!DBConvert.IsDBNull(item.Attributes["ClassName"]))
- {
- model.Class_name = DBConvert.ToString(item.Attributes["ClassName"].InnerText);
- }
- if (!DBConvert.IsDBNull(item.Attributes["NameSpace"]))
- {
- model.Name_space = DBConvert.ToString(item.Attributes["NameSpace"].InnerText);
- }
- if (!DBConvert.IsDBNull(item.Attributes["WsUrl"]))
- {
- model.Ws_url = DBConvert.ToString(item.Attributes["WsUrl"].InnerText);
- }
- _paramcollection.Add(model);
- }
- }
- }
- /// <summary>
- /// 获取当前实例
- /// </summary>
- public static WSCollectionHelper CurrentInstance
- {
- get
- {
- if (_ws == null)
- {
- lock (_obj)
- {
- if (_ws == null)
- {
- _ws = new WSCollectionHelper();
- }
- }
- }
- return _ws;
- }
- }
- #endregion
- /// <summary>
- /// 获取数据
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="param"></param>
- /// <returns></returns>
- public object GetData(string methodName, object[] param)
- {
- try
- {
- object item = null;
- if (_wsClassInstanceList == null)
- {
- _wsClassInstanceList = new Dictionary<string, object>();
- }
- WSCollectionModel selectmodel=_paramcollection.Find(x => x.Function_name == methodName);
- if (selectmodel != null)
- {
- object _wsClassInstance = null;
- if (!_wsClassInstanceList.ContainsKey(selectmodel.Ws_url))
- {
- _wsClassInstance = CreateClassInstance(selectmodel.Ws_url, selectmodel.Class_name);
- _wsClassInstanceList.Add(selectmodel.Ws_url, _wsClassInstance);
- }
- else
- {
- _wsClassInstance = _wsClassInstanceList[selectmodel.Ws_url];
- }
- System.Reflection.MethodInfo method = _wsClassType.GetMethod(methodName);
- item = method.Invoke(_wsClassInstance, param);
- if (_lastConn != true && null != WsConnectionHandle)
- {
- _lastConn = true;
- WsConnectionHandle.BeginInvoke(false, null, null);
- }
- }
- return item;
- }
- catch (Exception e)
- {
- if (_lastConn != false && null != WsConnectionHandle)
- {
- _lastConn = false;
- WsConnectionHandle.BeginInvoke(false, null, null);
- }
- return null;
- }
- }
- public void CreateXml()
- {
- XMLFileHelper xml = new XMLFileHelper(xmlurl);
- XmlNodeList nodelist = xml.GetNodeList("root/wsmodel");
- CreateParaCollectionList(nodelist);
- }
- private object CreateClassInstance(string url,string classname)
- {
- try
- {
- object _wsClassInstance = null;
- // 1. 使用 WebClient 下载 WSDL 信息。
- WebClient web = new WebClient();
- Stream stream = web.OpenRead(url);
- // 2. 创建和格式化 WSDL 文档。
- ServiceDescription description = ServiceDescription.Read(stream);
- // 3. 创建客户端代理代理类。
- ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
- // 指定访问协议。
- importer.ProtocolName = "Soap";
- // 生成客户端代理。
- importer.Style = ServiceDescriptionImportStyle.Client;
- importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
- // 添加 WSDL 文档。
- importer.AddServiceDescription(description, null, null);
- // 4. 使用 CodeDom 编译客户端代理类。
- // 为代理类添加命名空间,缺省为全局空间。
- CodeNamespace nmspace = new CodeNamespace();
- CodeCompileUnit unit = new CodeCompileUnit();
- unit.Namespaces.Add(nmspace);
- ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
- CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
- CompilerParameters parameter = new CompilerParameters();
- parameter.GenerateExecutable = false;
- parameter.GenerateInMemory = true;
- parameter.ReferencedAssemblies.Add("System.dll");
- parameter.ReferencedAssemblies.Add("System.XML.dll");
- parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
- parameter.ReferencedAssemblies.Add("System.Data.dll");
- CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
- if (!result.Errors.HasErrors)
- {
- Assembly asm = result.CompiledAssembly;
- _wsClassType = asm.GetType(classname);
- _wsClassInstance = Activator.CreateInstance(_wsClassType);
- }
- return _wsClassInstance;
- }
- catch (Exception e)
- {
- //_wsClassInstance = null;
- //_wsClassType = null;
- throw e;
- }
- }
- #region IDisposable
- private bool disposed;
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- private void Dispose(bool disposing)
- {
- if (!disposed)
- {
- if (disposing)
- {
- }
- disposed = true;
- }
- }
- ~WSCollectionHelper()
- {
- Dispose(false);
- }
- #endregion
- }
- }
xml实体WSCollectionModel
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PTool.WebService
- {
- public class WSCollectionModel
- {
- private string _function_name = string.Empty;
- private string _name_space = string.Empty;
- private string _class_name = string.Empty;
- private string _ws_url = string.Empty;
- public string Ws_url
- {
- get
- {
- return _ws_url;
- }
- set
- {
- _ws_url = value;
- }
- }
- public string Function_name
- {
- get
- {
- return _function_name;
- }
- set
- {
- _function_name = value;
- }
- }
- public string Name_space
- {
- get
- {
- return _name_space;
- }
- set
- {
- _name_space = value;
- }
- }
- public string Class_name
- {
- get
- {
- return _class_name;
- }
- set
- {
- _class_name = value;
- }
- }
- }
- }
WSConfig.xml
- <?xml version="1.0" encoding="utf-8" ?>
- <root>
- <wsmodel FunctionName="WorkRisk" ClassName="HookPlanService" NameSpace="http://tempuri.org/" WsUrl="http://192.168.2.247:8054/HookPlanService.asmx?wsdl"></wsmodel>
- <wsmodel FunctionName="WhatchSee" ClassName="HookPlanService" NameSpace="http://tempuri.org/" WsUrl="http://192.168.2.247:8054/HookPlanService.asmx?wsdl"></wsmodel>
- <wsmodel FunctionName="UserInfoAccess" ClassName="wsMonitor" NameSpace="http://tempuri.org/" WsUrl="http://10.169.153.40:8082/Access/wsMonitor.asmx?wsdl"></wsmodel>
- </root>
调用方式
- public static OtherRiskWs<WorkSee> GetWorkSeeInfo(string Date, string sta_name = "")
- {
- object[] obj = new object[];
- obj[] = Date;
- obj[] = sta_name;
- object o = WSCollectionHelper.CurrentInstance.GetData("WhatchSee", obj);
- if (o == null)
- {
- return null;
- }
- string strjson = o.ToString();
- OtherRiskWs<WorkSee> info = PTool.Ajax.JsonSerializer.Deserializer(strjson, typeof(OtherRiskWs<WorkSee>)) as OtherRiskWs<WorkSee>;
- if (info == null)
- {
- return null;
- }
- if (info.data == null)
- {
- return null;
- }
- return info;
- }
WebService帮助类改良版,支持多webservice的更多相关文章
- 解析利用wsdl.exe生成webservice代理类的详解
利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部 ...
- 部署基于JDK的webservice服务类
部署服务端 两个注解(@WebService @WebMethod).一个类(Endpoint) 首先新建JAVA工程ws-server 目录结构如下 在工程里新建一个接口,申明一个方法. packa ...
- WebService对跨域的支持
WebService对跨域的支持 跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源, ...
- C# 利用VS自带的WSDL工具生成WebService服务类
C# 利用VS自带的WSDL工具生成WebService服务类 WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲 ...
- C# 动态调用 webservice 的类
封装这个类是为之后使用 webservice 不用添加各种引用了. using System; using System.Collections.Generic; using System.Compo ...
- .net 代理类(WebService代理类的详解 )
http://hi.baidu.com/654085966/item/53ee8c0f108ad78202ce1b1d -----------转自 客户端调用Web Service的方式我现在知道 ...
- 利用wsdl.exe生成webservice代理类
通常要手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:\Proxy_UpdateService.cs http://localhost:1101 ...
- 跨域以及WebService对跨域的支持
无耻收藏该博主的成果啦!https://www.cnblogs.com/yangecnu/p/introduce-cross-domain.html 通过域验证访问WebService:https:/ ...
- 手动生成WebService代理类
方式一: 手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebServic ...
随机推荐
- Metinfo5.1 /include/common.php 变量覆盖+SQL注入漏洞
- 【DSP开发】【图像处理】Gray与YUV之间的转换关系
标准的V4L2 API http://v4l.videotechnology.com/dwg/v4l2.pdf 在例程/home/dvevm_1_20/demos/ImageGray中,涉及到图像采集 ...
- ajax提交表单包含文件
需要用到 FormData. html: <form id="formPost"> name: <input name="name" /&g ...
- [转帖]看完这篇文章,我奶奶都懂了https的原理
看完这篇文章,我奶奶都懂了https的原理 http://www.17coding.info/article/22 非对称算法 以及 CA证书 公钥 核心是 大的质数不一分解 还有 就是 椭圆曲线算法 ...
- python学习笔记四 (运算符重载和命名空间、类)
从以上代码中应该了解到: obj.attribute 查找的顺序: 从对象,类组成的树中,从下到上,从左到右到查找最近到attribute属性值,因为rec中存在name的属性,所以x.name可以 ...
- 小菜鸟之Oracle数据库之事务
Oracle数据库之事务 1. 什么是事务 在数据库中事务是工作的逻辑单元,一个事务是由一个或多个完成一组的相关行为的SQL语句组成,通过事务机制确保这一组SQL语句所作的操作要么都成功执行,完成整个 ...
- 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...
- python_0基础开始_day08
第八节 1,文件操作 文件操作目的: 持久化,永久存储 (数据库之前 -- 文件操作就是代替数据库) 读 1,找到文件位 2,双击打开 3,进行一些操作 4,关闭文件 open() 打开,通过pyth ...
- selenium与页面交互
selenium提供了许多API方法与页面进行交互,如点击.键盘输入.打开关闭网页.输入文字等. 一.webdriver对浏览器提供了很多属性来对浏览器进行操作,常用的如图: get(url).qui ...
- 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时, ...