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. 强连通分量(Kosaraju)

    //P2002解题思路: //先求SCC,缩点后,转换为DAG(有向无环图) //在DAG上统计入度为0的scc数量即可 //Kosaraju时间复杂度:O(N+E) //两次DFS,2N,图的转置E ...

  2. C++预编译头文件 – stdafx.h

    预编译头文件的由来 也许请教了别的高手之后,他们会告诉你,这是预编译头,必须包含.可是,这到底是为什么呢?预编译头有什么用呢? 咱们从头文件的编译原理讲起.其实头文件并不神秘,其在编译时的作用,就是把 ...

  3. Web_0002:关于MongoDB的操作

    1,启动moggdb服务端 打开cmd命令窗口进入到MongoDB的安装目录bin文件下: 如:  cd /d F:\Program Files\mongodb\bin   执行如下命令(该命令窗口为 ...

  4. 怎样解决canvas 插件 和html 页面中的事件冲突问题 ?

    很简单 ,在html 执行事件所在的div中 设置 position:relative;    

  5. js时间戳转日期

    //时间戳转日期 2017-04-30 13:20 //type=1--> 2017-04-30 13:20 //type=2-->2018年08月 //type=3-->2018- ...

  6. tomcat设置为开机自启动

    第一步:设置环境变量(在java环境变量配置完成的情况下) 计算机右键——>属性——>高级系统设置——>环境变量——>在用户变量中心新建CATALINA_HOME变量 编辑pa ...

  7. ES7的async/await

    async 表示这是一个async函数,await只能用在这个函数里面. await 表示在这里等待promise返回结果了,再继续执行. await 后面跟着的应该是一个promise对象 awai ...

  8. python numpy 间的的数据变算公式

    import numpy as np a = np.arange(100) print(np.sum(a))#求和 print(np.mean(a))#平均值 print(np.max(a))#最大值 ...

  9. MySQL1:客户端/服务器架构

    一.MySQL的客户端/服务器架构 前言 之前对MySQL的认知只限于会写些SQL,本篇算是笔记,记录和整理下自己对MySQL不熟悉的地方. 大致逻辑: MySQL的服务器程序直接和我们存储的数据打交 ...

  10. Appnium-API-Dvice

    Device Activity Start Activity Start an Android activity by providing package name and activity name ...