nicomp
nicomp

Reputation: 4647

Can't return an object from my .Net Web Service

Here is the web service. It builds and launches and I can browse to it. The ValidateCoupon method works because it returns an int.

The problem is with the GetCouponInfo method. It should return an object of type Coupon but the XML it generates looks like this:

<Coupon/>

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

/// <summary>
/// Web Services for Coupon Processing in SimGrocery
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 CouponService : System.Web.Services.WebService {

    /// <summary>
    /// Summary description for Coupon
    /// </summary>
    public class Coupon {
        private List<CouponDetail> mCouponDetails;
        private String mCoupon;
        private String mDescription;
        private String mCouponSource;
        private DateTime mStartDate, mThroughDate;

        // A Parameterless constructor is required for an object to be serialized.
        public Coupon() { }

        public Coupon(String coupon, String description, String couponSource, DateTime startDate, DateTime throughDate) {
            List<CouponDetail> mCouponDetails = new List<CouponDetail>();
            mCoupon = coupon;
            mDescription = description;
            mCouponSource = couponSource;
            mStartDate = startDate;
            mThroughDate = throughDate;
        }
        public String coupon { get { return mCoupon; } }
        public String description { get { return mDescription; } }
        public String couponSource { get { return mCouponSource; } }
        public DateTime startDate { get { return mStartDate; } }
        public DateTime throughDate { get { return mThroughDate; } }

        public List<CouponDetail> couponDetails {
            get { return mCouponDetails; }
            set { mCouponDetails = value; }
        }

        public void addCouponDetail(CouponDetail couponDetail) {
            mCouponDetails.Add(couponDetail);
        }

    }

    public class CouponDetail {
        String mProduct;
        double mAmountOff;
        int mPercentageDiscount;
        String mDiscountType;

        // A Parameterless constructor is required for an object to be serialized.
        public CouponDetail() { }

        public CouponDetail(String product, double amountOff, int percentageDiscount, String discountType) {
            mProduct = product;
            mAmountOff = amountOff;
            mPercentageDiscount = percentageDiscount;
            mDiscountType = discountType;
        }
        public String product { get { return mProduct; } }
        public double amountOff { get { return mAmountOff; } }
        public int percentageDiscount { get { return mPercentageDiscount; } }
        public String discountType { get { return mDiscountType; } }

    }

    public CouponService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public int ValidateCoupon(String coupon) {
        int couponID = 0;
        // (String pTarget, String pDomain, String pCriteria, String pAggregate)
        return couponID;
    }
    [WebMethod]
    public Coupon GetCouponInfo(int CouponID) {
        // ToDo: Write this
        // Use fGetCouponInfo table-valued function in SQL Server
        Coupon coupon = new Coupon("XXXXX","Test Coupon","Penny Saver", Convert.ToDateTime("1/1/2016"), Convert.ToDateTime("12/31/2016"));

        return coupon;
    }
}

Upvotes: 1

Views: 510

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 23200

You are getting nothing because all your properties except couponDetails into Coupon class are readonly.

Quote from this link :

If a Web service contains a Web method that either accepts a parameter or returns a value that is an object reference, and the class definition of the object contains a read-only property, the read-only property is not available when you build a proxy assembly for the Web service.

The workaround, if you persist to have a readonly properties, is to implement the setter by throwing an NotImplementedException

like this one :

public DateTime throughDate 
{ 
    get { return mThroughDate; } 
    set { throw new NotImplementedException("Cannot set read-only property 'throughDate '"); }
}

Upvotes: 4

Related Questions