【问题标题】:Can't return an object from my .Net Web Service无法从我的 .Net Web 服务返回对象
【发布时间】:2016-02-05 21:55:18
【问题描述】:

这里是网络服务。它构建并启动,我可以浏览它。 ValidateCoupon 方法有效,因为它返回一个 int。

问题出在 GetCouponInfo 方法上。它应该返回一个 Coupon 类型的对象,但它生成的 XML 如下所示:

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

【问题讨论】:

  • 通常序列化会忽略 R/O 字段/属性...
  • @AlexeiLevenkov 你是说让他们公开吗?
  • @nicomp 不,他是说让他们获取/设置。请参阅相关问题。
  • @AlexeiLevenkov Bingo!发表答案,我会投票给你。然后我将按照@D Stanley 的说明让它们获取/设置。

标签: c# .net asmx


【解决方案1】:

您什么也得不到,因为除了 couponDetails 进入 Coupon 类之外,您的所有属性都是只读的。

引用此link

如果 Web 服务包含接受参数或返回作为对象引用的值的 Web 方法,并且对象的类定义包含只读属性,则当您使用只读属性时,该只读属性不可用为 Web 服务构建代理程序集。

如果您坚持使用只读属性,解决方法是通过抛出 NotImplementedException 来实现设置器

喜欢这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多