0

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.

2
  • The error in the title points to Accounts being empty. The exception is being thrown later because of the lazy approach of LINQ. If you add a .ToList() after var Result = resultSet.Select(...) the error should be thrown there. Commented Oct 25, 2019 at 12:14
  • 1
    Thanks for your answer, the accounts are not showing as empty. I have posted the reason why it happened cause a few minutes after posting I managed to fix it on my own. Commented Oct 25, 2019 at 12:16

1 Answer 1

2

The issue in my case was a stupid overlooked mistake

When selecting and building the new list with other info, the match was being made on account ID against transaction ID which was not getting any results due to my using of "Single".

I kept the Single but matched appropriately against t.AccountID

From

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
} );

To

var Result = resultSet.Select( t => new ApiTransaction()
{
    ID = t.ID,
    Name = Accounts.Single( a => a.ID == t.AccountID ).Name,
    Surname = Accounts.Single( a => a.ID == t.AccountID ).Surname,
    Company = Accounts.Single( a => a.ID == t.AccountID ).Company,
    VatNumber = Accounts.Single( a => a.ID == t.AccountID ).VatNumber,
    Action = t.Action,
    Credit = t.Credit,
    Debit = t.Debit,
    Details = t.Details,
    Timestamp = t.Timestamp
} );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.