根据Webservice地址,动态传入参数(Webservice代理类)
using system;
using system.collections.generic;
using system.linq;
using system.text;
using sunlib;
using system.net;
using system.io;
using system.web.services.description;
using system.codedom;
using microsoft.csharp;
using system.codedom.compiler;
using system.reflection;
public class webservice
{
public static object invokewebservice(string url , string methodname , object[] args)
{
string @namespace="client";
stringbuilder sb = new stringbuilder();
try
{
webclient ws = new webclient();
stream stream=ws.openread(url+"?wsdl");
servicedescription sd=servicedescription.read(stream);
string classname=sd.services[0].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();
//设定编译参数
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 cs =csc.compileassemblyformdom(cplist,ccu);
if(cs.errors.haserrors)
{
foreach(compilererror ce in cs.errors)
{
sb.append(ce.tostring()).appendline();
}
throw new exception(sb.tostring());
}
assembly assembly =cs.compiledassembly;
type t = assembly.gettype(@namespace+"."+classname,true,true);
object obj =activator.createinstance(t);
methodinfo mi=t.getmethod(methodname);
return mi.invoke(obj,args);
}
catch(exception e)
{
sb.append(e.message).appendline();
return null;
}
finally
{
loghelper.writetxt(sb.tostring());
}
}
}
public class updatedata
{
public dynamic updatedatas(params string[] args)
{
//user用户名;pwd密码
string result=webservice.invokewebservice(webserviceurl,methodname,new object[]{ user,pwd,args[0],args[1],args[2],args[3] }).tostring();
//string result=webservice.invokewebservice(webserviceurl,methodname,new object[]{args }).tostring();
}
}
public class synsreaddata
{
public void readdata()
{
//
updatedata updatedata=new updatedata();
dynamic rlt=updatedata.updatedatas(usercode,metercode,marktime,markdata);
//dynamic rlt=updatedata.updatedatas(result.tostring());//result为json数据
if(rlt==null)
{
sb.append("webservice调用异常返回null").appendline();
continue;
}
//返回调用结果
string execcode=rlt.retinfo.code.tostring();
}
}
上一篇: C#时间戳的简单实现
下一篇: RPC理解