1

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;
    }
Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
wisemikky
  • 11
  • 1

1 Answers1

0

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).

Lucian
  • 3,981
  • 5
  • 30
  • 34
  • 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 '14 at 08:38
  • Please checkout these answers for the possible cause of this: http://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api – Lucian Jun 24 '14 at 09:20
  • Thanks Lucien, followed the link and tried the following and it worked just fine: GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); – wisemikky Jun 24 '14 at 10:05