1.在webconfig写:
<add key="webservices" value="http://127.0.0.1/SendMessageServices/Send_Gsm_New.asmx"/>
2.新建类:WS_Read
using System;
using System.Collections;
using System.Text;
using System.Web.Services.Description;
using System.Configuration;
namespace Message
{
/// <summary>
/// WS_Read 的摘要说明。
/// </summary>
public class WS_Read
{
/// <param name="sUrl">WebService的http形式的地址</param>
/// <param name="NameSpace">欲调用的WebService的命名空间</param>
/// <param name="ClsName">欲调用的WebService的类名(不包括命名空间前缀)</param>
/// <param name="MethodName">欲调用的WebService的方法名</param>
/// <param name="args">参数列表</param>
/// <param name="sErrMsg">用于接收错误信息</param>
/// <returns>WebService的执行结果</returns>
/// <remarks>
/// 如果调用失败,将会通过参数ErrMsg返回错误信息
/// 异常信息可能会发生在两个地方:
/// 1、动态构造WebService的时候,CompileAssembly失败。
/// 2、WebService本身执行失败。
/// </remarks>
public static object InvokeWebservice(string sUrl,string @NameSpace,string ClsName,string MethodName,object[] args,out string sErrMsg)
{
sErrMsg = "";
if (sUrl == "")
{
sErrMsg = "";
return null;
}
if (@NameSpace == "")
{
sErrMsg = "名称空间名不能为空";
return null;
}
if (ClsName == "")
{
sErrMsg = "类名不能为空!";
return null;
}
if (MethodName == "")
{
sErrMsg = "方法名不能为空!";
return null;
}
try
{
//获取描述文档
//并加载到流中
System.IO.Stream WSDL_Stream = new System.Net.WebClient().OpenRead(sUrl + "?WSDL");
//开始处理描述文档
//step.1处理文档描述
ServiceDescriptionImporter my_SerDesrImport = new ServiceDescriptionImporter();
my_SerDesrImport.AddServiceDescription(ServiceDescription.Read(WSDL_Stream), "", "");
//step.2加载文档名称空间
System.CodeDom.CodeNamespace my_CodeNamespace = new System.CodeDom.CodeNamespace(@NameSpace);
System.CodeDom.CodeCompileUnit my_CodeCompileUnit = new System.CodeDom.CodeCompileUnit();
my_CodeCompileUnit.Namespaces.Add(my_CodeNamespace);
//step.3获取名称空间中导入内容的集合
my_SerDesrImport.Import(my_CodeNamespace, my_CodeCompileUnit);
//step.4访问代码编译器,构建动态编译器实例
Microsoft.CSharp.CSharpCodeProvider my_csc = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler my_Icc = my_csc.CreateCompiler();
//step.5设置编译引用文件列表
System.CodeDom.Compiler.CompilerParameters cpFileList = new System.CodeDom.Compiler.CompilerParameters();
cpFileList.GenerateExecutable = false;
cpFileList.GenerateInMemory = true;
cpFileList.ReferencedAssemblies.Add("System.dll");
cpFileList.ReferencedAssemblies.Add("System.XML.dll");
cpFileList.ReferencedAssemblies.Add("System.Web.Services.dll");
cpFileList.ReferencedAssemblies.Add("System.Data.dll");
//step.6执行动态编译
System.CodeDom.Compiler.CompilerResults my_DnyComRet = my_Icc.CompileAssemblyFromDom(cpFileList, my_CodeCompileUnit);
if (true == my_DnyComRet.Errors.HasErrors) //=true 表示编译失败,将返回错误信息
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in my_DnyComRet.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
sErrMsg = sb.ToString();
return null;
}
//编译结束,并且编译成功
//=====================================================//
//开始获取程序集
//*******************************************************
//step.1获取已编译侧程序集
System.Reflection.Assembly assembly = my_DnyComRet.CompiledAssembly;
//step.2 从程序集中获取程序对象类型
Type tTmp = assembly.GetType(@NameSpace + "." + ClsName, true, true);
if (tTmp == null)
{
sErrMsg = "获取对象类型失败!";
return null;
}
//step.3 执行并返回执行结果
return tTmp.GetMethod(MethodName).Invoke(Activator.CreateInstance(tTmp)/*获取对象实例*/, args);
//
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message,
new Exception(ex.InnerException.StackTrace));
}
}
public static string Get_WS(string parm)
{
string result = ConfigurationSettings.AppSettings[parm].ToString();
return result;
}
}
}
3.调用类的方法:
public int ChargeUp(string acco, string pass)
{
int result;
string str_ws=WS_Read.Get_WS("webservices2");
string ErrMsg=String.Empty;
object[] args = new object[2];
args[0] =acco;
args[1] =pass;
result = (int)WS_Read.InvokeWebservice(str_ws, "SendMessageService", "Send_Gsm_New", "ChargeUp", args, out ErrMsg);
return result;
}