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

  1. Atitit 动态调用webservice与客户端代理方式调用

    Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke  直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...

  2. 动态调用WebService(C#) (非常实用)

    通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...

  3. 动态调用webservice(部分转载)

    动态调用webservice,做个笔记: public class WSHelper { /// < summary> /// 动态调用web服务 /// < /summary> ...

  4. C# 动态调用webservice

    最近项目中,用到动态调用webservice的内容,此处记录下来,留着以后COPY(我们只需要在XML,config文件,或者数据库中配置webservice连接地址和方法名即可使用): using ...

  5. 动态调用webservice及WCF服务

    动态调用web服务,该方法只针对Web service, WCF的服务不行,如果是WCF的就通过工具直接生产代理类,把代理类配置到调用的项目中,通过配置客户端的终结点动态的取实现: 通过Svcutil ...

  6. C# .NET 动态调用webservice的三种方式

    转载自 百度文库 http://wenku.baidu.com/link?url=Q2q50wohf5W6UX44zqotXFEe_XOMaib4UtI3BigaNwipOHKNETloMF4ax4W ...

  7. WebService – 2.动态调用WebService

    在本节课程中,将演示如何通过程序动态添加.调用.编译.执行WebService并返回结果. WebService动态调用示意图 WebService相关知识 代码文档对象模型CodeDom的使用 编程 ...

  8. 用C#通过反射实现动态调用WebService 告别Web引用

    我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...

  9. 动态调用Webservice 支持Soapheader身份验证(转)

    封装的WebserviceHelp类: using System; using System.CodeDom; using System.CodeDom.Compiler; using System. ...

  10. [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现

    本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...

随机推荐

  1. <HTML>初识HTML

    最近在阅读Head first HTML and CSS, 写一些笔记.   小知识: 1. 浏览器会忽略HTML文档中的制表符,回车和大部分空格——要用标记 2. WYSIWYG——使得用户在视图中 ...

  2. Precision 7520双硬盘无法识别固态硬盘

    将RAID ON 修改为AHCI,如图1,会使得 win10无法 启动,如图2 图 1 图 2 可以开legacy,如图3,让电脑可以从u盘启动,如图4,但是也无法查看到固态硬盘 图 3 图 4 网上 ...

  3. uCosII中的任务

    任务基本概念 任务是一个接受操作系统管理的独立运行单元,在uCosII中类似与普通平台上的main()函数,需要自己来保护其因调用或中断二产生的断点,所以需要一个自己的私有堆栈,即任务堆栈: 任务有两 ...

  4. 11GR2 Oracle数据库的远程投毒VNCR方式修复

    [环境介绍] 系统环境:Solaris + Oracle 11GR2 + 单机/RAC   [背景描述] 基于集团数据库安全检查项,需要数据库的远程投毒漏洞进行修复. 根据Oracle官方提供的修复文 ...

  5. Python——正则表达式初步应用(一)

    1.先附上转载(www.cnblogs.com/huxi)的一张图,有重要的参考价值,其含义大家请通过阅读来理解. 2.附上初步学习Python时编写的一个爬糗事百科段子的代码. # -*- codi ...

  6. 在页面加载前先出现加载loading,页面加载完成之后再显示页面

    在此加入一个关于页面加载成功之前先展现一个loading的案例: 如下代码写入js里放在html头部即可实现需求:添加的可以自己在css文件设置宽高,也可以放入一个background的gif的loa ...

  7. 【3】学习C++之const关键字的使用

    在C++中,const关键字是控制变量是否可以变化的,是否能够用好const关键字是区别小白和大佬的重要指标(大雾). 1.const与基本数据类型 ; //a是变量,a的值可以在后续操作中进行更改. ...

  8. go 【第二篇】包、变量、函数

    包 初试 每个 Go 程序都是由包组成的. 程序运行的入口是包 `main`. 这个程序使用并导入了包 "fmt" 和 `"math/rand"`. 按照惯例, ...

  9. JAVA的抽象类和接口

    抽象类 在面向对象的概念中,所有的对象都是通过类来描述的,但是反过来,并不是所有的类都是用来描述对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对 ...

  10. SQL - for xml path('') 实现多行合并到一行, 并带有分隔符

    docs.microsoft.com 链接:  SQL一个应用场景与FOR XML PATH应用 首先呢!我们在增加一张学生表,列分别为(stuID,sName,hobby),stuID代表学生编号, ...