调用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.Serialization;
namespace DCZY.HookPlanIF
{
public class WSHelper
{
private static object _obj = new object();
private static WSHelper _ws = null;
private object _wsClassInstance = null;
private Type _wsClassType = null;
private string _nameSpace = string.Empty;
private string _className = string.Empty;
private string _wsUrl = string.Empty;
private bool _lastConn = true;
#region 事件
public event Action<bool> WsConnectionHandle;
#endregion
#region 单一实例
private WSHelper()
{
_nameSpace = ConfigurationManager.AppSettings["NameSpace"].ToString();//没什么用
_className = ConfigurationManager.AppSettings["ClassName"].ToString();//webservice名称
_wsUrl = ConfigurationManager.AppSettings["WsUrl"].ToString() + "?wsdl";//url
}
/// <summary>
/// 获取当前实例
/// </summary>
public static WSHelper CurrentInstance
{
get
{
if (_ws == null)
{
lock (_obj)
{
if (_ws == null)
{
_ws = new WSHelper();
}
}
}
return _ws;
}
}
#endregion
/// <summary>
/// 获取数据
/// </summary>
/// <param name="methodName"></param>
/// <param name="param"></param>
/// <returns></returns>
public object GetData(string methodName, object[] param)
{
try
{
if (_wsClassInstance == null || _wsClassType == null)
CreateClassInstance();
System.Reflection.MethodInfo method = _wsClassType.GetMethod(methodName);
object 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;
}
}
private void CreateClassInstance()
{
try
{
// 1. 使用 WebClient 下载 WSDL 信息。
WebClient web = new WebClient();
Stream stream = web.OpenRead(_wsUrl);
// 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);
}
}
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;
}
}
~WSHelper()
{
Dispose(false);
}
#endregion
}
}
调用webservice帮助类的更多相关文章
- C# 动态调用 webservice 的类
封装这个类是为之后使用 webservice 不用添加各种引用了. using System; using System.Collections.Generic; using System.Compo ...
- C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- ios 调用webservice整理
资料地址:http://blog.sina.com.cn/s/blog_a30ee5f701016yn3.html 学iphone开发有一段时间了,对于我这个新手来说,学习过程中,遇到不少问题,尤其是 ...
- C# 调用Webservice并传递序列化对象
原文:C# 调用Webservice并传递序列化对象 C#动态调用WebService注意要点 1.动态调用的url后面注意一定要加上?WSDL 例如:string _url = "ht ...
- 【转】C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- 通用的调用WebService的两种方法。(调用别人提供的wsdl)(转)
转载自:http://blog.sina.com.cn/s/blog_65933e020101incz.html1.调用WebService的Client端采用jax-ws调用WebService:流 ...
- 转--Android中调用webservice的工具类
最近学习WebService,感觉利用这个借口开发网站的Android客户端方便及了,用到一个工具类,这里铭记一下. public static final String WebServiceName ...
- 动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法
[导读]ServiceDescriptionImporter是创建Web Service 时使用的类,它是引用继承System.Web.Services 当我将VS2005里写的一段代码放在VS201 ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
随机推荐
- Reactive Extensions 相见恨晚的Rx.Net
何为Reactive Extensions(Rx) Rx是一个遵循函数式编程的类库,它引用观察者以及迭代器设计模式对可观察对象产生的数据进行异步消费.使用Rx, 开发人员将使用LINQ运算符操作异步数 ...
- [SpringBoot guides系列翻译]调度任务
原文 调度任务 用spring实现一个任务调度. 你将做的 你将做一个应用每5秒钟打印当前时间,用@Scheduled注解. 你需要啥 15分钟 文本编辑器或者IDE JDK1.8+ Gradle4+ ...
- WKWebView讲解与使用
随着IOS开发的应用,对于网页嵌入也越来越多了,在IOS 8之前我们使用UIWebView展示详情页,自从IOS 8之后就出现了WKWebView,相比UIWebView,WKWebView优化了较多 ...
- aspx中的checkbox 取值问题
问题:前台checkbox控件,选中值为1,不选值为0: 解决方案: 插入一行 <input type="hidden" name="RemberPwd" ...
- 忘记Linux登录密码怎么办?
1.启动虚拟机,出现下面倒计时界面时,按e键.进入启动前编辑. 2.进入如下界面,再按e键. 3.进入如下页面后,选中第二项kernel开头的项,选中后再按e键. 4.进入如下界面后,在最后面输入空格 ...
- RSA签名和验签Util
目录 1.DigitalSign类 2.CryptException异常类 3.加签示例 1.DigitalSign类 import org.apache.commons.codec.binary.B ...
- 使用curl制作简易百度搜索
这几天研究了一下php中的curl类库,做了一个简单的百度搜索,先上代码 <div style="width:200px;height:100px;"> <div ...
- java的设计模式 - Builder模式
Builder 模式的目的? 构造对象的方式过于复杂,不如将之抽离出来.比如,构造器参数过多 这样说也有点抽象,举个例子吧. 举个例子 比如 非常热门的消息队列RabbitMQ 的 AMQP.Basi ...
- 使用Huginn抓取Discourse论坛
Hi! I don't know why the xpath does not work, but have an easier solution. Discourse also has a JSON ...
- jmeter使用TCP请求时,乱码问题,字符集设置
不墨迹,直接上干货.(提示:UTF-8一个汉字占3个字节) TCP请求默认发的是GBK字符集,要想修改成UTF-8,只需要修改bin目录下的jmeter.properties文件,其中tcp.char ...