0

On WCF Web API Preview 6, inside one of my services I used the object which directly comes from EF 4.2 and I got the following error:

Cannot serialize member DataAccess.SqlServer.Resort.AccommProperties of type System.Collections.Generic.ICollection`1[[DataAccess.SqlServer.AccommProperty, DataAccess, Version=1.0.4350.30311, Culture=neutral, PublicKeyToken=null]] because it is an interface.

This is the code of my service:

   [WebGet]
    public HttpResponseMessage<IQueryable<DataAccess.SqlServer.Resort>> GetAll() {

        var resorts = _resortRepo.GetAll(
                ApprovalStatus.Approved
        );

        var resortsResponse =
            new HttpResponseMessage<IQueryable<DataAccess.SqlServer.Resort>>(resorts);
        resortsResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddHours(6));
        return resortsResponse;

    }

The below is the Resort class which I am trying to expose above:

  public partial class Resort
    {
        public Resort()
        {
            this.AccommProperties = new HashSet<AccommProperty>();
            this.ResortDetails = new HashSet<ResortDetail>();
        }

        public int ResortID { get; set; }
        public int DestinationID { get; set; }
        public System.Guid ResortGUID { get; set; }
        public Nullable<int> SecondaryID { get; set; }
        public string ResortName { get; set; }
        public Nullable<bool> IsApproved { get; set; }
        public string ResortType { get; set; }

        public virtual ICollection<AccommProperty> AccommProperties { get; set; }
        public virtual Destination Destination { get; set; }
        public virtual ICollection<ResortDetail> ResortDetails { get; set; }
    }

Like the error message says, I can't serialize an interface. But what am I supposed to do all of my EF objects?

Community
  • 1
  • 1
tugberk
  • 57,477
  • 67
  • 243
  • 335

1 Answers1

0

You can use the JSON.NET Formatter from Web API Contrib and use the TypeNameHandling settings of JSON.NET to use your concrete types instead of interfaces.

Update: You could also try SharpSerializer.

Community
  • 1
  • 1
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • I am usually wrapping the EF objects and return them like that. But in some cases it is unnecessary. What if I would like to support OData with EF? It doesn't work either if I return `resorts.AsQueryable();` – tugberk Nov 30 '11 at 11:20
  • As for your suggestion, the problem is not with the `Resort` object directly. In fact, your suggestion does not make sense: `HttpResponseMessage>(resorts.ToList())`. We can do this: `HttpResponseMessage>(resorts.ToList())` but it throws the same error because `Resort` class has properties which are type of `ICollection` – tugberk Nov 30 '11 at 11:26
  • yes, actually I'm supporting both formats for now (json and xml). – tugberk Nov 30 '11 at 12:06
  • As for JSON (by using JSON.NET) you'll have to handle the IEnumerable(etc.) to XML serialization by yourself or maybe DataContractSerializer solves this problem. – Alexander Zeitler Nov 30 '11 at 12:39
  • Either there is a way which we are not aware of or they will support it in the near feature. considering WCF Web API will be merged with ASP.NET MVC, it has to be easy to return EF objects. otherwise, it will be a pain. – tugberk Nov 30 '11 at 13:21