简体   繁体   中英

C# webservice losing data on return

I am programming a client program that calls a webmethod but when I get the return data there are missing values on some of the fields and objects.

The webmethod in turn is calling a WCF method and in the WCF method the return data is fine. But when it is passing to the webservice the return data is missing.

Is there any way to fix this problem?


This is my client code calling the 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);
    }

The webmethod code is here:

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

And at last the WCF code:

    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().");
        }
    }

The 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().");
        }
    }

In all this code the return data is fine. It is loosing data only when passing to the webmetod.

Enter code here.

Make surethe XML tags are being accessed with the same casing at either end. if the casing is not the same then the value won't be read.

另外,消息中XML标记的顺序也有所不同-大约两年前,我遇到了类似的问题,在这种情况下,参数值在传输过程中消失了 ,因为发送方对标记的排序方式与架构中定义的方式不同。

You should check it all message are sending back from your webservice. Call your webservice manually and check its response.

  • If all data is there, probably your webservice reference is outdated; update it by right-clicking your webservice reference and choose "Update"
  • If your data don't came back, your problem is probably related to webservice code. You should check your serialization code (if any) again, and make sure all returned types are [Serializable] . You should check if all return types are public as it's mandatory for serialization.

As noted per John Saunders, [Serializable] isn't used by XmlSerializer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM