我在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