c# - Can't return an object from my .Net Web Service -
here web service. builds , launches , can browse it. validatecoupon method works because returns int.
the problem getcouponinfo method. should return object of type coupon xml generates looks this:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using confignamespace; using utilsnamespace; /// <summary> /// web services coupon processing in simgrocery /// </summary> [webservice(namespace = "http://tempuri.org/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] // allow web service called script, using asp.net ajax, uncomment following line. // [system.web.script.services.scriptservice] public class couponservice : system.web.services.webservice { /// <summary> /// summary description coupon /// </summary> public class coupon { private list<coupondetail> mcoupondetails; private string mcoupon; private string mdescription; private string mcouponsource; private datetime mstartdate, mthroughdate; // parameterless constructor required object 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 { { return mcoupon; } } public string description { { return mdescription; } } public string couponsource { { return mcouponsource; } } public datetime startdate { { return mstartdate; } } public datetime throughdate { { return mthroughdate; } } public list<coupondetail> coupondetails { { return mcoupondetails; } set { mcoupondetails = value; } } public void addcoupondetail(coupondetail coupondetail) { mcoupondetails.add(coupondetail); } } public class coupondetail { string mproduct; double mamountoff; int mpercentagediscount; string mdiscounttype; // parameterless constructor required object serialized. public coupondetail() { } public coupondetail(string product, double amountoff, int percentagediscount, string discounttype) { mproduct = product; mamountoff = amountoff; mpercentagediscount = percentagediscount; mdiscounttype = discounttype; } public string product { { return mproduct; } } public double amountoff { { return mamountoff; } } public int percentagediscount { { return mpercentagediscount; } } public string discounttype { { return mdiscounttype; } } } public couponservice () { //uncomment 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 // 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; } }
you getting nothing because properties except coupondetails
coupon class readonly.
quote link :
if web service contains web method either accepts parameter or returns value object reference, , class definition of object contains read-only property, read-only property not available when build proxy assembly web service.
the workaround, if persist have readonly properties, implement setter throwing notimplementedexception
like 1 :
public datetime throughdate { { return mthroughdate; } set { throw new notimplementedexception("cannot set read-only property 'throughdate '"); } }
Comments
Post a Comment