3

私のデスクトップ wpf アプリケーションは、mvc 4 Web API と通信します。すべてのデータベース エントリを読み取ろうとしています。これはシンプルなインターフェースです:

public interface IEventRepository
{
    IEnumerable<Event> GetAll();
}

そして、これはリポジトリです:

public class EventRepository : IEventRepository
{
    private List<Event> events = new List<Event>();
    public EventRepository()
    {
        HeronEntities context = new HeronEntities();
        events = context.Events.ToList();
    }

    public IEnumerable<Event> GetAll()
    {
        return events;
    }
 }

これはコントローラーです:

 public class EventController : ApiController
{
    static readonly IEventRepository repository = new EventRepository();

    public IEnumerable<Event> GetAllEvents()
    {
        return repository.GetAll();
    }
}

イベントクラスは次のようになります。

public partial class Event
{
    public Event()
    {
        this.Comments = new HashSet<Comment>();
        this.Rates = new HashSet<Rate>();
        this.RawDates = new HashSet<RawDate>();
    }

    public int ID { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public string SiteURL { get; set; }
    public string ContactEmail { get; set; }
    public string LogoURL { get; set; }
    public int EventType_ID { get; set; }
    public Nullable<int> Location_ID { get; set; }
    public Nullable<System.DateTime> BegginingDate { get; set; }
    public string nTrain { get; set; }
    public string Content { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual Conference Conference { get; set; }
    public virtual ICollection<Rate> Rates { get; set; }
    public virtual ICollection<RawDate> RawDates { get; set; }
    public virtual EventType EventType { get; set; }
    public virtual Location Location { get; set; }
}

コントローラーにアクセスしようとすると、上記failed to serialize the response body for content typeのエラーが発生します。Eventクラスのシリアル化に問題があります。プリミティブ型を含むクラスでまったく同じコードを使用したところ、完全に機能しました。この種のシリアル化の問題を克服する最善の方法は何ですか?

4

1 に答える 1

4

遅延読み込みとプロキシ クラス生成を無効にしました。これで問題は解決しました。

public EventRepository()
    {
        HeronEntities context = new HeronEntities();
        context.Configuration.LazyLoadingEnabled = false;
        context.Configuration.ProxyCreationEnabled = false;
        events = context.Events.ToList();
    }
于 2013-03-29T18:21:10.100 に答える