tadej
tadej

Reputation: 711

Using EF in WCF REST service - SerializationException

Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.Runtime.Serialization.dll

This is the error i got, when i try to get data with FK from DB using EF.

This is my Context class:

public class Context : DbContext
{

        public DbSet<TypeCar> TypeCarList { get; set; }

        public DbSet<Car> CarList{ get; set; }
}


    [DataContract]
    public class TypeCar
    {
        [DataMember]
        [Key]
        public int TypeCarId{ get; set; }
        [DataMember]
        public string typeName{ get; set; }

    }

    [DataContract]
    public class Car
    {
        [DataMember]
        [Key]
        public int CarId { get; set; }
        [DataMember]
        public string carName{ get; set; }
        [DataMember]
        public virtual TypeCar FK_Car{ get; set; }
    }

This is my IService interface:

public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "getCars",
      RequestFormat = WebMessageFormat.Json,
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Bare)]
    List<Car> getCars();

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "getTypeCars",
      RequestFormat = WebMessageFormat.Json,
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Bare)]
    List<TypeCar> getTypeCars();


}

And this is my Service1 class:

public class Service1 : IService1
{


    public List<Avtomobil> getCars()
    {
        using (var db = new Context())
        {
            return db.CarList.ToList();
        }
    }
    public List<TypeCar> getTypeCars()
    {
        using (var db = new Context())
        {
            return db.TypeCarList.ToList();
        }
    }
}

When i call REST method getTypeCars it works like it should. But when I call getCars method (with FK) it throws given exception (Serialization Exception). I tried the same with other entities, but always the same (exception when entity with FK is being called).

EDIT: And if i delete "virtual" word from attribute FK_TypeCar, the exception is not thrown, but the value of FK_TypeCar is null.

Upvotes: 0

Views: 117

Answers (1)

strickt01
strickt01

Reputation: 4048

@Rabban is correct - you should be using separate objects to return the data via EF and then convert these to your DataContract objects. Here's one of many reasons why:

Currently you have created your EF entities with a virtual property on Car for the TypeCar but no corresponding ICollection<Car> in TypeCar for the one-to-many relationship back to Car. So the relationship is not fully described. If you were to do so then the WCF DataContractJsonSerializer would throw a SerializationException because of a circular reference anyway. So in order to have EF entities that fully describe your underlying schema and enable you to pass objects back via WCF you will have to create separate DTOs.

Upvotes: 1

Related Questions