简体   繁体   中英

OData Expand fails on Client Win8.1 universal app

Just a quick question, is this not supported in a Win 8.1 universal class library? Or if it is, can anyone help with what i'm doing wrong.

http://jbsapplication.azurewebsites.net/Modules?$filter=Name%20eq%20'JBS%20Electronic%20forms'&$expand=Menus

When I do this from the browser or Fiddler I receive the correct response.

My code in the client view model class is as follows (using the OData Client v2 code generated objects)

var application = new UriBuilder(ServiceBaseAddress);
var context = new Models.Application(application.Uri);

var modulesQuery = context.Modules.Expand(m=>m.Menus).Where(m => m.Name == ApplicationName);
var modules = await ((DataServiceQuery<Module>) modulesQuery).ExecuteAsync();
_currentModule = modules.FirstOrDefault();

The following exception is generated on the last line

A first chance exception of type 'Microsoft.OData.Core.ODataException' occurred in Microsoft.OData.Core.DLL

Additional information: When writing a JSON response, a user model must be specified and the entity set and entity type must be passed to the ODataMessageWriter.CreateODataEntryWriter method or the ODataFeedAndEntrySerializationInfo must be set on the ODataEntry or ODataFeed that is being writen.

If I remove the Expand portion of the request, everything is fine, but I need to then perform another round trip to get the menus.

A cut down reference for the Module class:

[Key("Id")]
public class Module: BindableBase
{
    public string Name
    {
        get { return _name; }
        set { SetProperty(ref _name, value); }
    }

    DataServiceCollection<Menu> _menus = new DataServiceCollection<Menu>(null,TrackingMode.AutoChangeTracking);

    public DataServiceCollection<Menu> Menus
    {
        get { return _menus; }
        set
        {
            _menus = value;
            OnPropertyChanged("Menus");
        }
    }
}

I have encountered the problem you describe when I forgot to add the expanding entity into the ODataModelBuilder as an EntitySet. Try this in your ASP.NET OData Web API:

builder.EntitySet<Menus>("Menus");

客户端需要显式地扩展具有ID属性的模型,并且可扩展的模型需要在构建器中注册为实体集,以便自动生成的OData客户端能够调用扩展。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM