WCF,“反序列化”出错,Why???               
public System.Collections.ObjectModel.ObservableCollection<MyCharts.MyChartsService.Monitoring> EndGetMonitoringEntityData(System.IAsyncResult result) {
                object[] _args = new object[0];
System.Collections.ObjectModel.ObservableCollection<MyCharts.MyChartsService.Monitoring> _result = ((System.Collections.ObjectModel.ObservableCollection<MyCharts.MyChartsService.Monitoring>)(base.EndInvoke("GetMonitoringEntityData", _args, result)));
                return _result;错误信息如下:     格式化程序尝试对消息反序列化时引发异常: 尝试对参数  进行反序列化时出错: monitordate。InnerException 消息是““EndElement”命名空间“”中的“monitordate”并非所需元素。所需元素应为“key”。”。有关详细信息,请参见 InnerException。

“启动Silverlight 的WCF服务 ” 序列化的类:1、父类(其中有一个抽象方法)public abstract class Monitoring
    {
        #region 检索条件
       
        public EnergyType Type
        {
            get; set;
        }        public KeyValuePair<Period, string> Date
        {
            get; set;
        }
        public KeyValuePair<MonitorType, string> Target
        {
            get; set;
        }
        public string UseType
        {
            get; set;
        }
        #endregion        #region 检索结果
        public string SN
        {
            get;
            set;
        }
        public string U
        {
            get; set;
        }
        public double V
        {
            get; set;
        }
        public double ECV
        {
            get;
            set;
        }        public double CUV
        {
            get;
            set;
        }
        public string SS
        {
            get
            {
                switch (Target.Key)
                {
                    case MonitorType.D:
                        return DN;
                    case MonitorType.C:
                        return CN;
                    case MonitorType.B:
                        return BN;
                    case MonitorType.F:
                        return FN;
                    case MonitorType.FR:
                        return string.Format("{0}({1})", FN, RN);
                    case MonitorType.R:
                        return RN;
                    case MonitorType.E:
                        return EN;
                }
                return string.Empty;
            }
        }
        public string E
        {
            get;
            set;
        }
        public string EN
        {
            get;
            set;
        }
        public string R
        {
            get;
            set;
        }
        public string RN
        {
            get;
            set;
        }
        public string F
        {
            get;
            set;
        }
        public string FN
        {
            get;
            set;
        }
        public string B
        {
            get;
            set;
        }
        public string BN
        {
            get;
            set;
        }
        public string C
        {
            get;
            set;
        }
        public string CN
        {
            get;
            set;
        }
        public string D
        {
            get;
            set;
        }
        public string DN
        {
            get;
            set;
        }
        public abstract double[] Datas
        { 
            get;
        }
        #endregion        #region ENUM
        public enum EnergyType
        {
            
            EM,
           
            WM,
           
            GM,
           
            OTHER,
            
            ALL
        }        public enum Period
        {
           
            D,
           
            M,
           
            Y,
            
            X
        }       
        public enum MonitorType
        {
           
            E = 0,
          
            R = 1,
            
            FR = 2,
            
            B = 3,
            
            C = 4,
          
            D = 5,
           
            F= 6
        }
        #endregion
    }2、子类(实现了父类的“Datas”方法):    public class MonitoringYear : Monitoring
    {
        public override double[] Datas
        {
            get
            {
                return new [] { M01, M02, M03, M04, M05, M06, M07, M08, M09, M10, M11, M12 };
            }
        }       
        public double M01
        {
            get; set;
        }       
        public double M02
        {
            get;
            set;
        }       
        public double M03
        {
            get;
            set;
        }       
        public double M04
        {
            get;
            set;
        }     
        public double M05
        {
            get;
            set;
        }        
        public double M06
        {
            get;
            set;
        }       
        public double M07
        {
            get;
            set;
        }      
        public double M08
        {
            get;
            set;
        }      
        public double M09
        {
            get;
            set;
        }       
        public double M10
        {
            get;
            set;
        }       
        public double M11
        {
            get;
            set;
        }        public double M12
        {
            get;
            set;
        }
    }  问题:为什么会反序列化出错呢???   是因为抽象方法“Datas”吗???   在这两个“类”中也没有“monitordate”字段啊???到底怎么回事呢???

解决方案 »

  1.   

    [OperationContract]
        public List<Monitoring> GetMonitoringEntityData(Monitoring.EnergyType energytype, KeyValuePair<Monitoring.Period, string> monitordate, KeyValuePair<Monitoring.MonitorType, string> monitortype, string meterusetype, bool issubstation)
        {
            List<Monitoring> MonitoringEntityDatas = new Mycems.BLL.MonitoringManager(issubstation).GetMonitorDatas(energytype, monitordate, monitortype, meterusetype);
            return MonitoringEntityDatas;
        }
    有一个“KeyValuePair<Monitoring.Period, string> monitordate”参数问题:这个参数为什么不能被序列化???
      

  2.   

    KeyValuePair<Monitoring.Period, string> monitordate中需要有构造函数才可以做序列化的
      

  3.   

    楼主、一楼是同一个人吗?3楼不懂别乱说。那个失败是因为找不到对应值,枚举类型默认对应了一个int值,范序列化到枚举类型时,如果找不到对应的值就会报那个错误。它提示的Key即为Monitoring.Period类型。
      

  4.   

     谢谢您,谢谢
    找不到“Monitoring.Period类型”,是这个原因吗???
      

  5.   

    KeyValuePair”支持默认的序列化啊,实体类“Monitor”没有定义契约也没事。public List<MonitoringEntity> GetMonitoringReggionDatas(Monitoring.EnergyType energytype, KeyValuePair<Monitoring.Period, string> monitordate, KeyValuePair<Monitoring.MonitorType, string> monitortype, string meterusetype, bool issubstation)就是调用“服务接口类”的“服务方法”中“不能有”KeyValuePair“类型???Why???    您好,谢谢您。    
     [DataContract]
      public sealed class KeyValuePair<TKey, TValue>
      {
        [DataMember(Name = "key")]
        public TKey Key { get; set; }    [DataMember(Name = "value")]
        public TValue Value { get; set; }
      }这个类,是定义在“接口服务类”中,还是定义在实体类“Monitor”中
      

  6.   

    我不知道你的引用方式,最好是有个接口项目,里面存放所有的接口定义和实体类,同时给客户端和服务端引用,这样就避免了命名空间不一致的问题,另外你这里也用到了抽象类,因此需要对实际可能涉及到的派生类进行ServiceKnownType标注(其实可以放到接口定义上的,不需要每个内部方法标注)