我知道SO中有很多这样的错误消息,因为我已经阅读了所有这些错误消息,可惜没有用。
我有一个WebApi控制器,该控制器通过EF6从SQL Server DB获取一组“人员”。很简单的例子
到目前为止,我已经尝试过的事情没有成功:-禁用代理生成-禁用延迟加载-添加Includes以获得带有linq和string参数的子引用。-用try / finally替换使用->处理DbContext。-通过WebApiConfig从受支持的媒体类型中删除“ application / xml”-确保循环依存关系归因于[IgnoreDataMember]-...更多我不记得了:)
这是PersonController的 Get方法:
public IEnumerable<Person> Get()
{
try
{
IEnumerable<Person> persons = null;
using (PersonContext entities = new PersonContext())
{
entities.Configuration.ProxyCreationEnabled = false;
entities.Configuration.LazyLoadingEnabled = false;
persons = entities.Persons.Take(5);
}
return persons;
}
catch(Exception ex)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在控制器的任何位置都不会引发异常。但是,异常显示在浏览器中:
"<Error><Message>An error has occurred.<\Message>
<ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application\/json; charset=utf-8'.
<\ExceptionMessage>
<ExceptionType>System.InvalidOperationException<\ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.<\/Message>
<ExceptionMessage>**The operation cannot be …Run Code Online (Sandbox Code Playgroud)