Breeze webapi controller 'cannot support query

2019-08-19 09:34发布

问题:

I have setup very similar to breeze sample application which comes in sample nuget. This is the code of my api controller:

[JsonFormatter, ODataActionFilter]
public class WorkOrdersController : ApiController
{
    readonly EFContextProvider<WorkOrdersContext> _contextProvider =
        new EFContextProvider<WorkOrdersContext>();

    public WorkOrdersController()
    {
        // I was thinking this may be the cause of the issue
        this._contextProvider.Context.Configuration.ProxyCreationEnabled = false;
    }

    [HttpGet]
    public string Metadata()
    {
        return _contextProvider.Metadata();
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        return _contextProvider.SaveChanges(saveBundle);
    }

    [HttpGet]
    public IQueryable<WorkOrder> WorkOrders()
    {
        return _contextProvider.Context.WorkOrders;
    }

}

The problem I'm having is when I try to perform query over WorkOrders action, I get 500 - Internal server error, and this is the payload of response:

{"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"An error has occurred.","ExceptionMessage":"The action 'WorkOrders' on controller 'WorkOrders' with return type 'System.Collections.Generic.List`1[[WorkOrders.Domain.Models.WorkOrder, WorkOrders.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot support querying. Ensure the type of the returned content is IEnumerable, IQueryable, or a generic form of either interface.","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.QueryableAttribute.ValidateReturnType(Type responseContentType, HttpActionDescriptor actionDescriptor)\r\n   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)\r\n   at System.Web.Http.Tracing.Tracers.ActionFilterAttributeTracer.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\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)"}

WorkOrders is Dbset on context:

public DbSet<WorkOrder> WorkOrders { get; set; }

I have also tried explicitly casting it to IQueryable, with no change:

[HttpGet]
public IQueryable<WorkOrder> WorkOrders()
{
    return (IQueryable<WorkOrder>)_contextProvider.Context.WorkOrders;
}

The only thing that works for me is:

[HttpGet] public IEnumerable WorkOrders() { return _contextProvider.Context.WorkOrders.AsEnumerable(); }

This, however causes another problem for me on the client side, described in this question.

回答1:

Just a guess here, but can you try using the latest breeze (v 0.82.1) with the following attribute.

[BreezeController]

instead of these

[JsonFormatter, ODataActionFilter]

The new [BreezeController] attribute is a complete replacement for the other two and avoids some other issues.