简体   繁体   English

发送到Web服务的对象包含“空”属性

[英]Object Sent to Web Service Contains “null” Property

It is possible to send an instance of a class to a webmethod on a webserver isn't it. 可以将类的实例发送到Web服务器上的Web方法吗? (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)?? 我问是因为我有一个尝试将其发送回Web服务器的已填充的类实例,但是当实例到达Web方法时,它已被清空(已创建一个新实例)?

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

If you are using [WebMethod] , then typically XmlSerializer is being used behind the scenes. 如果使用[WebMethod] ,则通常在幕后使用XmlSerializer It looks like the biggest problem is your empty set on pickingDetail . 看来最大的问题是您在pickingDetail上的空set Since XmlSerializer demands a set on lists/arrays, I'd probably keep it simple and use: 由于XmlSerializer要求对列表/数组进行set ,因此我可能会使其简单并使用:

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

(and maybe initialize it in the parameterless ctor to make usage simple) (并可能在无参数ctor中对其进行初始化以简化用法)

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: 对于信息,我倾向于在C#中使用.NET命名约定,而在xml中使用xml约定-例如:

[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. 有关信息, DataContractSerializer (默认情况下在WCF服务中使用)不需要设置器。 You could also switch to WCF... 您也可以切换到WCF ...

You need to provide a setter for pickingDetail , otherwise how can the class be filled once is deserialized? 您需要为pickingDetail提供一个setter,否则反序列化后如何填充该类?

Try 尝试

set {
  pD.Clear();
  foreach (PickingDetails p in value)
    addPickingDetail(p); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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