views:

60

answers:

2

It is possible to send an instance of a class to a webmethod on a webserver isn't it. (The class is actually created on the webserver).

I ask because I have a populated class instance which I'm trying to send back to the webserver, however when the instance arrives at the webmethod, it has been emptied (a fresh instance has been created)??

Some code that may help explain:

    private void miIssue_Click(object sender, EventArgs e)
    {
        string r = sC.updatePickItem(batch, deptID, issued, pL);
    }

   //The creation of the webserver class I want to pass
    [Serializable]
    public class PickingList
    {
        List<PickingDetails> pD = new List<PickingDetails>();

        public bool found { get; set; }
        public PickingDetails[] pickingDetail
        {
            get { return pD.ToArray(); }
            set { }
        }

        public void addPickingDetail(PickingDetails pI)
        {
            pD.Add(pI);
        }
    }

    [Serializable]
    public class PickingDetails
    {
        public string warehouse { get; set; }
        public string stockCode { get; set; }
    }

    //The webmethod I'm connecting to
    [WebMethod]
    public string updatePickItem(string batch, string deptID, bool issued, PickingList pL )
    {
        try
        {
            if (!issued)
                issued = issueStockToWIP(rows, batch, deptID, pL);
        }
        catch (Exception e)
        { return e.ToString(); }
    }
+3  A: 

If you are using [WebMethod], then typically XmlSerializer is being used behind the scenes. It looks like the biggest problem is your empty set on pickingDetail. Since XmlSerializer demands a set on lists/arrays, I'd probably keep it simple and use:

[XmlElement("pickingDetail")
public List<PickingDetails> Details {get;set;}

(and maybe initialize it in the parameterless ctor to make usage simple)

public PickingList() {
    Details = new List<PickingDetails>();
}

For info, I tend to use .NET naming conventions in the C#, and xml conventions in the xml - for example:

[XmlElement("found")] // or XmlAttribute
public bool Found { get; set; }


For info, DataContractSerializer (used in WCF services by default) doesn't have this annoying need for a setter. You could also switch to WCF...

Marc Gravell
Thank you, this answer gave me some useful information
SumGuy
+1  A: 

You need to provide a setter for pickingDetail, otherwise how can the class be filled once is deserialized?

Try

set {
  pD.Clear();
  foreach (PickingDetails p in value)
    addPickingDetail(p); 
}
Sklivvz
Cheers, that snippet solved the problem. A very simple mistake, completely forgot to set. Been doing nothing but getting, this was the first set required
SumGuy