Code
/// <summary>
/// 动态调用 WebService的代理类
/// </summary>
class WebServiceHelper
{
/// <summary>
/// 动态调用web服务
/// </summary>
/// <param name="url">web服务的地址</param>
/// <param name="methodname">方法名</param>
/// <param name="args">参数</param>
/// <returns></returns>
public static object InvokeWebService(string url, string methodname, object[] args)
{
return WebServiceHelper.InvokeWebService(url, null, methodname, args);
}
public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
if ((classname == null) || (classname == ""))
{
classname = WebServiceHelper.GetWsClassName(url);
}
try
{ // 1. 使用 WebClient 下载 WSDL 信息
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(description, "", "");
CodeNamespace cn = new CodeNamespace();// 为代理类添加命名空间,缺省为全局空间
//3.生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider icc = new CSharpCodeProvider();
//4.设定编译参数
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");
//5.编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//6.生成代理实例,使用 Reflection并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(classname,true,true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo method = t.GetMethod(methodname);
return method.Invoke(obj, args);
}
catch (Exception)
{
return null;
}
}
private static string GetWsClassName(string wsUrl)
{
string[] parts = wsUrl.Split('/');
string[] pps = parts[parts.Length - 1].Split('.');
return pps[0];
}
}
我也是借助网络上的勉强写成,可能有问题,但我用好着,希望给需要帮助的朋友参考下。下面是我自己调用:/// <summary>
/// 动态调用 WebService的代理类
/// </summary>
class WebServiceHelper
{
/// <summary>
/// 动态调用web服务
/// </summary>
/// <param name="url">web服务的地址</param>
/// <param name="methodname">方法名</param>
/// <param name="args">参数</param>
/// <returns></returns>
public static object InvokeWebService(string url, string methodname, object[] args)
{
return WebServiceHelper.InvokeWebService(url, null, methodname, args);
}
public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
if ((classname == null) || (classname == ""))
{
classname = WebServiceHelper.GetWsClassName(url);
}
try
{ // 1. 使用 WebClient 下载 WSDL 信息
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(description, "", "");
CodeNamespace cn = new CodeNamespace();// 为代理类添加命名空间,缺省为全局空间
//3.生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider icc = new CSharpCodeProvider();
//4.设定编译参数
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");
//5.编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//6.生成代理实例,使用 Reflection并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(classname,true,true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo method = t.GetMethod(methodname);
return method.Invoke(obj, args);
}
catch (Exception)
{
return null;
}
}
private static string GetWsClassName(string wsUrl)
{
string[] parts = wsUrl.Split('/');
string[] pps = parts[parts.Length - 1].Split('.');
return pps[0];
}
}
object tablejhinfo = WebServiceHelper.InvokeWebService(Form2.m_web_address, "GetIhDatabase", new Object[] {stt, jhname });
而中的Form2.m_web_address是我的WebService的地址,因为我放在.config中的。GetIhDatabase是我的调用的方法名,后面是方法的参数。