c# - 仅当返回类型为 IQueryable 时,Azure MobileApp Controller 才返回 500

标签 c# azure azure-mobile-services iqueryable http-status-code-500

我的 azure 移动应用程序后端出现了奇怪的行为。 如果我的 Controller 操作返回 IQueryable<T>并且实体类型具有导航属性,它返回 500。

一个简单的例子:

型号

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 操作

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

当我请求此操作时,解决方案在我的本地计算机上运行,​​一切正常,但是当我将解决方案发布到 azure 时,FromBrand操作开始返回 500,带有通用消息

"An error has occurred."

此外,当我向操作发出请求时,Azure 日志会显示以下异常:

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

我发现了两个可以避免该错误的更改:

1) 当我装饰Brand时模型的属性 JsonIgnoreBrand属性被忽略,一切正常

2) 当我将操作返回类型更改为 List<ProductHierarchy> 时,保留 Brand没有 JsonIgnore 的模型的属性属性,一切都工作正常。

这让我得出结论,问题发生在序列化 IQueryable<T> 上。当T具有另一个实体作为类型的属性。

我没有发现任何人有同样的问题,所以我开始研究我的 nuget 包,寻找哪个包可以工作或与序列化过程交互,我的怀疑遍布 Newtonsoft Json 和 AutoMapper。

有人知道如何查看这些软件包的内部情况并确定问题的根源吗?

最佳答案

围绕关系存在很多问题和边缘情况。您遇到问题我并不感到惊讶。

一些资源:

  1. 这本书 - http://aka.ms/zumobook (特别是第3章)
  2. 关于关系的博客: https://shellmonger.com/2016/05/27/30-days-of-zumo-v2-azure-mobile-apps-day-26-relationship-advice/

关于c# - 仅当返回类型为 IQueryable 时,Azure MobileApp Controller 才返回 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40663134/

相关文章:

c# - 我需要帮助翻译这部分 ECMAScript 语法吗?

c# - 依赖注入(inject)和库/框架

C#/WPF : Make a GridViewColumn Visible=false?

azure - 如何从不同构建中的第二个存储库运行 Powershell 脚本?

node.js - 使用 Azure 移动服务计划任务访问 Azure 服务管理 REST Api

android - 在 Android 的 Listview 中显示 SQLite 查询结果

c# - 谷歌认证过程

Azure 移动服务寻呼限制

android - 使用 Cordova 的 Azure 通知中心

azure - 使用 Webhook 在 Azure 中配置警报邮件 (SendGrid)