简体   繁体   English

如何从静态WCF服务返回对象列表

[英]How do I return a list of objects from a restful WCF service

I am trying to return a list of objects from my restful WCF service. 我正在尝试从我的静态WCF服务返回一个对象列表。 I am using Entity Framework. 我正在使用实体框架。 The problem is that when the WCF service runs and if I put a debugger on that then the debugger is visited twice and I get a 404 server not found error. 问题是,当运行WCF服务时,如果我在上面放置了调试器,则该调试器将被访问两次,并且出现404服务器未找到错误。 Why is this happening.? 为什么会这样呢?

If I return a return a single class(db table) which is not having relation with other class(DB table) then it is returning the list but if I have to return a class(DB table) then I get a 404 error. 如果我返回一个与其他class(DB table)没有关系的单个class(db table) ,则它返回列表,但是如果我必须返回一个class(DB table)则会收到404错误。

Interface : 介面

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetDataString/{value}",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Course> GetDataString(string value);

Implementation: 执行:

public List<Course> GetDataString(string value)
{
    int v = Convert.ToInt32(value);
    TestEntities1 t = new TestEntities1();
    List<Course> u = t.Courses.ToList();
    return u;
}

What can be the problem.? 可能是什么问题? Here Courses is lined to student table . 在这里, Courses排成student table

When i write an ADO.NET code for fetching the same data, it works just fine. 当我编写用于获取相同数据的ADO.NET代码时,它工作得很好。

Then after the debugger has executed twice I get a server not found error page and the URL changes from 然后,在调试器执行两次后,我得到一个服务器未找到错误页面,URL从

http://localhost:5127

to

http://www.localhost.com:5127/Service1.svc/GetDataString/1/Service1.svc/GetDataString/1

also I enabled the trace and I am getting the following exception. 我也启用了跟踪,并且遇到了以下异常。 I dont have any idea as to what this exception is. 我不知道这个例外是什么。 Please help. 请帮忙。

System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Message in stack trace for exception: 堆栈跟踪中的异常消息:

Type 'System.Data.Entity.DynamicProxies.Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11' with data contract name 'Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Following is my Course class: 以下是我的课程:

namespace JSONWebService
{
    [DataContract]
    public partial class course
    {
        public Course()
        {
            this.Students = new HashSet<Student>();
        }

        [DataMember]
        public int C_Id { get; set; }

        [DataMember]
        public string C_Name { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }
}

Your service doesn't know how to serialize course class. 您的服务不知道如何序列化course Did you add [DataContract] and [DataMember] attributes to it ? 您是否向其中添加了[DataContract][DataMember]属性? Can you post how it looks ? 你能发表它的样子吗?

By default, you cannot transfer an object as its base type between a WCF server and client. 默认情况下,您无法在WCF服务器和客户端之间传输对象作为其基本类型。 The DataContractSerializer will throw an exception when it attempts to serialize the base class, You can find good article regarding your problem in here DataContractSerializer尝试序列化基类时将引发异常,您可以在此处找到有关您的问题的好文章

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

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