【问题标题】:Breeze Expand Property微风扩展属性
【发布时间】:2013-09-30 18:16:41
【问题描述】:

当我尝试在任何导航属性上使用 Expand 时,我遇到了以下异常。

    $id: "1",
    $type: "System.Web.Http.HttpError, System.Web.Http",
    Message: "An error has occurred.",
    ExceptionMessage: "'object' does not contain a definition for 'Include'",
    ExceptionType: "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException",
    StackTrace: " at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__b.MoveNext() --- End of stack trace from previous  location where exception was thrown --- at  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 

如果我将包含在 Breeze 控制器中,一切正常。如果我不这样做并且只是在客户端上使用扩展,我会收到该错误。非常感谢任何帮助!

这是用于尝试检索数据的 URL

   /breeze/maxsys/CallOutcomes?$expand=CallOutcomeAction

这是模型

[Table("CallOutcomes")]
public class CallOutcome {
    [Key]
    public int Id { get; set; }

    [Required]
    public string Description { get; set; }
    public bool IsInternal { get; set; }


    public int CallOutcomeActionId { get; set; }

    [ForeignKey("CallOutcomeActionId")]
    [InverseProperty("CallOutcomes")]
    public CallOutcomeAction CallOutcomeAction { get; set; }
    public ICollection<CallOutcomeHistory> CallOutcomeHistories { get; set; }
}

Controller看起来如下(我删除了一些其他的get方法)

[BreezeController]
[Authorize]
[RequireHttps]
public class MaxsysController : ApiController

    protected IMaxsysBreezeRepository Repository { get; set; }

    public MaxsysController(IMaxsysBreezeRepository repository)
    {
        Repository = repository;
    }

    [HttpGet]
    public IQueryable<CallOutcome> CallOutcomes()
    {
        return Repository.CallOutcomes;
    } 
}

错误来自 BreezeQueryableAttribute.cs 中的这个方法

    public virtual IQueryable ApplyExpand(IQueryable queryable, string expandsQueryString, HttpRequestMessage request)
    {
        (from s in expandsQueryString.Split(new char[] { ',' }) select s.Trim()).ToList<string>().ForEach(delegate (string expand) {
            queryable = (IQueryable) ((dynamic) queryable).Include(expand.Replace('/', '.'));
        });
        return queryable;
    }

参数值为

   queryable = {SELECT 
[Extent1].[Id] AS [Id], 
N'b1d28373-98a2-4a88-9733-7872acd28bd2' AS [C1], 
[Extent1].[Description] AS [Description], 
[Extent1].[IsInternal] AS [IsInternal], 
[Extent1].[CallOutcomeActionId] AS [CallOutcomeActionId], 
N'CallOutcomeAction' AS [C2], 
N'b1d28373-98a2-4a88-9733-7872acd28bd2' AS [C3], 
[Extent2].[Id] AS [Id1], 
[Extent2].[Description] AS [Description1]
FROM  [dbo].[CallOutcomes] AS [Extent1]
INNER JOIN [dbo].[CallOutcomeActions] AS [Extent2] ON [Extent1].[CallOutcomeActionId] = [Extent2].[Id]}

expandsQueryString = "CallOutcomeAction"

    HttpRequestMessage ={Method: GET, RequestUri: 'http://127.0.0.1:82/breeze/maxsys/CallOutcomes?$expand=CallOutcomeAction', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: keep-alive
  Accept: text/html
  Accept: application/xhtml+xml
  Accept: application/xml; q=0.9
  Accept: */*; q=0.8
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Accept-Encoding: sdch
  Accept-Language: en-US
  Accept-Language: en; q=0.8
  Host: 127.0.0.1:81
  User-Agent: Mozilla/5.0
  User-Agent: (Windows NT 6.1; WOW64)
  User-Agent: AppleWebKit/537.36
  User-Agent: (KHTML, like Gecko)
  User-Agent: Chrome/27.0.1453.94
  User-Agent: Safari/537.36
}}

【问题讨论】:

  • 你的控制器方法是什么样的?

标签: breeze


【解决方案1】:

问题解决了。安装了一些预发布软件来测试 Web API 中的 CORS 支持。一旦我删除了这些更改,expand 就可以正常工作了。感谢您的帮助。

【讨论】:

  • 嗯。我很高兴你找到了。但我对即将出现的威胁 Breeze.net 行为的事情感到不高兴。您能否通过确定您尝试过的预发布软件来补充您的答案。然后,我至少可以考虑为将来与该软件共存做准备。
  • 当然,我安装了 Microsoft ASP.NET Cross-Origin Support (Microsoft.AspNet.Cors 5.0.0-rtm130525)、Microsoft ASP.NET Web API Cross-Origin (Microsoft.AspNet.WebApi .Cors (5.0.0-rtm130525) 和 Microsoft ASP.NET Web API Web Host、Microsoft.AspNet.WebApi.WebHost (5.0.0-rtm-13025)
【解决方案2】:

我也遇到过这个问题,并追踪到应用了 BreezeQueryable 注释。

例外:

ExceptionMessage: "'System.Linq.EnumerableQuery' 不包含 'Include' 的定义",

端点:

[HttpGet]
[BreezeQueryable(PageSize = 1000)] //1000 row limit
public IQueryable<Postcode> Postcodes()
{
    return _db.Context.Postcodes;
}

调用脚本:

var qry = breeze.EntityQuery
                .from('Postcodes')
                .where('Name', breeze.FilterQueryOp.StartsWith, searchTerm)
                .orderBy('PostcodeId')
                .expand('State')
                .take(25);

要修复,只需删除 [BreezeQueryable] 注释:

[HttpGet]
//[BreezeQueryable(PageSize = 1000)] //1000 row limit
public IQueryable<Postcode> Postcodes()
{
    return _db.Context.Postcodes;
}

编辑 - 显然这不是“固定的”。 看起来像是 Breeze WebApi 程序集中的问题。

【讨论】:

    【解决方案3】:

    预赛

    您确定可以查询MaxsysController.CallOutcomes 端点无需展开

    您确定 Repository.CallOutcomes 返回正确的 EF 具体类型吗?

    您在控制器的CallOutcomes() 中尝试了以下类似操作:

    [HttpGet] 公共 IQueryable CallOutcomes() { var foo = Repository.CallOutcomes // 在这里中断;稍后删除。 .Include('CallOutcomeAction').ToList(); 返回 Repository.CallOutcomes; }

    在休息时,您已确认该方法成功而没有失败,并且foo 具有值。

    下一步

    一旦你确认了这些观点,我想我的下一步就是删除[RequireHTTPS]。我最近没有尝试过,而且对我来说要快速设置它并不容易。有可能是我们搞砸了,[RequireHTTPS] 属性和[BreezeController] 属性之间存在干扰。

    还可以尝试将[RequireHTTPS] 放在[BreezeController] 之前。没关系;我现在只是猜测。我会等待你的报告,然后我们可以从那里继续前进。

    【讨论】:

    • 感谢您的回复。 1. 是的,端点确实可以在没有展开的情况下工作。 2. Repository.CallOutcomes 确实返回了正确的具体类型。 3. 我在控制器中添加了包含并且确实有效,但我想从客户端调用扩展。 4. 我删除了 RequireHTTPS 属性,但仍然遇到同样的问题。我还删除了存储库并在控制器中创建了一个 EFContextProvider 对象。
    • ...你让我挂了。那行得通吗?无论哪种方式,我们都有谜团要解决。但如果没有,我怀疑对 Web API 中的操作处理程序有一些干扰。
    • ApplyExpand 方法的 BreezeQueryableAttribute.cs 中出现错误。我把这个方法的参数值放在上面。它在引用包含的 Foreach 处崩溃。
    • 如果我从 GITHUB 运行 Breeze.Learn 示例并升级到最新版本的微风并访问 localhost:3818/breeze/Northwind/Orders?$expand=OrderDetails,如果我将 [BreezeController] 属性添加到 NorthwindController,我会收到类似的错误。
    • 请注意阅读此线程的任何人:@nmushov 发现了预发布 Web API 的原因。见上面他的回答。
    【解决方案4】:

    当我尝试使用操作上的 BreezeQueryableAttribute 限制操作返回的页面大小时,我开始遇到此问题。如果没有该属性,该操作将返回预期的数据。和 gopheruk 一样,我相信 Breeze 目前存在一个错误。

    库版本:

    使用任何预发布库,并且所有内容在 nuget 中都是最新的。

    • Breeze 1.4.2(Breeze.WebApi 1.4.0.0、Breeze.WebApi.EF 1.4.0.0)
    • Microsoft.AspNet.WebApi 4.0.30506
    • EntityFramework 5.0.0

    请求:

    http://localhost/breeze/Data/MyObjects?$filter=Id%20eq%201&$expand=User
    

    在服务器上:

    [BreezeController]
    public class DataController : ApiController
    {
    
        //...
    
        [HttpGet]
        [BreezeQueryable(PageSize = 30)] //if this line is commented out, everything works
        public IQueryable<MyObject> MyObjects()
        {
            return _myObjectRepository.All(User.Identity);
        }
    
    }
    

    回应:

    {"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"An error has occurred.","ExceptionMessage":"'System.Linq.EnumerableQuery<MyObject>' does not contain a definition for 'Include'","ExceptionType":"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException","StackTrace":"   at CallSite.Target(Closure , CallSite , Object , String )\r\n   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)\r\n   at Breeze.WebApi.QueryHelper.<>c__DisplayClass14.<ApplyExpand>b__11(String expand)\r\n   at System.Collections.Generic.List`1.ForEach(Action`1 action)\r\n   at Breeze.WebApi.QueryHelper.ApplyExpand(IQueryable queryable, String expandsQueryString)\r\n   at Breeze.WebApi.QueryHelper.ApplySelectAndExpand(IQueryable queryable, NameValueCollection map)\r\n   at Breeze.WebApi.BreezeQueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted(HttpActionContext actionContext, HttpResponseMessage response, Exception exception)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<>c__DisplayClass2.<System.Web.Http.Filters.IActionFilter.ExecuteActionFilterAsync>b__0(HttpResponseMessage response)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass41`2.<Then>b__40(Task`1 t)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多