简体   繁体   English

如何将所有对象属性传递给WCF服务?

[英]how to pass all object properties to a WCF service?

I must admit my grasp of the more complex abilities of WCF confound me, so this may be a dumb question. 我必须承认我对WCF更复杂的功能的掌握使我感到困惑,因此这可能是一个愚蠢的问题。 I have a set of classes like so: 我有一组这样的类:

[DataContract]
public class InvoiceItem
{
    #region Properties
    public bool Submittable { get; set; }
    public string InvoiceID { get; set; }
    public string VendorId { get; set; }
    public string BatchId { get; set; }
    /// <summary>
    /// Marked as "Not Used", but explicitly set to 176.
    /// </summary>
    [ForFutureUse("Not used at this time", "176")]
    public string LocationCode { get; set; }
    public string VendorDocNumber { get; set; }
    public string Description { get; set; }
    public DateTime DocumentDate { get; set; }
    public decimal PurchaseInvoiceAmount { get; set; }
    public decimal TaxForm1099Amount { get; set; }
    [ForFutureUse("Not used at this time")]
    public string PayNumber { get; set; }
    /// <summary>
    /// Not marked as "Not Used", but not used.
    /// </summary>
    public string PaymentTerms { get; set; }
    [ForFutureUse("Not used at this time")]
    public decimal PurchaseAmount { get; set; }
    [ForFutureUse("Not used at this time")]
    public DateTime PayDate { get; set; }
    [ForFutureUse("Not used at this time")]
    public string DocumentID { get; set; }
    public string DocumentType { get; set; }
    public IList<InvoiceDetail> InvoiceDetails { get; set; }
    /// <summary>
    /// If the invoice is marked as not submittable, this should be set to explain why.
    /// </summary>
    public TroubleClasses TroubleClass { get; set; }
    #endregion

    #region Constructors
    public InvoiceItem() { }
    public InvoiceItem(XmlNode invoiceNode)
    {
        //collect the invoice data
        this.InvoiceID = invoiceNode.SelectSingleNode("INVOICEHEADER/VOUCHERID").InnerText.Trim();
        this.VendorId = invoiceNode.SelectSingleNode("INVOICEHEADER/SUPPLIER.CODE").InnerText.Trim();
        this.BatchId = string.Format("D{0}", DateTime.UtcNow.ToShortDateString());
        this.LocationCode = "176";
        this.VendorDocNumber = invoiceNode.SelectSingleNode("INVOICEHEADER/SUPPLIERREF").InnerText.Trim();
        this.Description = invoiceNode.SelectSingleNode("INVOICEHEADER/ARRIVAL").InnerText.TrimEnd() + " " + invoiceNode.SelectSingleNode("ACCOUNTLINES/ACCOUNTLINE/DIMENSION.D2.CODE").InnerText.TrimEnd();
        this.DocumentDate = DateTime.ParseExact(
            invoiceNode.SelectSingleNode("INVOICEHEADER/INVOICEDATE").InnerText.Trim(),
            "YYYYMMdd", CultureInfo.InvariantCulture);
        this.PurchaseInvoiceAmount = Math.Abs(decimal.Parse(invoiceNode.SelectSingleNode("INVOICEHEADER/AMOUNT").InnerText.Trim()));
        this.TaxForm1099Amount = decimal.Parse(invoiceNode.SelectSingleNode("INVOICEHEADER/SUPPLIER.CODE").InnerText.Trim());
        this.PayNumber = string.Empty;
        this.PaymentTerms = string.Empty;
        this.PurchaseAmount = 0.0M;
        this.PayDate = DateTime.MinValue;
        this.DocumentID = string.Empty;
        this.DocumentType = invoiceNode.SelectSingleNode("INVOICEHEADER/CATEGORY").InnerText.Trim();

        //collect the detail data
        this.InvoiceDetails = new List<InvoiceDetail>();
        var rowNumber = 0;
        foreach (XmlNode detail in invoiceNode.SelectNodes("ACCOUNTLINES/ACCOUNTLINE"))
        {
            var newDetail = new InvoiceDetail(detail, this);
            newDetail.RowNumber = ++rowNumber;
            this.InvoiceDetails.Add(newDetail);
        }
        //all done!
    }
    #endregion
}

[DataContract]
public class InvoiceDetail
{
    #region Properties
    public string Account { get; set; }
    /// <summary>
    /// This number must be unique and sequential in a given invoice.
    /// </summary>
    public int RowNumber { get; set; }
    /// <summary>
    /// Always set to "6".
    /// </summary>
    public int PayType { get; set; }
    public decimal Debit { get; set; }
    public decimal Credit { get; set; }
    #endregion

    #region Constructors
    public InvoiceDetail() { }
    public InvoiceDetail(XmlNode line, InvoiceItem invoiceItem)
    {
        var amount = Math.Abs(decimal.Parse(line.SelectSingleNode("AMOUNT").InnerText.Trim()));
        this.Account = string.Format("{0}-{1}-{2}-{3}-{4}",
            line.SelectSingleNode("ACCOUNTLINK.CODE").InnerText.TrimEnd(),
            line.SelectSingleNode("DIMENSION.D1.CODE").InnerText.TrimEnd(),
            line.SelectSingleNode("DIMENSION.D2.CODE").InnerText.TrimEnd(),
            line.SelectSingleNode("DIMENSION.D3.CODE").InnerText.TrimEnd(),
            line.SelectSingleNode("DIMENSION.D4.CODE").InnerText.TrimEnd());
        switch (invoiceItem.DocumentType)
        {
            case "2":
                this.Credit = amount;
                this.Debit = 0M;
                break;
            default:
                this.Credit = 0M;
                this.Debit = amount;
                break;
        }
        this.PayType = 6; //no idea what this is.
    }
    #endregion
}

I would like to transmit this InvoiceItem object via WCF web service. 我想通过WCF Web服务传输此InvoiceItem对象。 I would expect to do something like this at the client: 我希望在客户端执行以下操作:

var invoice = new InvoiceItem(someXmlNode);
using (var WebService = new WCFWebServices.WCFWebService())
{
    WebService.SubmitPayableTransaction(invoice);
}

Likewise I would expect to have something like this at the service: 同样,我希望在服务中有这样的东西:

public class WCFWebService:IWCFWebService
{
    public void SubmitPayableTransaction(InvoiceItem invoice)
    {
        ...
    }
}

Clearly this is not the case. 显然不是这样。 Even if I move the InvoiceItem class into its own library and both projects reference it, the client has a very different looking object as part of its web service definition, and a different namespace too. 即使我将InvoiceItem类移到其自己的库中,并且两个项目都引用了它,客户端作为其Web服务定义的一部分,对象的外观也非常不同,并且命名空间也不同。

How would I do this? 我该怎么做?

You need to add the DataMember attribute to all of the data items of the object you want to pass across to the client 您需要将DataMember属性添加到要传递给客户端的对象的所有数据项中

Remember though you are not persisting objects per se but defining the data to be passed between service and client 请记住,虽然您本身并不是持久化对象,但是定义了要在服务和客户端之间传递的数据

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

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