动态引用WebService,建立WebService虚拟机
2024-07-21 02:21:18
供稿:网友
建立webservice虚拟代理,使用xml登记webservice的引用地址。实现动态引用webservice.
使用技术
1、 动态编译
2、 反射技术
实现代码
using system;
using system.reflection ;
using system.web .services .description ;
using microsoft.csharp;
using system.codedom ;
using system.codedom.compiler ;
using system.io ;
using system.text ;
using system.xml ;
using system.net ;
using webserviceproxy;
namespace webserviceproxy
{
public class webserviceproxy
{
private assembly _ass = null;
private string _protocolname = "soap";
private string _srcwsproxy = string.empty;
public assembly assembly { get{ return _ass; } }
public string protocolname
{ get{ return _protocolname; } set {_protocolname = value; } }
public string srcwsproxy { get{ return _srcwsproxy; } }
public webserviceproxy ()
{
}
public webserviceproxy (string wsdlsourcename)
{
assemblyfromwsdl(getwsdl(wsdlsourcename));
}
public string wsdlfromurl(string url)
{
webrequest req = webrequest.create(url);
webresponse result = req.getresponse();
stream receivestream = result.getresponsestream();
encoding encode = system.text.encoding.getencoding("utf-8");
streamreader sr = new streamreader( receivestream, encode );
string strwsdl = sr.readtoend();
return strwsdl;
}
public string getwsdl(string source)
{
if(source.startswith("<?xml version") == true)
{
return source;
}
else
if(source.startswith("http://") == true)
{
return wsdlfromurl(source);
}
return wsdlfromfile(source);
}
public string wsdlfromfile(string filefullpathname)
{
fileinfo fi = new fileinfo(filefullpathname);
if(fi.extension == "wsdl")
{
filestream fs = new filestream(filefullpathname, filemode.open,
fileaccess.read);
streamreader sr = new streamreader(fs);
char[] buffer = new char[(int)fs.length];
sr.readblock(buffer, 0, (int)fs.length);
return new string(buffer);
}
throw new exception("this is no a wsdl file");
}
public assembly assemblyfromwsdl(string strwsdl)
{
// xml text reader
stringreader wsdlstringreader = new stringreader(strwsdl);
xmltextreader tr = new xmltextreader(wsdlstringreader);
servicedescription sd = servicedescription.read(tr);
tr.close();
// wsdl service description importer
codenamespace cns = new codenamespace("webserviceproxy.webserviceaccessor");
servicedescriptionimporter sdi = new servicedescriptionimporter();
sdi.addservicedescription(sd, null, null);
sdi.protocolname = _protocolname;
sdi.import(cns, null);
// source code generation
csharpcodeprovider cscp = new csharpcodeprovider();
icodegenerator icg = cscp.creategenerator();
stringbuilder srcstringbuilder = new stringbuilder();
stringwriter sw = new stringwriter(srcstringbuilder);
icg.generatecodefromnamespace(cns, sw, null);
_srcwsproxy = srcstringbuilder.tostring();
sw.close();
// assembly compilation.
compilerparameters cp = new compilerparameters();
cp.referencedassemblies.add("system.dll");
cp.referencedassemblies.add("system.xml.dll");
cp.referencedassemblies.add("system.web.services.dll");
cp.generateexecutable = false;
cp.generateinmemory = true;
cp.includedebuginformation = false;
icodecompiler icc = cscp.createcompiler();
compilerresults cr = icc.compileassemblyfromsource(cp, _srcwsproxy);
if(cr.errors.count > 0)
throw new exception(string.format("build failed: {0} errors",
cr.errors.count));
return _ass = cr.compiledassembly;
}
public object createinstance(string objtypename)
{
type t = _ass.gettype("webserviceproxy.webserviceaccessor" + "." + objtypename);
return activator.createinstance(t);
}
public object invoke(object obj, string methodname, params object[] args)
{
methodinfo mi = obj.gettype().getmethod(methodname);
return mi.invoke(obj, args);
}
}
}
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。