I did some searches around and don't like to remove the xml formatter as many have suggested, I want to pin point the issue and fix it appropriately, as in the future this api will need to output xml not just json.
With that out of the way, the issue is, with 1 of the api endpoints I am creating a list of transactions with some account info that I am trying to retrieve on an app. However, I am getting the following error in Postman.
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
Within my code i am accessing a facade that gets the data for me, and assembling the result set in a bucket.
var Accounts = AccountsFacade.GetByID( resultSet.Select( t => t.AccountID ).Distinct() );
var Result = resultSet.Select( t => new ApiTransaction()
{
ID = t.ID,
Name = Accounts.Single( a => a.ID == t.ID ).Name,
Surname = Accounts.Single( a => a.ID == t.ID ).Surname,
Company = Accounts.Single( a => a.ID == t.ID ).Company,
VatNumber = Accounts.Single( a => a.ID == t.ID ).VatNumber,
Action = t.Action,
Credit = t.Credit,
Debit = t.Debit,
Details = t.Details,
Timestamp = t.Timestamp
} );
var resultBucket = new Bucket( )
{
TotalResults = TransactionsCount,
Count = resultSet.Count,
Page = Page,
TotalPages = TotalPages,
Result = Result
};
var response = Request.CreateResponse( HttpStatusCode.OK, resultBucket, Configuration.Formatters.JsonFormatter );
response.Headers.Add( "Access-Control-Allow-Origin", "*" );
return response;
The return type of my endpoint is of 'HttpResponseMessage'
Debugging
I did some debugging and tried to identify the issue, but failed to find anything wrong with the response. The response is being set to the json format as expected and no errors are thrown within the controller or the application.
Any help is greatly appreciated.
Accountsbeing empty. The exception is being thrown later because of the lazy approach of LINQ. If you add a.ToList()aftervar Result = resultSet.Select(...)the error should be thrown there.