帮助类代码

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. 论文阅读 | Trojaning Attack on Neural Networks

    对神经网络的木马攻击 Q: 1. 模型蒸馏可以做防御吗? 2. 强化学习可以帮助生成木马触发器吗? 3. 怎么挑选建立强连接的units? 本文提出了一种针对神经元网络的木马攻击.模型不直观,不易被人 ...

  2. 切换windows系统输入法的中英文,可以忽视是哪种打字法

    调用windows的API //用户获取当前输入法句柄 [DllImport("imm32.dll")] public static extern IntPtr ImmGetCon ...

  3. PostgreSQL查看等待锁的SQL和进程

    查看等待锁的查询和进程: The following query may be helpful to see what processes are blocking SQL statements (t ...

  4. 如何获取字符串中某个具体的数值--通过json.load转化成字典形式获取

    r=requests.get('http://httpbin.org/get').text print(r) # print(type(r)) # 如果要获取User-Agent的详细数值,需要做JS ...

  5. Spring 自定义注解,结合AOP,配置简单日志注解 (转)

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  6. Postman之前言

    Postman是一款流行的接口api调试/测试工具.几乎可以发送大多数的HTTP请求. 1.依据开发提供的接口文档,对接口进行测试. 2.如果是自己学习,可以网上找一些免费的接口进行学习,或者抓包 - ...

  7. C++ 数组操作符重载、函数对象分析、赋值操作符

    string类型访问单个字符 #include <iostream> #include <string> #include <sstream> using name ...

  8. golang(3):strings和strconv使用 & 时间和日期类型 & 指针类型 & 流程控制 & 函数

    strings和strconv使用 . strings.HasPrefix(s string, prefix string) bool: // 判断字符串s是否以prefix开头 . . string ...

  9. loj 2392「JOISC 2017 Day 1」烟花棒

    loj 答案显然满足二分性,先二分一个速度\(v\) 然后显然所有没有点火的都会往中间点火的人方向走,并且如果两个人相遇不会马上点火,要等到火快熄灭的时候才点火,所以这两个人之后应该在一起行动.另外有 ...

  10. 对xxl-job进行simpleTrigger并动态创建任务扩展

    业务场景 需求上要求能实现quartz的simpleTrigger任务,同时还需要动态的创建任务而非在控制面板上创建,查阅xxl-job官方文档发现simpelTrigger其暂时还躺在to do l ...