Breeze | datajs - no handler for data

2019-09-11 11:28发布

I am new in breeze world, but I want use it in my application. I tried test it with simple ASP.Net Api OData service based on Northwind.sdf database.

In DataService project I have a simple controller:

[BreezeController]
public class CategoriesController : ODataController
{
    readonly EFContextProvider<NORTHWNDContext> contextProvider =
        new EFContextProvider<NORTHWNDContext>();

    [HttpGet]
    [EnableBreezeQuery]
    public IQueryable<Category> GetCategories()
    {
        return contextProvider.Context.Categories;
    }

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

And on client I call:

breeze.config.initializeAdapterInstance("dataService", "webApiOData", false);

var manager = new breeze.EntityManager({
    dataService: new breeze.DataService({
        serviceName: "http://localhost:18384/",
        hasServerMetadata: true,
        adapterName: "webApiOData"
    })
});

breeze.EntityQuery.from('Categories').using(manager).execute();

Problem occures in datajs.js file in this method

var dispatchHandler = function (handlerMethod, requestOrResponse, context) {
    /// <summary>Dispatches an operation to handlers.</summary>
    /// <param name="handlerMethod" type="String">Name of handler method to invoke.</param>
    /// <param name="requestOrResponse" type="Object">request/response argument for delegated call.</param>
    /// <param name="context" type="Object">context argument for delegated call.</param>

    var i, len;
    for (i = 0, len = handlers.length; i < len && !handlers[i][handlerMethod](requestOrResponse, context); i++) {
    }

    if (i === len) {
        throw { message: "no handler for data" };
    }
};

It allways throws "no handler for data" exception but I dont understand. There are already some default odata handlers like

var handlers = [odata.jsonHandler, odata.atomHandler, odata.xmlHandler, odata.textHandler];

Can somebody help me with this problem? Thanks.

2条回答
时光不老,我们不散
2楼-- · 2019-09-11 12:13

I think you are missing Metadata in your contoller

public string Metadata()
    {
        return _contextProvider.Metadata();
    }
查看更多
3楼-- · 2019-09-11 12:23

If your service uses OData version 4 then DataJs and inherently BreezeJs (which usess datajs) will throw this exception as you have found.

Breeze has an open issue for this: https://github.com/Breeze/breeze.js/issues/39. It looks like you can use this BreezeJs adapter to solve problems with OData version 4.0: https://github.com/tschettler/breezejs-odata4-adapter

Here is a status report as of May 2014 that confirms the above: http://www.odata.org/blog/status-updates-of-odata-libraries-that-support-odata-version-4-0/

For OData version 4 you can use another javascript library called Olingo as described here: http://olingo.apache.org/doc/javascript/index.html

Here is a summary of libraries with specified supported version: http://www.odata.org/libraries/

查看更多
登录 后发表回答