从WCF服务返回Object时反序列化错误

时间:2012-04-30 17:16:08

标签: wcf

我在返回Activity对象数组时遇到以下错误。无法理解出错的地方。任何人都可以帮我解决这个问题。

这是错误

  

命名空间中的结束元素'ActivityTypeId'   'http://schemas.datacontract.org/2004/07/BusinessEntities'预计。   从命名空间中找到元素'a:Code'   'http://schemas.datacontract.org/2004/07/BusinessEntities'。第1行,   450号位置。

UI相关代码:

  protected void Page_Load(object sender, EventArgs e)
    {
        TimeSheetManagementServiceClient serviceClient = new TimeSheetManagementServiceClient("WSHttpBinding_ITimeSheetManagementService");
        Activity[] activities=serviceClient.GetActivities();
        GridView1.DataSource = activities;
        GridView1.DataBind();

    }

WCFService代码

 public class TimeSheetManagementService:ITimeSheetManagementService 
    {
        public BusinessEntities.Activity[] GetActivities()
        {
            TimeSheetManagementDataController controller= new TimeSheetManagementDataController();
            var activities = controller.GetActivities().Select(activity => new BusinessEntities.Activity()
                                                                               {
                                                                                   Code = activity.Code,
                                                                                   Description = activity.Description,
                                                                                   Status =
                                                                                       (EntityStatus)
                                                                                       Enum.Parse(typeof(EntityStatus), ((activity.Status==true) ? 0 : 1).ToString()),
                                                                                   ActivityTypeId = new BusinessEntities.ActivityType()
                                                                                                        {
                                                                                                          Code=activity.ActivityType.Code,
                                                                                                          Description = activity.ActivityType.Description,
                                                                                                          Name = activity.ActivityType.Name
                                                                                                        }


                                                                               });
            return activities.ToArray();
        }

    }

服务合同

  [ServiceContract]
    interface ITimeSheetManagementService
    {
        [OperationContract]
        Activity[] GetActivities();
    }

数据合同

[DataContract]
public class Activity
{
    [DataMember]
    public string Code { get; set; }
    [DataMember]
    public string Description { get; set; }
    [DataMember]
    public EntityStatus Status { get; set; }
    [DataMember]
    public ActivityType  ActivityTypeId { get; set; }
}
[DataContract]
public enum EntityStatus
{
    [EnumMember]
    Active=0,
    [EnumMember]
    Inactive=1
}

[DataContract]
public class ActivityType
{
    [DataMember]
    public string Code { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Description { get; set; }


}

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为问题是您的Activity类的数据库的字母顺序。仅用于测试,请考虑在Datamember属性中指定order属性。 http://msdn.microsoft.com/en-us/library/ms729813%28v=vs.90%29.aspx

希望这会有所帮助