user610217
user610217

Reputation:

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. 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. 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.

How would I do this?

Upvotes: 0

Views: 818

Answers (1)

Richard Blewett
Richard Blewett

Reputation: 6109

You need to add the DataMember attribute to all of the data items of the object you want to pass across to the client

Remember though you are not persisting objects per se but defining the data to be passed between service and client

Upvotes: 3

Related Questions