WebService客户端引用 服务器无法处理请求。 ---> 未将对象引用设置到对象的实例

问题

服务引用后报错:WebService客户端引用 服务器无法处理请求。 —> 未将对象引用设置到对象的实例

webservice在这里 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather
是一个天气预报的接口 从网页上测试没问题 自己调用时就会出现以下错误(代码里面报错的地方标红了)

“/”应用程序中的服务器错误。
服务器无法处理请求。 —> 未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ServiceModel.FaultException: 服务器无法处理请求。 —> 未将对象引用设置到对象的实例。
源错误:
行 128:

行 129: public string[] getWeather(string theCityCode, string theUserID) {
行 130: return base.Channel.getWeather(theCityCode, theUserID);
行 131: }
行 132:
源文件: c:\Users\Administrator\Desktop\WebApplication2\Service References\WeatherWS\Reference.cs 行: 130
堆栈跟踪:
[FaultException: 服务器无法处理请求。 —> 未将对象引用设置到对象的实例。]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10733331
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
WebApplication2.WeatherWS.WeatherWSSoap.getWeather(String theCityCode, String theUserID) +0
WebApplication2.WeatherWS.WeatherWSSoapClient.getWeather(String theCityCode, String theUserID) in c:\Users\Administrator\Desktop\WebApplication2\Service References\WeatherWS\Reference.cs:130
WebApplication2.WebForm1.test() in c:\Users\Administrator\Desktop\WebApplication2\WebForm1.aspx.cs:19
WebApplication2.WebForm1.Page_Load(Object sender, EventArgs e) in c:\Users\Administrator\Desktop\WebApplication2\WebForm1.aspx.cs:34
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

例子

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void test() {
WeatherWS.WeatherWSSoapClient w = new WeatherWS.WeatherWSSoapClient(“WeatherWSSoap”);
string[] infos = new string[50];
if(w.getWeather(“天津”,”“)!=null)
infos = w.getWeather(“天津”, ” “);
}
protected void Page_Load(object sender, EventArgs e)
{
test();
}

解决办法

这是ASP.NET的问题,Microsoft原想用服务引用完全兼容web引用,但是有时候会出现问题。
你调用的是一个WebService当然最好是使用Web引用的方式了。毕竟WCF是后来出的,说是兼容WebService,但是总归没有原生态的好。所以建议如果是WebService客户端引用一律用Web引用(好像是.NET2.0时就叫Web引用)。
操作步骤:删除之前的服务引用(如果有的话);点击服务引用,点击最底下的高级,点击兼容性中的web引用,输入你的webservice地址(例如天气预报:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx)点击生成,finished!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值