1

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; }
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
user2998990
  • 970
  • 5
  • 18
  • 36
  • It seems that your web browser can't find a web server listening localhost:5127. Make sure it's running. (Hit F5 or Ctrl+F5) in VS.) – abatishchev Dec 08 '14 at 05:41
  • @abatishchev : No, when i write an ADo.net code for fetching the same data, it works just fine. – user2998990 Dec 08 '14 at 05:48
  • The way how you fetch data isn't related to the web server availability. Double check it's running. For example, put a breakpoint and make sure it's being hit. – abatishchev Dec 08 '14 at 05:50
  • Also you may get an exception that kills the web server process, it's quite possible. – abatishchev Dec 08 '14 at 05:50
  • Anyway, debug your application. Find the line causing an exception, if any. – abatishchev Dec 08 '14 at 05:51
  • @abatishchev : I debugged it. Debugger is getting visited twice which should visit only once. That is also something strange – user2998990 Dec 08 '14 at 05:52
  • @abatishchev : Not helping , did it many times. – user2998990 Dec 08 '14 at 06:17
  • What is the stack trace when the breakpoint is being hit, is it equal both times or different? – abatishchev Dec 08 '14 at 06:59
  • @abatishchev : I am getting exception as System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 – user2998990 Dec 08 '14 at 07:00
  • @user2998990 can you post full stack trace for the exception – Mahesh Dec 08 '14 at 08:45
  • Please look at this question which might answer your question http://stackoverflow.com/questions/8794594/type-not-expected-using-datacontractserializer-but-its-just-a-simple-class – Mahesh Dec 08 '14 at 08:50
  • @CoderofCode : I will try.. but I am a new bird with WCF. If you can please explain me the problem. Thanks – user2998990 Dec 08 '14 at 09:06

2 Answers2

0

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 ?

tchrikch
  • 2,428
  • 1
  • 23
  • 43
  • I had added it to course class. Do i need to add it to course class or student class.? – user2998990 Dec 08 '14 at 09:35
  • @user2998990 You need to Add `[DataContact]` and `[DataMember]` to student class also or else need to add the `[KnownType(typeof(student)]` attribute – Mahesh Dec 08 '14 at 09:47
  • @CoderofCode : [KnownType(typeof(student)] on student class or course.? – user2998990 Dec 08 '14 at 10:06
  • @CoderofCode,tchrikch : I have not written the classes. The classes are auto generated when I added the entity datamodel(edmx). Please help. Nothing is working. – user2998990 Dec 08 '14 at 11:54
0

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

akothalawala
  • 119
  • 8
  • Ado.net doent have a problem as it is not interacting with classes..? Rite..?? Coz using ado.net gives me proper results. – user2998990 Dec 08 '14 at 09:47
  • WCF uses serialization to transfer data from server to client. but Ado.net doesn't. the WCF and ADo.net are totally different concepts. Ado.net uses to get data from DB and WCF uses to transfer data from one end to another. – akothalawala Dec 08 '14 at 09:55
  • I know.. what i am saying is.. in the WCF service itself instead of using EF and LINQ , I am using ado.net code(STored Procedures) for fetching data and storing data returned by SP in dataset and converting it to list and then returning. In this case everythin works fine – user2998990 Dec 08 '14 at 10:09