我在Final()方法中遇到了一些麻煩。它應該返回IWeather的列表,但在我調用它時返回null。在調試我停在什麼都不返回
return this.returner;
,但它總是空,我不知道爲什麼,因爲MainMethod()返回「完成」,並列出「武者回歸」不爲空時,調試是MainMethod()。
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using LibW;
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AllInOne
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
[DataMember]
private List<LibW.IWeather> returner = new List<LibW.IWeather>();
/// <summary>
/// method set connection to google and get xml document weather for there
/// </summary>
/// <param name="city">city for which find weather</param>
/// <param name="lang">lang of text</param>
/// <returns>return either "finish if all successful or Exception msg or errors with city finding and error with connection</returns>
[OperationContract]
public string MainMethod(string city, string lang)
{
//check connection
Ping p = new Ping();
PingReply pr = p.Send(@"google.com");
IPStatus status = pr.Status;
if (status != IPStatus.Success)
return "Error with Connection";
//try tp get xml weather
try
{
XElement el;
HttpWebRequest req =
(HttpWebRequest) WebRequest.Create("http://www.google.com/ig/api?weather=" + city + "&hl=" + lang);
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
StringBuilder sb = new StringBuilder();
using (StreamReader streamreader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(1251)))
{
el = XElement.Load(streamreader);
}
int addv = 0;
var v = from c in el.Elements()
select c;
//I get here data from XML(condition,temperature and etc.)
return "finish";
}
catch (Exception exc)
{
return exc.Message;
}
}
/// <summary>
/// return list of weather fot 4 days
/// </summary>
/// <returns>list</returns>
[OperationContract]
public List<IWeather> Final()
{
return this.returner;
}
}
當你說'returner'是'null'時,你真的指'null'還是你的意思是它是一個空的'List <>'? – 2012-03-29 16:57:26