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>
上一篇:python--使用递归的方式建立二叉树


下一篇:面试求职中需要了解的Java多线程知识