1

在返回 Activity 对象数组时,我得到下面显示的错误。无法理解哪里出了问题。任何人都可以帮我解决这个问题。

这是错误

预期来自命名空间“http://schemas.datacontract.org/2004/07/BusinessEntities”的结束元素“ActivityTypeId”。从命名空间“http://schemas.datacontract.org/2004/07/BusinessEntities”中找到元素“a:Code”。第 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();

    }

WCF服务代码

 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; }


}
4

1 回答 1

0

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

希望这会有所帮助

于 2012-04-30T20:20:09.790 回答