我正在尝试将服务编写为可获取天气数据的练习。我从控制台运行时已经运行了,但是当我启动服务时它失败并出现以下错误
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.Serialization.SerializationException
Stack:
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(System.Runtime.Serialization.XmlReaderDelegator, Boolean, System.Runtime.Serialization.DataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(System.Xml.XmlDictionaryReader)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(System.IO.Stream)
at LyncWeather.WeatherGetter.ConvertStreamToWeatherData(System.IO.Stream)
at LyncWeather.WeatherGetter.GetWeatherByCityId(Int32)
at LyncWeather.Service1.UpdateLync(System.Object)
at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()
at System.Threading.TimerQueue.AppDomainTimerCallback()
以下是我的数据合约类。
namespace LyncWeather
{
[DataContract (Name = "coord")]
public class Coord
{
[DataMember(Name = "lon")]
public double Lon { get; set; }
[DataMember(Name = "lat")]
public double Lat { get; set; }
}
[DataContract(Name = "Weather")]
public class Weather
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "main")]
public string Main { get; set; }
[DataMember(Name = "description")]
public string Description { get; set; }
[DataMember(Name = "icon")]
public string Icon { get; set; }
}
[DataContract (Name = "Main")]
public class Main
{
[DataMember(Name = "temp")]
public double Temp { get; set; }
[DataMember(Name = "pressure")]
public int Pressure { get; set; }
[DataMember(Name = "humidity")]
public int Humidity { get; set; }
[DataMember(Name = "temp_min")]
public double TempMin { get; set; }
[DataMember(Name = "temp_max")]
public double TempMax { get; set; }
}
[DataContract(Name = "Wind")]
public class Wind
{
[DataMember(Name = "speed")]
public double Speed { get; set; }
[DataMember(Name = "deg")]
public double Deg { get; set; }
}
[DataContract (Name = "Clouds")]
public class Clouds
{
[DataMember(Name = "all")]
public int All { get; set; }
}
[DataContract(Name = "Sys")]
public class Sys
{
[DataMember(Name = "type")]
public int Type { get; set; }
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "message")]
public double Message { get; set; }
[DataMember(Name = "country")]
public string Country { get; set; }
[DataMember(Name = "sunrise")]
public int Sunrise { get; set; }
[DataMember(Name = "sunset")]
public int Sunset { get; set; }
}
[DataContract (Name = "WeatherData")]
public class WeatherDataClass
{
[DataMember(Name = "coord")]
public Coord CoordData { get; set; }
[DataMember(Name = "weather")]
public List<Weather> WeatherData { get; set; }
[DataMember(Name = "main")]
public Main MainData { get; set; }
[DataMember(Name = "wind")]
public Wind WindData { get; set; }
[DataMember(Name = "clouds")]
public Clouds CloudsData { get; set; }
[DataMember(Name = "dt")]
public int Dt { get; set; }
[DataMember(Name = "sys")]
public Sys SysData { get; set; }
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
}
}
这是我调用DataContractSerializer
的地方public WeatherDataClass ConvertStreamToWeatherData(Stream Json)
{
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(WeatherDataClass));
return (WeatherDataClass)js.ReadObject(Json);
}
有谁知道为什么这可能在VS中工作而不是从服务中工作?除非有这样的方法,我可以得到一个更明确的错误,找到它失败的数据库?
感谢您的帮助!