0

WCF Web API Preview 6 で、サービスの 1 つで、EF 4.2 から直接取得したオブジェクトを使用したところ、次のエラーが発生しました。

System.Collections.Generic.ICollection`1[[DataAccess.SqlServer.AccommProperty, DataAccess, Version=1.0.4350.30311, Culture=neutral, PublicKeyToken=null]] 型のメンバー DataAccess.SqlServer.Resort.AccommProperties をシリアル化できません。インターフェース。

これは私のサービスのコードです:

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

    }

以下は、上で公開しようとしている Resort クラスです。

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

エラー メッセージにあるように、 I can't serialize an interface . しかし、すべての EF オブジェクトをどうすればよいのでしょうか?

4

1 に答える 1

0

Web APIContribのJSON.NETFormatterを使用し、JSON.NETのTypeNameHandling設定を使用して、インターフェイスの代わりに具象型を使用できます。

更新:SharpSerializerを試すこともできます。

于 2011-11-30T10:07:53.017 に答える