Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to return some data to client page using webAPI. Please see below the exception am currently seeing while trying to access the api/controller from a browser

StackTrace System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType) 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)

Controller GET method that returns the Quotes is

    // GET api/<controller>
    private ForumDBDataContext db = new ForumDBDataContext(); 

    public dynamic GetQuotes()
    {
        var ret = (from qt in db.vwUsersQuotess.ToList()                      
                   select new
                   {
                       Message = qt.Desc,
                       Price= qt.price,
                       Qty = qt.Quantity
                   }).AsEnumerable();
        return ret;
    }
share|improve this question
add comment

1 Answer

Try to use the ToList() instead of AsEnumerable():

public dynamic GetQuotes()
    {
        var ret = (from qt in db.vwUsersQuotess                      
                   select new
                   {
                       Message = qt.Desc,
                       Price= qt.price,
                       Qty = qt.Quantity
                   }).ToList();
        return ret;
    }

If you use EntityFramework and SQL Server you can also remove the call to vwUsersQuotess.ToList() and do the projection (select new...) directly on the SQL server, so that the query that is being executed only retrieves the necessary columns (Desc, price and Quantity).

share|improve this answer
    
tried that and got this An error has occurred.The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.System.InvalidOperationExceptionAn error has occurred.Type 'System.Collections.Generic.List`1[[<>f__AnonymousType1`7[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], –  wisemikky Jun 24 at 8:38
    
Please checkout these answers for the possible cause of this: stackoverflow.com/questions/12641386/… –  Lucian Jun 24 at 9:20
    
Thanks Lucien, followed the link and tried the following and it worked just fine: GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configur‌​ation.Formatters.XmlFormatter); –  wisemikky Jun 24 at 10:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.