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

本文关键字:WCF 服务 属性 对象 | 更新日期: 2023-09-27 18:22:06

我必须承认,我对WCF更复杂能力的理解让我感到困惑,所以这可能是一个愚蠢的问题。我有一组这样的类:

[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
}

我想通过WCF web服务传输此InvoiceItem对象。我希望在客户端做这样的事情:

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

同样,我也希望在服务中有这样的东西:

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

显然情况并非如此。即使我将InvoiceItem类移到它自己的库中,并且两个项目都引用它,作为其web服务定义的一部分,客户端也有一个外观非常不同的对象,还有一个不同的命名空间。

我该怎么做?

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

您需要将DataMember属性添加到要传递给客户端的对象的所有数据项中

请记住,尽管您不是在持久化对象本身,而是在定义要在服务和客户端之间传递的数据