Vitor Linares Vitor Linares - 18 days ago 7
C# Question

Azure MobileApp Controller returns 500 only if return type is IQueryable

My azure mobile app backend is presenting a strange behavior.
If my controller action returns an

IQueryable<T>
and the entity type has a navigation property, it returns 500.

A simple example:

Model

public class ProductHierarchy : EntityData
{
public string Name { get; set; }

public string Description { get; set; }

public DateTime ValidFrom { get; set; }

public DateTime ValidTo { get; set; }

public string BrandId{ get; set; }
[ForeignKey("BrandId")]
public virtual Brand Brand { get; set; }

public ProductStatus Status { get; set; }

public int CreatedBy { get; set; }

public int ModifiedBy { get; set; }
}


Controller Action

[HttpGet]
[Route("api/ProductHierarchies/FromBrand/{brandId}")]
public IQueryable<ProductHierarchy> FromBrand(int brandId)
{
var hierarchies = Query().Where(hi => hi.Brand.OldBrandId ==brandId);
return hierarchies;
}


When I make a request to this action, with the solution running on my local machine, everything works fine, however when I publish the solution to azure, the
FromBrand
action starts to return 500, with the generic message

"An error has occurred."


In addition, Azure Logs shows me the following exception when I make a request to the action:

Detailed Error Information: Module
__DynamicModule_Microsoft.Owin.Host.SystemWeb.OwinHttpModule,Microsoft.Owin.Host .SystemWeb, Version=3.0.1.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35_19e9f0a3-023d-4d8b-83ef- 180a415e7921 Notification PreExecuteRequestHandler Handler ExtensionlessUrlHandler-Integrated-4.0 Error Code 0x00000000


I've found two changes that can avoid the error:

1) When I decorate the
Brand
property of the Model with
JsonIgnore
, the
Brand
property is ignored and everything works fine

2) When I change the action return type to
List<ProductHierarchy>
, keeping the
Brand
property of the model without the
JsonIgnore
atribute, everything works fine too.

It leads me to conclude that the problem is happening serializing
IQueryable<T>
when
T
have a property with another entity as type.

I didn't found anyone having the same issue, so I started to look into my nuget packages looking for wich package Works or Interact with the serialization proccess, and my suspicious are all over Newtonsoft Json and AutoMapper.

Anyone have some clue about how to look under the hood of these packages and determine the origin of the problem?

Answer

There are lots of issues and edge cases around relationships. I'm not surprised you have bumped into an issue.

Some resources:

  1. The book - http://aka.ms/zumobook (particularly chapter 3)
  2. Blog on relationships: https://shellmonger.com/2016/05/27/30-days-of-zumo-v2-azure-mobile-apps-day-26-relationship-advice/