代码改变世界

Silverlight3+WCF遇到的问题(二):wcf system.servicemodel.communicationexception

2010-01-28 13:43  Virus-BeautyCode  阅读(3559)  评论(1编辑  收藏  举报

 

  以前我访问的数据库都是一张表,没有关联,昨天添加了两张表,一共两张表,用户表和用户类型表,然后修改了原来的两个实体类

 

  用户信息实体类

 

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Domain.Entity
{
    [DataContract]
    
public class Customer : INotifyPropertyChanged
    {
        
private int _intCustomerId;
        
private string _strCustomerName;
        
private string _strCustomerCode;
        
private CustomerType  _CustomerType;
        
private int _intCustomerTypeId;

        [DataMember ]
        
public virtual  int CustomerTypeId
        {
            
get { return _intCustomerTypeId; }
            
set
            {
                _intCustomerTypeId 
= value;
                OnPropertyChanged(
"CustomerTypeId");
            }
        }

        [DataMember ]
        
public virtual  CustomerType  CustomerType
        {
            
get { return this._CustomerType; }
            
set
            {

                
this._CustomerType = value;
                OnPropertyChanged(
"CustomerType");
            }
        }

        [DataMember]
        
public virtual int CustomerId
        {
            
get { return this._intCustomerId; }
            
set
            {
                
this._intCustomerId = value;
               OnPropertyChanged(
"CustomerId");
            }
        }
        [DataMember]
        
public virtual string CustomerName
        {
            
get { return this._strCustomerName; }
            
set
            {
                
this._strCustomerName = value;
               OnPropertyChanged(
"CustomerName");
            }
        }
        [DataMember]
        
public virtual string CustomerCode
        {
            
get { return _strCustomerCode; }
            
set
            {
                
this._strCustomerCode = value;
                OnPropertyChanged(
"CustomerCode");
            }
        }

        
#region INotifyPropertyChanged Members

        
public event PropertyChangedEventHandler PropertyChanged;

        
#endregion
        
private void OnPropertyChanged(string propertyName)
        {
            
if (PropertyChanged != null)
            {
                PropertyChanged(
thisnew PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

 

  用户类型实体类

  

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Domain.Entity
{
    [DataContract]
    
public class CustomerType : INotifyPropertyChanged
    {
        
private string _strCustomerTypeName;
        
private int _intCustomerTypeId;
        
private IList  <Customer> _customers;
        [DataMember ]
        
public virtual  int CustomerTypeId
        {
            
get { return this._intCustomerTypeId; }
            
set
            {

                
this._intCustomerTypeId = value;
                OnPropertyChanged(
"CustomerTypeId");
            }
        }

        [DataMember]
        
public virtual string CustomerTypeName
        {
            
get { return this._strCustomerTypeName; }
            
set
            {
                
this._strCustomerTypeName = value; OnPropertyChanged("CustomerTypeName");
            }
        }
        [DataMember]
        
public virtual IList<Customer> Customers
        {
            
get { return this._customers; }
            
set
            {
                
this._customers = value;
                OnPropertyChanged(
"Customers");
            }
        }
        
#region INotifyPropertyChanged Members

        
public event PropertyChangedEventHandler PropertyChanged;

        
#endregion
        
private void OnPropertyChanged(string propertyName)
        {
            
if (PropertyChanged != null)
            {
                PropertyChanged(
thisnew PropertyChangedEventArgs(propertyName));
            }
        }
        
public override string ToString()
        {
            
return CustomerTypeName;
        }
    }
}
  

 

  请注意上面的这个红色部分IList,本来我是想返回集合的,一想针对接口编程,然后就写了一个IList,可是问题就出来了,原来好好的东西,就跑不了了,一个劲的报错。

  在SL3客户端的代码

  

void client_GetCustomerCompleted(object sender, GetCustomerCompletedEventArgs e)
        {
            LayoutRoot.DataContext 
= e.Result;
        }

 

  报错,就在e.Result,报错wcf system.servicemodel.communicationexception  我就开始找啊找,google啊google,很多人提出这个错误,就是没有回答。后来我在这篇请教当返回自定义类型时如何通过<declaredTypes>实现序列化帖子中找到了一些提示信息,他说“查了一些帮助,发现是由于返回类型中有个IList,所以无法序列化”,恍然大悟,还是WCF版的版主Frank Xu Lei厉害啊,哈哈。

  修改成List就可以了,问题解决。