8

我已经使用流利的 api 制作了实体框架代码。

我有一个 web api 控制器:

public class NewsController : ApiController
{
    private TrafficTheoryContext db = new TrafficTheoryContext();

    // GET api/News
    public IEnumerable<News> GetNews()
    {
        //return new List<News> {
       //  new News{ Title = "Featuring Azure ACS Authentication", Subtile="Hello "}};
        return db.News.AsEnumerable();
    }
} 

如果我发出获取请求,我会收到错误消息:

此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.News_786DE29B12691F869E9C9DF523A808EABE06546C3FCE3354F77875B83B9EB51C' with data contract name 'News_786DE29B12691F869E9C9DF523A808EABE06546C3FCE3354F77875B83B9EB51C:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>
<StackTrace>
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfNewsToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.<>c__DisplayClass7.<WriteToStreamAsync>b__6() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
</StackTrace>
</InnerException>
</Error>

如果我只是返回一个新闻列表,它可以正常工作。我注意到这些类附加了某种我不知道是什么的名称?

我该怎么做才能让 webapi 工作?

4

3 回答 3

4

当我禁用代理创建时,我在 Mcv5 项目中也遇到了同样的问题,每件事都像魅力一样工作。我希望它也会对你有所帮助。

DbContext.Configuration.ProxyCreationEnabled = false;
于 2014-11-13T09:27:32.843 回答
4

我相信您会因为为您的实体生成动态代理而获得此异常。如果您查看异常消息,您实际上并没有序列化 News 类型,而是 System.Data.Entity.DynamicProxies.News_786DE29B12691F869E9C9DF523A808EABE06546C3FCE3354F77875B83B9EB51C 类型。此类型由 Entity Framework 为您创建以支持延迟加载。您可以在此处找到有关动态代理的更多详细信息:http: //msdn.microsoft.com/en-US/data/jj592886。我不太了解 WebAPI,但异常实际上说明了需要做什么:

Type 'System.Data.Entity.DynamicProxies.News_786DE29B12691F869E9C9DF523A808EABE06546C3FCE3354F77875B83B9EB51C' with data contract name 'News_786DE29B12691F869E9C9DF523A808EABE06546C3FCE3354F77875B83B9EB51C:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. 考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

由于类型是自动创建的,因此您不能放置属性。因此,我将专注于“考虑使用 DataContractResolver”和“将它们添加到传递给 DataContractSerializer 的已知类型列表中”部分。

顺便说一句,您使用的是什么版本的 EF?我想我之前看到过这个问题,它在 EF5 RTM 中得到了修复。

于 2012-09-28T21:32:40.203 回答
0

使用 .ToList() 代替 .AsEnumerable。我也很好奇您是如何创建和销毁数据上下文的。

于 2012-09-28T21:40:48.300 回答