C#webservice在返回时丢失数据

时间:2009-12-14 08:31:00

标签: c# wcf web-services asmx

我正在编写一个调用web方法的客户端程序,但是当我得到返回数据时,某些字段和对象上缺少值。

webmethod反过来调用WCF方法,在WCF方法中返回数据很好。但是当它传递给webservice时,返回的数据就丢失了。

有没有办法解决这个问题?


这是我调用webservice的客户端代码:

    ReLocationDoc query = new ReLocationDoc();

    query.PerformerSiteId = 1;
    query.PerformerUserId = 1;
    query.FromStatus = 10;
    query.ToStatus = 200;

    ReLocationDoc doc = new ReLocationDoc();

    ServiceReference1.QPSoapClient service = new QPSoapClient();
    try {
        service.GetRelocationAssignment(query, out doc);

        string test = doc.Assignment.Id.ToString();

    } catch(Exception ex) {

        MessageBox.Show(ex.Message);
    }

网络方法代码在这里:

        [WebMethod]
        return m_reLocationClient.GetRelocationAssignment(query, out reLocationDoc);
    }

最后是WCF代码:

    public ReLocationResult GetRelocationAssignment(ReLocationDoc query, out ReLocationDoc reLocationDoc) {
        try {
            LOGGER.Trace("Enter GetRelocationAssignment().");

            ReLocationResult result = reLocationCompactServiceClient.GetRelocationAssignment(out reLocationDoc, query);

            if(reLocationDoc.Assignment == null || reLocationDoc.Assignment.CurrentStatus == STATUS_FINISHED) {
                ReLocationDoc newQuery = new ReLocationDoc();
                newQuery.Assignment = new AssignmentDoc();
                newQuery.Assignment.EAN = DateTime.Today.ToString();
                newQuery.PerformerSiteId = QPSITE;
                newQuery.PerformerUserId = QPUSER;
                reLocationDoc.AssignmentStatus = m_settings.ReadyStatus; ;
                result = reLocationCompactServiceClient.CreateReLocationAssignment(out reLocationDoc, newQuery);
            }

            return result;

        } finally {
            LOGGER.Trace("Exit GetRelocationAssignment().");
        }
    }

GetRelocationAssignment:

    public ReLocationResult GetRelocationAssignment(ReLocationDoc query, out ReLocationDoc reLocationDoc) {
        try {
            LOGGER.Trace("Enter GetRelocationAssignment().");

            ReLocationDoc doc = new ReLocationDoc();
            ReLocationResult result = new ReLocationResult();

            new Database(Connection).Execute(delegate(DBDataContext db) {

                User user = GetVerifiedUser(db, query, MODULE_ID);
                SiteModule siteModule = SiteModule.Get(db, query.PerformerSiteId, MODULE_ID);

                Status status = Status.Get(db, query.FromStatus, query.ToStatus, 0);
                Status startStatus = Status.Get(db, query.FromStatus, 0);
                Status endStatus = Status.Get(db, query.ToStatus, 0);

                IQueryable<Assignment> assignments = Assignment.GetAssignmentsWithEndStatus(db, siteModule, endStatus);
                assignments = Assignment.FilterAssignmentStartStatus(assignments, startStatus);

                foreach(Assignment assignment in assignments) {

                    LOGGER.Debug("Handling assignment: " + assignment.Id);

                    result.Status = true;
                    AssignmentDoc assignmentDoc = FillAssignmentDoc(assignment);
                    //ReLocationDoc doc = new ReLocationDoc();

                    AssignmentStatus sts = assignment.AssignmentStatus.OrderByDescending(ass => ass.Id).First();
                    assignmentDoc.CurrentStatus = sts.Status.Zone;

                    Status currentStatus = sts.Status;

                    IList<Item> items = assignment.Items.ToList();
                    IList<ItemDoc> itemDocs = new List<ItemDoc>();
                    foreach(Item item in items) {

                        ItemDoc itemDoc = FillItemDoc(item);

                        ItemDetail itemDetail;
                        if(ItemDetail.TryGet(db, item.Id, out itemDetail)) {
                            ItemDetailDoc itemDetailDoc = FillItemDetailDoc(itemDetail);
                            itemDoc.Details = new ItemDetailDoc[1];


                            Event eEvent = null;
                            if(Event.GetEvent(db, itemDetail, currentStatus, out eEvent)) {
                                EventDoc eventDoc = FillEventDoc(eEvent);
                                itemDetailDoc.Events = new EventDoc[1];

                                if(eEvent.LocationId.HasValue) {
                                    Location location = null;
                                    if(Location.TryGet(db, eEvent.LocationId.Value, out location)) {
                                        eventDoc.Location = new LocationDoc();
                                        eventDoc.Location = FillLocationDoc(location, db);
                                    }
                                }
                                itemDetailDoc.Events[0] = eventDoc;
                            }
                            itemDoc.Details[0] = itemDetailDoc;
                        }
                        itemDocs.Add(itemDoc);
                    }
                    assignmentDoc.Items = itemDocs.ToArray();
                    doc.Assignment = assignmentDoc;
                }

            }, delegate(Exception e) {
                result.Message = e.Message;
            });

            reLocationDoc = doc;
            return result;

        } finally {
            LOGGER.Trace("Exit GetRelocationAssignment().");
        }
    }

在所有这些代码中,返回数据都很好。只有在传递给webmetod时才会丢失数据。

在此输入代码。

3 个答案:

答案 0 :(得分:1)

确保在任一端使用相同的外壳访问XML标记。如果外壳不相同则不会读取该值。

答案 1 :(得分:1)

此外,消息中XML标记的排序也有所不同 - 大概两年前我遇到了类似的问题,在这种情况下,参数值在传输过程中消失,因为发送部件已经订购标签与模式中定义的标签不同。

答案 2 :(得分:1)

您应该检查所有邮件是否从您的网络服务发回。手动调用您的Web服务并检查其响应。

  • 如果所有数据都存在,那么您的网络服务参考可能已过时;通过右键单击您的Web服务引用并选择“更新”
  • 来更新它
  • 如果您的数据没有回来,您的问题可能与网络服务代码有关。 您应该再次检查序列化代码(如果有),并确保所有返回的类型都是[Serializable] 。您应该检查所有返回类型是否为public,因为它是序列化必需的。

如John Saunders所述,[Serializable]未使用XmlSerializer