简体   繁体   English

WCF REST 服务 - 无法返回对象列表

[英]WCF REST Service - Unable to return list of objects

I created a WCF Web Service application which exposes a REST service.我创建了一个 WCF Web 服务应用程序,它公开了一个 REST 服务。 I would like this service to return a generic response with a Data and Status section (see HttpResponse class below).我希望此服务返回带有数据和状态部分的通用响应(请参阅下面的 HttpResponse 类)。 The HttpResponse has Data member which is a List of objects. HttpResponse 具有 Data 成员,它是一个对象列表。 Depending which REST service call is executed, I will populate Data with a list of different types of objects.根据执行的 REST 服务调用,我将使用不同类型对象的列表填充 Data。

The problem I have is that if the Data member is a list of objects, the web service crashes.我遇到的问题是,如果 Data 成员是一个对象列表,Web 服务就会崩溃。 If it make it a specific class, I get a valid response.如果它使它成为一个特定的类,我会得到一个有效的响应。

How can I solve this issue?我该如何解决这个问题?

Thanks!谢谢!

  public class HttpStatus
    {
        public int StatusCode = (int)StatusCodeEnum.SUCCESS;
        public string UserMessage = "";
        public string InternalMessage = "";
        public string LinkedList = "";

    }

    public class HttpResponse
    {
        private List<object> _data { set; get; }
        private HttpStatus _status { set; get; }

        public HttpResponse()
        {
            _data = null;
            _status = new HttpStatus();
        }

        public List<object> Data
        {
            get
            {
                return _data;
            }
            set
            {
                if (value != null) _data = value;
            }
        }

        public HttpStatus Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (value != null) _status = value;
            }
        }
    }

        [WebInvoke(
            Method = "GET",
            UriTemplate = "MyEntity",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        [OperationContract]
        HttpResponse GetAllEntities();



        public HttpResponse GetAllEntities()
        {
            // Create Response
            HttpResponse response = new HttpResponse();

            try{
                // Set the data
                response.Data = GetMyEntities();
                response.Status = 1;
            }
            catch(Exception ex)
            {
                Debug.WriteLine("Exception: "+ex.Message);
                response.Status.StatusCode = (int)StatusCodeEnum.FAIL;
                response.Status.InternalMessage = ex.Message;
                response.Status.UserMessage = Constants.GENERIC_USER_ERROR;
            }
            return response;
        }

You could use你可以用

public class HttpResponse<T> where T : class
{
    private List<T> _data { set; get; }
    private HttpStatus _status { set; get; }

    public HttpResponse()
    {
        _data = null;
        _status = new HttpStatus();
    }

    public List<T> Data
    {
        get
        {
            return _data;
        }
        set
        {
            if (value != null) _data = value;
        }
    }

    public HttpStatus Status
    {
        get
        {
            return _status;
        }
        set
        {
            if (value != null) _status = value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM