2012-01-31 4 views
0

私はWCFのより複雑な能力の私の把握は私を混乱させることを認めなければならないので、これは愚かな質問かもしれません。私は、WCFのWebサービスを介してこのInvoiceItemオブジェクトを送信したいすべてのオブジェクトプロパティを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 
} 

:私はそうのようなクラスのセットを持っています。私はクライアント側でこのような何かを期待する:

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サービス定義の一部として全く異なる見た目のオブジェクトを持ち、異なる名前空間も持っています。

どうすればよいですか?あなたは、それ自体のオブジェクトを永続化するが、サービス間で渡されるデータを定義していないのにあなたがクライアント

に渡って渡したいオブジェクトのデータ項目のすべてにデータメンバーの属性を追加する必要が

+2

私はあなたのデータコントラクト(Webサービスで使用されるすべてのフィールド)に含める各フィールドに[DataMember]属性を追加することをお勧めします。このリンクを見てください:http://msdn.microsoft.com/en-us/library/aa480190.aspx#introt_topic4。 –

答えて

3

は覚えておいてくださいとクライアント

+0

私はそのような単純なことは問題ではないと思っていませんでしたが、問題を修正したようです。 –

+0

クラスが公開されている場合、.NET 3.5 SP1以降を使用していると仮定すると、公開されているデフォルトのctorを持ち、 '[DataContract]'属性を省略すると、それはうまく機能します。 '[DataContract]'を使用する場合は、 '[DataMember]'も使用する必要があります - 属性を使用すると、直列化をより詳細に制御することができます。 –

関連する問題