2016-04-14 4 views
0

Webサービスを習得しようとしています。私は、Webサービスに値を渡すとletしようとしていますWebサービスで複数の値を返す

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 


namespace WebServiceTaxCalc 
{ 
public class ShoppingBasket 
{ 
    double itemsTotalPrice = 0.0; 
    double totalTax = 0.0; 
    public void addItem(List<Item> i) 
    { 

     foreach (Item a in i) 
     { 
      totalTax += taxPerItem(a); 
      double eachItemPrice = (a.getPrice() + taxPerItem(a)); 
      itemsTotalPrice += eachItemPrice; 

      //Console.WriteLine(a.getQuantity() + " " + a.getName() + ": " + eachItemPrice); 
     } 

     //Console.WriteLine("sales tax: " + totalTax); 
     //Console.WriteLine("total cost: "+itemsTotalPrice); 
    } 

    public double taxPerItem(Item i) 
    { 
     double tax = 0; 
     if (i.getType() == Type.BASIC) 
     { 
      tax = i.getPrice() * 5/100; 
      return tax; 
     } 
     else if (i.getType() == Type.EXEMPT) 
     { 
      tax = 0; 
      return tax; 
     } 
     else 
     { 
      tax = i.getPrice() * 15/100; 
      return tax; 
     } 

    } 

} 

} 

:私の問題は、次のように私はクラス「アイテム」を作成しました

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WebServiceTaxCalc 
{ 
public enum Type { BASIC, IMPORTED, EXEMPT} 
public class Item 
{ 
    string name; 
    double basePrice; 
    int quantity; 
    Type TaxType; 
    public Item(string name, double price, int quantity, Type type) 
    { 
     this.basePrice = price; 
     this.name = name; 
     this.quantity = quantity; 
     this.TaxType = type; 
    } 
    public string getName() 
    { 
     return name; 
    } 
    public double getPrice() 
    { return basePrice; } 
    public int getQuantity() 
    { return quantity; } 
    public Type getType() { return TaxType; } 
} 
} 

と税を計算するための別のクラスでありますWebサービスがクラスを呼び出します。 input image:

と が起動した後、私はちょうどこれを取得しています私はこのような入力を与えている

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 


namespace WebServiceTaxCalc 
{ 
/// <summary> 
/// Summary description for Service1 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public ShoppingBasket Calc(string name, double price, int quantity, Type catg) 
    { 
     List<Item> l1 = new List<Item>(); 
     Item i1 = new Item(name, price, quantity,catg); 
     l1.Add(i1); 
     ShoppingBasket sb = new ShoppingBasket(); 
     sb.addItem(l1); 

     return sb; 

    } 
} 
} 

、: Output image

は私が渡されたかのドキュメントツリーを取得しておりません。 ここで便利な投稿を見ましたhttps://stackoverflow.com/a/12039010/3768995 しかし、私は自分の問題を解決できませんでした。 私にこれを案内してください。

+0

'ShoppingBasket'のフィールドを' public'プロパティに変換しようとしてください。 –

答えて

0

[DataContract]属性をShoppingBasketに追加し、[DataMember]属性を応答の送信時に含める必要があるプロパティに追加します。 (そして、言われたように、それらを公的なものにしてください)

関連する問題