public static ODataRoute CustomMapODataServiceRoute(this HttpConfiguration configuration, string routeName, string routePrefix) { ODataRoute route = configuration.MapODataServiceRoute(routeName, routePrefix, builder => { // Get the model from the datasource of the current request: model-per-request. builder.AddService(ServiceLifetime.Scoped, sp => { IHttpRequestMessageProvider requestMessageProvider = sp.GetRequiredService <IHttpRequestMessageProvider>(); string dataSource = requestMessageProvider.Request.Properties[Constants.ODataDataSource] as string; IEdmModel model = DataSourceProvider.GetEdmModel(dataSource); return(model); }); // Create a request provider for every request. This is a workaround for the missing HttpContext of a self-hosted webapi. builder.AddService <IHttpRequestMessageProvider>(ServiceLifetime.Scoped, sp => new HttpRequestMessageProvider()); // The routing conventions are registered as singleton. builder.AddService(ServiceLifetime.Singleton, sp => { IList <IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault(); routingConventions.Insert(0, new MatchAllRoutingConvention()); return(routingConventions.ToList().AsEnumerable()); }); }); CustomODataRoute odataRoute = new CustomODataRoute(route.RoutePrefix, new CustomODataPathRouteConstraint(routeName)); configuration.Routes.Remove(routeName); configuration.Routes.Add(routeName, odataRoute); return(odataRoute); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseODataBatching(); app.UseEndpoints(endpoints => { endpoints.Select().Expand().Filter().OrderBy().MaxTop(100).Count(); var routeBuilder = endpoints.MapODataRoute("odataPrefix", "odata/{dataSource}", containerBuilder => { containerBuilder.AddService(Microsoft.OData.ServiceLifetime.Scoped, typeof(IEdmModel), sp => { var serviceScope = sp.GetRequiredService <HttpRequestScope>(); IEdmModel model = DataSourceProvider.GetEdmModel(serviceScope.HttpRequest); return(model); }); containerBuilder.AddService(Microsoft.OData.ServiceLifetime.Scoped, typeof(IEnumerable <IODataRoutingConvention>), sp => { IList <IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault(); routingConventions.Insert(0, new MatchAllRoutingConvention()); return(routingConventions.ToList().AsEnumerable()); }); }); }); }
private static Func <HttpRequestMessage, IEdmModel> GetModelFuncFromRequest() { return(request => { string odataPath = request.Properties[Constants.CustomODataPath] as string ?? string.Empty; string[] segments = odataPath.Split('/'); string dataSource = segments[0]; request.Properties[Constants.ODataDataSource] = dataSource; IEdmModel model = DataSourceProvider.GetEdmModel(dataSource); request.Properties[Constants.CustomODataPath] = string.Join("/", segments, 1, segments.Length - 1); return model; }); }
public static ODataRoute CustomMapODataServiceRoute(this IRouteBuilder routeBuilder, string routeName, string routePrefix) { ODataRoute route = routeBuilder.MapODataServiceRoute(routeName, routePrefix, builder => { // Get the model from the datasource of the current request: model-per-pequest. builder.AddService(ServiceLifetime.Scoped, sp => { var serviceScope = sp.GetRequiredService <HttpRequestScope>(); // serviceScope. string sourceString = serviceScope.HttpRequest.GetDataSource(); IEdmModel model = DataSourceProvider.GetEdmModel(sourceString); return(model); }); // The routing conventions are registered as singleton. builder.AddService(ServiceLifetime.Singleton, sp => { IList <IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault(); routingConventions.Insert(0, new MatchAllRoutingConvention()); return(routingConventions.ToList().AsEnumerable()); }); }); // route.Constraints. IRouter customRouter = routeBuilder.ServiceProvider.GetService <IRouter>(); // Get constraint resolver. IInlineConstraintResolver inlineConstraintResolver = routeBuilder.ServiceProvider.GetRequiredService <IInlineConstraintResolver>(); CustomODataRoute odataRoute = new CustomODataRoute(customRouter != null ? customRouter : routeBuilder.DefaultHandler, routeName, routePrefix, new CustomODataPathRouteConstraint(routeName), inlineConstraintResolver); routeBuilder.Routes.Remove(route); routeBuilder.Routes.Add(odataRoute); return(odataRoute); }