How do I return a list of objects from a restful W

2019-08-10 21: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; }
    }
}

2条回答
祖国的老花朵
2楼-- · 2019-08-10 22:06

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

查看更多
对你真心纯属浪费
3楼-- · 2019-08-10 22:29

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 ?

查看更多
登录 后发表回答