调用.cls 结尾的webService文件
string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((classname == null) || (classname == ""))
{
classname = GetWsClassName(url);
}
try
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = null;
try
{
stream = wc.OpenRead(url+"?WSDL");
}
catch
{
msg = "webService服务打开失败!请检查网络或者确认webService服务器是否可以连接!";
sc.AddLog.WriteLog(msg);
}
ServiceDescription sd = ServiceDescription.Read(stream); //报错点!!!!!!!!
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 icc = 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 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());
}
//生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
#region
CodeNamespace cn1 = new CodeNamespace(@namespace);
CodeCompileUnit ccu1 = new CodeCompileUnit();
Microsoft.CSharp.CSharpCodeProvider icc1 = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
CompilerResults cr1 = icc1.CompileAssemblyFromDom(cp, ccu);
#endregion
//Type t = cr1.CompiledAssembly.GetTypes()[0];
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
if (mi == null)
{
msg += "找不到终端确认的webService中的方法:" + methodname;
sc.AddLog.WriteLog("开始" + msg);
}
//释放
if (stream != null)
{
stream.Close();
stream.Dispose();
}
try
{
re = mi.Invoke(obj, args).ToString();
sc.AddLog.WriteLog(re);
}
catch(Exception ex) {
sc.AddLog.WriteLog(msg);
}
}
catch (Exception ex)
{
msg += ex.ToString();
sc.AddLog.WriteLog(msg);
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
sc.AddLog.WriteLog(re);
return re;
//报错信息如下
System.InvalidOperationException: XML 文档(1, 2)中有错误。 ---> System.InvalidOperationException: 不应有 <html xmlns=''>。
在 System.Web.Services.Description.ServiceDescriptionSerializationReader.Read125_definitions()
在 System.Web.Services.Description.ServiceDescription.ServiceDescriptionSerializer.Deserialize(XmlSerializationReader reader)
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
在 System.Web.Services.Description.ServiceDescription.Read(XmlReader reader, Boolean validate)
在 System.Web.Services.Description.ServiceDescription.Read(Stream stream, Boolean validate)
在 System.Web.Services.Description.ServiceDescription.Read(Stream stream)