为什么我会遇到这种奇怪的序列化异常?

时间:2014-06-09 22:37:39

标签: json silverlight serialization

我有以下课程:

[DataContract]
    public class MapServiceInfo
    {
        private EventHandler _infoRetrievalFinished;
        public event EventHandler InfoRetrievalFinished
        {
            add
            {
                if (this._infoRetrievalFinished == null ||
                    !this._infoRetrievalFinished.GetInvocationList().Contains(value))
                {
                    this._infoRetrievalFinished += value;
                }
            }
            remove { this._infoRetrievalFinished -= value; }
        }

        [DataMember] public string currentVersion { get; set; }
        [DataMember] public string serviceDescription { get; set; }
        [DataMember] public string mapName { get; set; }
        [DataMember] public string description { get; set; }
        [DataMember] public string copyrightText { get; set; }
        [DataMember] public bool supportsDynamicLayers { get; set; }
        [DataMember] public List<LayerInfo> layers { get; set; }

        public MapServiceInfo() { }

        public void RetrieveServiceInfo(string mapServiceUrl)
        {
            string url = mapServiceUrl.TrimEnd("/".ToCharArray()) + "?f=json";
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(new Uri(url));
        }

        private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MapServiceInfo info = null;

                try
                {
                    Stream responseStream = e.Result;
                    StreamReader reader = new StreamReader(responseStream);
                    DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(MapServiceInfo));
                    info = (MapServiceInfo)serializer.ReadObject(responseStream);
                }
                catch (Exception ex) { string message = ex.Message; }
                finally { this.SetInfo(info); }
            }
            else { /**/ }
        }

        public string GetLayerId(string layerName)
        {
            Debug.Assert(!(string.IsNullOrEmpty(layerName)));
            Debug.Assert(!(string.IsNullOrWhiteSpace(layerName)));

            if (layerName == null) { throw new ArgumentNullException("layerName"); }
            else if (string.IsNullOrEmpty(layerName)) { return ""; }
            else if (string.IsNullOrWhiteSpace(layerName)) { return ""; }

            string id = string.Empty;

            if (layers != null)
            {
                foreach (LayerInfo i in layers)
                {
                    if (i.name == layerName)
                    {
                        id = i.id.ToString();
                        break;
                    }
                    else { continue; }
                }
            }
            else { /**/ }

            return id;
        }

        private void SetInfo(MapServiceInfo info)
        {
            Debug.Assert(!(info == null));

            if (info != null)
            {
                this.currentVersion = info.currentVersion;
                this.serviceDescription = info.serviceDescription;
                this.mapName = info.mapName;
                this.description = info.description;
                this.copyrightText = info.copyrightText;
                this.supportsDynamicLayers = info.supportsDynamicLayers;
                this.layers = info.layers;

                this.TriggerInfoRetrievalFinished();
            }
            else { /* Do nothing. */ }
        }

        private void TriggerInfoRetrievalFinished()
        {
            if (this._infoRetrievalFinished != null) { this._infoRetrievalFinished(this, null); }
        }
    }

    [DataContract]
    public class LayerInfo
    {
        [DataMember] public int id { get; set; }
        [DataMember] public string name { get; set; }
        [DataMember] public int parentLayerId { get; set; }
        [DataMember] public bool defaultVisibility { get; set; }
        [DataMember] public List<int> subLayerIDs { get; set; }
        [DataMember] public int minScale { get; set; }
        [DataMember] public int maxScale { get; set; }
    }

我第一次运行我的Silverlight应用程序时,我得到Visual Studio即时调试器弹出说,&#34;未处理的异常(&#39; Silverlight应用程序代码中的未处理错误:4004类别:ManagedRuntimeError消息:System.Runtime.Serialization.SerializationException:&#39; html&gt;&#34;。

如果我说&#34;否&#34; to&#34;您想使用所选的调试器进行调试吗?&#34;并刷新网页,错误不会返回,我的应用程序按预期运行。它只在我第一次进行调试时才会发生。

我已经确定异常是在我的finally块下面的大括号之后抛出的,这个大括号在webClient_OpenReadCompleted()之后,就在else语句之前。我的捕获声明中没有任何内容。

我以前从未见过这个。有谁知道可能会发生什么?

1 个答案:

答案 0 :(得分:0)

问题是读取的数据是不是有效的序列化JSON。

错误消息中的指示符 - SerializationException: 'html><head> - 是一个赠品,数据是[X] HTML文档,不是预期的JSON。

然后,这只是找出(并修复)为什么会发生这种情况的问题 - 也许服务器在这个时候回复了自定义的403或404响应?

但是,此问题与序列化分开 - 无效数据无法可靠地反序列化。