0

I am trying to learn web services. My problem is I have created a class "Item" as follows,

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

and another class for calculating the tax:

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

    }

}

}

I am trying to pass the values to the web service and let the web service call the classes.

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;

    }
}
}

I am giving the input like this, input image:

and after invoking I am just getting this: Output image

I am not getting the document tree of what I passed. I have seen a useful post here https://stackoverflow.com/a/12039010/3768995 But I couldn't solve my problem. Please guide me through this.

Community
  • 1
  • 1
Techno
  • 142
  • 4
  • 23

1 Answers1

0

Add a [DataContract] attribute to ShoppingBasket and a [DataMember] attribute to the properties that need to be included when the response is sent. (And as was said, make them public properties.)

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62