Search code examples
c#odatawin-universal-app

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");
        }
    }
}

Solution

  • 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");