C# 动态调用WebService 3
using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web.Services.Description; namespace BOBO
{
public class MyWSDL
{
public object InvokeWebService3(string url, string methodname, int WSDLCacheTime, params object[] args)
{
return InvokeWebService(url, methodname, WSDLCacheTime, args);
} public object InvokeWebService2(string url, string methodname, params object[] args)
{
return InvokeWebService(url, methodname, , args);
} /// <summary>
/// 实例化WebServices
/// </summary>
/// <param name="url">WebServices地址</param>
/// <param name="methodname">调用的方法</param>
/// <param name="args">把webservices里需要的参数按顺序放到这个object[]里</param>
public object InvokeWebService(string url, string methodname, int WSDLCacheTime, object[] args)
{
try
{
object obj = null;
MethodInfo mi = GetMethodInfo(url, methodname, ref obj, WSDLCacheTime);
return mi.Invoke(obj, args);
}
catch (Exception ex)
{
ex.ToString();
return null;
}
} MyString mstr = new MyString();
CacheManage cm = new CacheManage(); public MethodInfo GetMethodInfo(string url, string methodname, ref object obj, int WSDLCacheTime = * )
{
string fname = MethodBase.GetCurrentMethod().Name;
string key = mstr.MD5Encrypt(fname + url + methodname);
MethodInfo mi = (MethodInfo)cm.GetFromCache(key);
obj = cm.GetFromCache(key + "obj");
if (mi == null)
{
string @namespace = "client";
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
sd.Bindings.Add(CreateBinding("MaxBufferSize", ""));
sd.Bindings.Add(CreateBinding("maxReceivedMessageSize", ""));
string classname = sd.Services[].Name;
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
//ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;//动态编译后的程序集不生成可执行文件
cplist.GenerateInMemory = true;//动态编译后的程序集只存在于内存中,不在硬盘的文件上
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类
CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(Environment.NewLine);
} throw new Exception(sb.ToString());
} //生成代理实例,并调用方法
Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true); obj = Activator.CreateInstance(t);
mi = t.GetMethod(methodname);
if (WSDLCacheTime < )
{
WSDLCacheTime = ;
}
cm.SetCache(key, mi, WSDLCacheTime);
cm.SetCache(key + "obj", obj, WSDLCacheTime + );
}
return mi;
} private Binding CreateBinding(string name, string doc)
{
Binding bd = new Binding();
bd.Name = name;
bd.Documentation = doc;
return bd;
}
}
}
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferSize="" maxReceivedMessageSize="">
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
C# 动态调用WebService 3的更多相关文章
- Atitit 动态调用webservice与客户端代理方式调用
Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke 直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...
- 动态调用WebService(C#) (非常实用)
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- 动态调用webservice(部分转载)
动态调用webservice,做个笔记: public class WSHelper { /// < summary> /// 动态调用web服务 /// < /summary> ...
- C# 动态调用webservice
最近项目中,用到动态调用webservice的内容,此处记录下来,留着以后COPY(我们只需要在XML,config文件,或者数据库中配置webservice连接地址和方法名即可使用): using ...
- 动态调用webservice及WCF服务
动态调用web服务,该方法只针对Web service, WCF的服务不行,如果是WCF的就通过工具直接生产代理类,把代理类配置到调用的项目中,通过配置客户端的终结点动态的取实现: 通过Svcutil ...
- C# .NET 动态调用webservice的三种方式
转载自 百度文库 http://wenku.baidu.com/link?url=Q2q50wohf5W6UX44zqotXFEe_XOMaib4UtI3BigaNwipOHKNETloMF4ax4W ...
- WebService – 2.动态调用WebService
在本节课程中,将演示如何通过程序动态添加.调用.编译.执行WebService并返回结果. WebService动态调用示意图 WebService相关知识 代码文档对象模型CodeDom的使用 编程 ...
- 用C#通过反射实现动态调用WebService 告别Web引用
我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...
- 动态调用Webservice 支持Soapheader身份验证(转)
封装的WebserviceHelp类: using System; using System.CodeDom; using System.CodeDom.Compiler; using System. ...
- [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现
本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...
随机推荐
- docker 基础知识分享ppt
给团队做的docker基础分享ppt, 见下面的附件. https://files.cnblogs.com/files/harrychinese/docker_intro.pptx
- 关于接口(Interface)
接口,其实是指类之间约定的协议,可以包含方法.属性.事件和索引: 接口成员不允许使用访问修饰符号(public.private.protected.internal),所有的接口成员都是公共的. 接口 ...
- window.location的方法属性详解
示例URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性 含义 值 protocol: 协议 "http:&quo ...
- HTML5 scada 组态工具
底层引擎 提供了基于WebGL的3D技术的图形组件, WebGL基于OpenGL ES 2.0图形接口,因此WebGL属于底层的图形API接口, 二次开发还是有很高的门槛,通过对WebGL底层技术的封 ...
- 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- MinGW GCC 8.3.1 2019年2月23日 出炉啦
GNU 2019-02-22 发布了 GCC 8.3 https://gcc.gnu.org/onlinedocs/8.3.0/ 有详细的说明 MinGW 上可用的 GCC 8.3.1 版本下载地址 ...
- Go语言--基础语法笔记
### 换了工作,好久没有添加新文章了,本来是想更新到github上的,想想还是在博客里放着,感觉以前的文章都没有很仔细,都只是问题处理的记录, 以后想新加一些整理的笔记也好 ### 主要内容 2.1 ...
- Python env使用(virtualenv)
前言 Python 的 virualenv 模块闻名已久,乘着有点时间,学习一下 变更记录 # 19.3.26 创建文章 # 19.3.27 完善文章 正文 安装 pip install virt ...
- Python——类与对象,异常处理
类 class C1: def setdata(self,value): self.data = value def display(self): print(self.data) class C2( ...
- Centos 7部署docker
master安装: 安装zookeeper -openjdk java--openjdk-headless rpm -i packages/mesosphere-zookeeper--.centos7 ...