动态调用WebService 的问题~~ 不知道为什么

Pony999 2009-12-30 11:39:05
在网上看了一个动态调用 WebService的代理类,试了试,不知道为什么,调用其他webserver都不行,只有调用这个实例上的一个地址(http://www.webservicex.net/globalweather.asmx)才行,我调用其他地址都不行,然后自己弄了一个简单webserver也还是不行,执行到
                Type t = assembly.GetType(@namespace + "." + classname, true, true);    //类类型
就不行了,错误提示为
{"未能从程序集“sta-qnph, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“Service”。":"Service"}
[System.TypeLoadException]: {"未能从程序集“sta-qnph, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“Service”。":"Service"}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: null
Message: "未能从程序集“sta-qnph, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“Service”。"
Source: "mscorlib"
StackTrace: " 在 System.Reflection.Assembly._GetType(String name, Boolean throwOnError, Boolean ignoreCase)\r\n 在 System.Reflection.Assembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)\r\n 在 WebServiceHelper.InvokeWebService(String url, String classname, String methodname, Object[] args) 位置 e:\\WWW\\New\\Index\\App_Code\\WebServiceHelper.cs:行号 97"
TargetSite: {System.Type _GetType(System.String, Boolean, Boolean)}



调用

string url = "http://www.webservicex.net/globalweather.asmx" ;
string[] args = new string[2] ;
args[0] = "ShangHai";
args[1] = "China" ;
object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;
Response.Write(result.ToString());








public static object InvokeWebService(string url, string methodname, object[] args)
{
return InvokeWebService(url, null, methodname, args);
}

public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((classname == null) || (classname == ""))
{
classname = GetWsClassName(url);
}

try
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream); //xml web services 类实例
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(); //C#代码编译器和代码生成器类实例
ICodeCompiler icc = csc.CreateCompiler();

//设定编译参数
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); //类类型
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname); //方法类型

return mi.Invoke(obj, args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
}

private static string GetWsClassName(string wsUrl)
{
string[] parts = wsUrl.Split('/');
string[] pps = parts[parts.Length - 1].Split('.');

return pps[0];
}

...全文
824 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsnihao520jj 2011-03-07
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 liumrzy 的回复:]
怎么取java webservice的类名呀?
[/Quote]
其实只要在vs里面导入web引用输入就可以看见类名。
还有一种方法就是通过代码动态获得类名。
hyaoyan 2010-03-29
  • 打赏
  • 举报
回复
同意楼上
LIUMRZY 2010-03-29
  • 打赏
  • 举报
回复
怎么取java webservice的类名呀?
Pony999 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 goat_wolf 的回复:]
我尝试过不传类名,结果返回的是T是空值,也就是意味着对方是申明了类名的,我就必须按照对方申明的类名进行传,是吗?我拿到的只有对方的地址,该如何去获取对方的类名呢?不是很了解,希望大大解释下。
[/Quote]
当你打开webserver的时候,左上角蓝底的字就是类名,比如“http://www.webservicex.net/globalweather.asmx”这个webserver上显示的“GlobalWeather”就是类名
  • 打赏
  • 举报
回复
mark in mind...
[Quote=引用 17 楼 ahwwmb 的回复:]
引用 14 楼 chichenzhe 的回复:
麻烦LZ如果自己解决问题了的话 把解决方法说一下....  这样也是有效提问 对大家都有帮助.


其实代码没问题,是我没看好,之前拿到代码没看就试,所以没试出来,后来看了看代码才发现,“InvokeWebService”是个重载函数,如果webserver没有申明类名,就不传类名,代码就自动获取默认的类名,如果有申明类名,就必须要传,否则就找不到类,我就是这个原因导致出错的
[/Quote]
goat_wolf 2009-12-31
  • 打赏
  • 举报
回复
我尝试过不传类名,结果返回的是T是空值,也就是意味着对方是申明了类名的,我就必须按照对方申明的类名进行传,是吗?我拿到的只有对方的地址,该如何去获取对方的类名呢?不是很了解,希望大大解释下。
Aslangg 2009-12-31
  • 打赏
  • 举报
回复
mark
Pony999 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 chichenzhe 的回复:]
麻烦LZ如果自己解决问题了的话 把解决方法说一下....  这样也是有效提问 对大家都有帮助.

[/Quote]
其实代码没问题,是我没看好,之前拿到代码没看就试,所以没试出来,后来看了看代码才发现,“InvokeWebService”是个重载函数,如果webserver没有申明类名,就不传类名,代码就自动获取默认的类名,如果有申明类名,就必须要传,否则就找不到类,我就是这个原因导致出错的
Pony999 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 goat_wolf 的回复:]
我也遇到了同个问题,想问下楼主原因是什么,我还没找到,如何解决。。。。
[/Quote]


其实代码没问题,是我没看好,之前拿到代码没看就试,所以没试出来,后来看了看代码才发现,“InvokeWebService”是个重载函数,如果webserver没有申明类名,就不传类名,代码就自动获取默认的类名,如果有申明类名,就必须要传,否则就找不到类,我就是这个原因导致出错的
风龙-3 2009-12-31
  • 打赏
  • 举报
回复
从上面看出动态Webservice调用主要使用动态编译以及C#的反射,如果发布的WebService不支持反射或者不是用C#发布,则C#的反射机制可能取不到类,从而报错;
chichenzhe 2009-12-31
  • 打赏
  • 举报
回复
麻烦LZ如果自己解决问题了的话 把解决方法说一下.... 这样也是有效提问 对大家都有帮助.
goat_wolf 2009-12-31
  • 打赏
  • 举报
回复
继续等有大大能解决问题。。。
goat_wolf 2009-12-31
  • 打赏
  • 举报
回复
对方的webservice是JAVA写的,没有看到类名,只有页面报文。。。
如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://sms.ccit.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://sms.ccit.com" xmlns:intf="http://sms.ccit.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <!-- WSDL created by Apache Axis version: 1.2.1
Built on Aug 08, 2005 (11:49:10 PDT)
-->
- <wsdl:types>
- <schema targetNamespace="http://sms.ccit.com" xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
- <complexType name="ArrayOf_xsd_string">
- <complexContent>
- <restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]" />
</restriction>
</complexContent>
</complexType>
- <complexType name="ResponseInfo">
- <sequence>
<element name="p_IResult" type="xsd:int" />
<element name="p_SMsg" nillable="true" type="xsd:string" />
</sequence>
</complexType>
</schema>
</wsdl:types>

ginni215 2009-12-30
  • 打赏
  • 举报
回复
有添加这个namespace + "." + classname类所在的程序集的引用吗?
myrroom 2009-12-30
  • 打赏
  • 举报
回复
参数不同呗,可以自己先用vs自动生成一个,然后修改.cs文件就OK了
erydemimi 2009-12-30
  • 打赏
  • 举报
回复
你为什么要用动态调用??
LutzMark 2009-12-30
  • 打赏
  • 举报
回复
web reference problem?
up
antony1029 2009-12-30
  • 打赏
  • 举报
回复
接分啊!!!
goat_wolf 2009-12-30
  • 打赏
  • 举报
回复
大大帮忙啊,在线等。。。。
kensouterry 2009-12-30
  • 打赏
  • 举报
回复
Mark 这会儿比较忙了!
加载更多回复(5)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧