Error:You must write an attribute 'type'='object' after writing the attribute with local name '__type'

昨天在使用WebApi的时候,遇到这个错误。

You must write an attribute 'type'='object' after writing the attribute with local name '__type'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.

代码如下:

        public HttpResponseMessage<List<ArticleSimple>> GetArticles(DateTime date)
        {
            //DONE returns top 10 articles
            ArticleService service = new ArticleService();

            var articles = service.GetLatestArticleSimplesByDate(date, null);

            //DONE using viewModel that do not display Metadata and some useless properties
            return new HttpResponseMessage<List<ArticleSimple>>(articles, System.Net.HttpStatusCode.OK);           
        }

解决方法:

        有两种,其中一种简单的是在实例化DbContext时指定 context.Configuration.ProxyCreationEnabled = false;

参考:

       http://forums.asp.net/p/1773173/4848023.aspx/1?You+must+write+an+attribute+type+object+after+writing+the+attribute+with+local+name+__type+

       http://msdn.microsoft.com/zh-cn/library/bb738612

原文地址:https://www.cnblogs.com/swlin/p/2503933.html