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

故事扮演 提交于 2019-12-11 07:40:33

问题


I am trying to return a list of objects from my restful WCF service. 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. 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.

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.

When i write an ADO.NET code for fetching the same data, it works just fine.

Then after the debugger has executed twice I get a server not found error page and the URL changes from

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

回答1:


Your service doesn't know how to serialize course class. Did you add [DataContract] and [DataMember] attributes to it ? Can you post how it looks ?




回答2:


By default, you cannot transfer an object as its base type between a WCF server and client. The DataContractSerializer will throw an exception when it attempts to serialize the base class, You can find good article regarding your problem in here



来源:https://stackoverflow.com/questions/27351267/how-do-i-return-a-list-of-objects-from-a-restful-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!