服务器无法处理请求。 --->
未将对象引用设置到对象的实例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:UsersAdministratorDesktopWebApplication2Service ReferencesWeatherWSReference.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:UsersAdministratorDesktopWebApplication2Service ReferencesWeatherWSReference.cs:130
WebApplication2.WebForm1.test() in c:UsersAdministratorDesktopWebApplication2WebForm1.aspx.cs:19
WebApplication2.WebForm1.Page_Load(Object sender, EventArgs e) in c:UsersAdministratorDesktopWebApplication2WebForm1.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();
}
------解决思路----------------------默认添加服务引用是通过WCF的方式添加的,这种方式可能会存在问题,添加Web服务的话是按照wsdl协议的
------解决思路----------------------你调用的是一个WebService当然最好是使用Web引用的方式了。毕竟WCF是后来出的,说是兼容WebService,但是总归没有原生态的好。
------解决思路----------------------服务引用需要有config中添加wcf客户端终节点配置,很长一段
<system.serviceModel>
<bindings>
<customBinding>
<binding name="XXX">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true">
</httpTransport>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://....." bindingConfiguration="XXX" contract="服务引用的命名空间" name="XXXPort" />
</client>
</system.serviceModel>
服务引用默认用的是messageVersion="Soap12" 这个可以改成跟服务端一样的,web引用用的是Soap1.1,还没有碰到1.2不兼容1.1的情况。一般不会导致错误,只是在请求时,服务端日志可能会有个警告
比较倾向你代码逻辑问题