Guest User

.Net core OData with EDM troubles

a guest
Aug 3rd, 2019
353
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Thanks for the article! 
  2. I seem to be having mixed results trying to get my code working. 
  3. I'm running .net core 2.1 and Microsoft.AspNetCore.OData Version=7.1.0.
  4. My startup extensions:
  5.     // invoked from startup, ConfigureServices()
  6. public static IServiceCollection AddODataSupport(this IServiceCollection services)
  7. {
  8.     services.AddOData();
  9.     //Workaround: https://github.com/OData/WebApi/issues/1177
  10.     services.AddMvcCore(options =>
  11.     {
  12.         foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>()
  13.             .Where(_ => _.SupportedMediaTypes.Count == 0))
  14.         {
  15.             outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
  16.         }
  17.         foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>()
  18.             .Where(_ => _.SupportedMediaTypes.Count == 0))
  19.         {
  20.             inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
  21.         }
  22.     });
  23.     return services;
  24. }
  25.  
  26. // invoked from startup, app.UseMVC()
  27. public static void ConfigureODataRouting(this IRouteBuilder routeBuilder)
  28. {
  29.     routeBuilder.EnableDependencyInjection();
  30.     routeBuilder.Select().Filter().OrderBy().Expand().Count().MaxTop(100);routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
  31. }
  32.  
  33. private static IEdmModel GetEdmModel()
  34. {
  35.     var obuilder = new ODataConventionModelBuilder();
  36.     obuilder.EntitySet<TransactionEntity>("Transactions");
  37.     return obuilder.GetEdmModel();
  38. }
  39. Here's my controller:
  40.  
  41. public class ODataController : ControllerBase
  42. {
  43.     private readonly ITmDataContext dataContext;
  44.     public ODataController(ITmDataContext dataContext)
  45.     {
  46.         this.dataContext = dataContext;
  47.     }
  48.  
  49.     [Route("odata/transactions")]
  50.     [HttpGet]
  51.     [EnableQuery]
  52.     public ActionResult<IEnumerable<object>> GetTransactions()
  53.     {
  54.         return dataContext.Transactions;
  55.     }
  56.  }
  57. I can hit the $metadata endpoint and get my edm xml. 
  58.  
  59. <?xml version="1.0" encoding="utf-8"?><edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"><edmx:DataServices><Schema Namespace="TMS.DataManagement.Service.Data.Entities" xmlns="http://docs.oasis-open.org/odata/ns/edm"><EntityType Name="TransactionEntity"><Key><PropertyRef Name="Id" /></Key><Property Name="Id" Type="Edm.Int32" Nullable="false" /> ...
  60.  
  61. and I can hit the odata/Transactions endpoint.
  62.     HTTP GET https://localhost:5001/odata/Transactions returns data like
  63.     [
  64.         {
  65.           "id": 2860,
  66.           "school_id": 3,
  67.           "type_code": 1,
  68.           "post_status_code": 0,
  69.           "post_date": "2019-07-01T00:00:00",
  70.           "deleted": true,
  71.           "description": "fee name",
  72.           "comments": "",
  73.           "insert_date": "2019-07-01T11:35:48.2343931",
  74.           "modified_date": "2019-07-02T17:24:35.5232986",
  75.           "last_modify_user_id": 100169
  76.         }
  77.      ,... etc
  78.     ]
  79. BUT:
  80. 1) my results never have the edm metadata (payload is missing the "@odata.context" node)
  81. 2) some operations like $expand and $filter work, but others like $count=true don't include the count node. 
  82. 3) If I try to consume my odata feed in a client application (like excel), I get an error           
  83.     'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected. 
  84.  
  85. If I remove the [Route] attribute on my endpoint as you suggested, I can't hit the odata/transactions endpoint. I get a 404.  
  86. All of the above makes me think I haven't implemented the OData routing correctly, but I'm not sure where I've messed up. 
  87. Any thoughts?
RAW Paste Data