我必須承認我對WCF更復雜的能力的把握讓我感到困惑,所以這可能是一個愚蠢的問題。我有一組類,像這樣:如何將所有對象屬性傳遞給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服務定義的一部分,並且也有一個不同的名稱空間。
我該怎麼做?
我想你應該爲每個你想要包含在你的數據合同(web服務使用的每個字段)中的字段添加[DataMember]屬性。看看這個鏈接:http://msdn.microsoft.com/en-us/library/aa480190.aspx#introt_topic4。 – 2012-01-31 20:27:24