帮助类代码

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的更多相关文章

  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. 【Linux开发】linux设备驱动归纳总结(四):3.抢占和上下文切换

    linux设备驱动归纳总结(四):3.抢占和上下文切换 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  2. channel 介绍

    !!!1.Memory Channel 内存通道 事件将被存储在内存中的具有指定大小的队列中. 非常适合那些需要高吞吐量但是失败是会丢失数据的场景下.   属性说明: !type – 类型,必须是“m ...

  3. filter方法常用过滤条件

    #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_ from ...

  4. 学习shell的第三天

    编程原理:1.编程介绍 早期编程:  驱动 硬件默认是不能使用的:   不同的厂家硬件设备之间需要进行指令沟通,我们需要驱动程序来进行“翻译”:  更趋近与硬件开发的工程师,要学习“汇编语言”:而“汇 ...

  5. Mongo数据库备份

    安全访问状态下 手动在线备份: mongodump -h 127.0.0.1:27017 -u=username -p=123456 -d dbname -o /home/backups 手动恢复: ...

  6. liunx 安装rsync

    新建一个rsync.s文件,把下面的代码写入文件里: #!/usr/bin/env bash mkdir -p /data/app/rsync/etc/ mkdir -p /data/logs/rsy ...

  7. Centos7 更换为网易YUM源

    当我们刚刚安装系统的时候 yum 的速度那是真滴慢所以我们就需要一个更加快速的镜像,这时候网易镜像带给我们便捷.下面来一起更换吧! 备份当前的 yum 源 # yum 源在目录 /etc/yum.re ...

  8. Python 入门 之 异常处理

    Python 入门 之 异常处理 1.异常处理 (1)程序中的错误分为两种 <1> 语法错误 (这种错误,根本过不了Python解释器的语法检测,必须在程序执行前就改正) # 语法错误示范 ...

  9. ABCD 谁是小偷

    题目: 警察局抓了a,b,c,d 4名偷窃嫌疑犯,其中只有一人是小偷.审问中,a说:“我不是小偷.”b说:“c是小偷.”c说:“小偷肯定是d.”d说:“c在胡说.” 现在已经知道这四人中有三人说的是真 ...

  10. webpack的基本使用

    安装webpack npm i webpack -g npm i webpack-cli -g 1.基础用法(无需配置webpack.config.js文件) 1.2 新建需要打包的测试文件input ...