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. Spring ElasticsearchTemplate 经纬度按距离排序

    es实体,用 @GeoPointField 注解,值为:中间逗号隔开,如 29.477000,119.278536(经度, 维度) @Document(indexName = "v_inte ...

  2. Spring.Net 入门学习笔记-----one

    一. 基本概念    Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架: Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationC ...

  3. vmware虚拟机磁盘挂载

    执行mount命令时找不到介质或者mount:no medium found的解决办法 使用vmware时,在虚拟机设置里,设置CD/DVD为系统镜像,挂载时,有时会有找不到介质或者no medium ...

  4. 设置下载文件路径 & 获取接口结尾名称。

    // 获取下载位置 private String isExistDir(String saveDir) throws IOException { File downloadFile = new Fil ...

  5. Map的几种取值方法

    public static void main(String[] args) throws IOException,ParseException { Map<String,String> ...

  6. react项目构建

    1.react脚手架 npm install -g create-react-app create-react-app myproject 2.页面配置(bootcdn) <script src ...

  7. 贯穿RobotFramework框架 - 关键字(一) 最全面的疏理

    在RF中,关键字是一个非常重要的存在.想做任何事情,都是通过关键字来实现的. 这篇文章对RobotFramework中的关键字做个整理.大概分为以下几点内容: 1.什么是关键字 2.关键字来自哪里.有 ...

  8. RNN,写起来真的烦

    曾经,为了处理一些序列相关的数据,我稍微了解了一点递归网络 (RNN) 的东西.由于当时只会 tensorflow,就从官网上找了一些 tensorflow 相关的 demo,中间陆陆续续折腾了两个多 ...

  9. SpringMVC整合Thymeleaf3

    (1).pom添加相关依赖 <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thym ...

  10. Linux查看日志工具

    ⒈journalctl journalctl是Centos7才有的工具用于systemd统一管理所有unit的启动日志,只用一个journalctl命令就可以查看所有的日志(包括内核日志和应用日志), ...