/// <summary>
        /// Invoke the middleware.
        /// </summary>
        /// <param name="context">The http context.</param>
        /// <returns>A task that can be awaited.</returns>
        public async Task Invoke(HttpContext context)
        {
            HttpRequest request     = context.Request;
            bool        isPreFlight = HttpMethods.IsOptions(request.Method);

            // Attempt to match the path to a batch route.
            ODataBatchPathMapping batchMapping = context.RequestServices.GetRequiredService <ODataBatchPathMapping>();

            if (!isPreFlight && batchMapping.TryGetRouteName(context, out string routeName))
            {
                // Get the per-route container and retrieve the batch handler.
                IPerRouteContainer perRouteContainer = context.RequestServices.GetRequiredService <IPerRouteContainer>();
                if (perRouteContainer == null)
                {
                    throw Error.InvalidOperation(SRResources.MissingODataServices, nameof(IPerRouteContainer));
                }

                IServiceProvider  rootContainer = perRouteContainer.GetODataRootContainer(routeName);
                ODataBatchHandler batchHandler  = rootContainer.GetRequiredService <ODataBatchHandler>();

                await batchHandler.ProcessBatchAsync(context, next);
            }
            else
            {
                await this.next(context);
            }
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeRoutingConvention"/> class.
        /// </summary>
        /// <param name="routeName">The name of the route.</param>
        /// <param name="serviceProvider">The <see cref="IServiceProvider"/> to use for figuring out all the controllers to
        /// look for a match.</param>
        /// <param name="pathTemplateHandler">The path template handler to be used for parsing the path templates.</param>
        /// <remarks>
        /// While this function does not use types that are AspNetCore-specific,
        /// the functionality is due to the way assembly resolution is done in AspNet vs AspnetCore.
        /// </remarks>
        public AttributeRoutingConvention(string routeName, IServiceProvider serviceProvider,
                                          IODataPathTemplateHandler pathTemplateHandler = null)
            : this(routeName)
        {
            if (serviceProvider == null)
            {
                throw Error.ArgumentNull("serviceProvider");
            }

            _serviceProvider = serviceProvider;

            if (pathTemplateHandler != null)
            {
                ODataPathTemplateHandler = pathTemplateHandler;
            }
            else
            {
                IPerRouteContainer perRouteContainer = _serviceProvider.GetRequiredService <IPerRouteContainer>();
                if (perRouteContainer == null)
                {
                    throw Error.InvalidOperation(SRResources.MissingODataServices, nameof(IPerRouteContainer));
                }

                IServiceProvider rootContainer = perRouteContainer.GetODataRootContainer(routeName);
                ODataPathTemplateHandler = rootContainer.GetRequiredService <IODataPathTemplateHandler>();
            }
        }
Exemple #3
0
        /// <remarks>This signature uses types that are AspNetCore-specific.</remarks>
        private IEnumerable <ODataPathTemplate> GetODataPathTemplates(string prefix, ControllerActionDescriptor controllerAction)
        {
            Contract.Assert(controllerAction != null);

            IEnumerable <ODataRouteAttribute> routeAttributes =
                controllerAction.MethodInfo.GetCustomAttributes <ODataRouteAttribute>(inherit: false);

            IPerRouteContainer perRouteContainer = _serviceProvider.GetRequiredService <IPerRouteContainer>();

            if (perRouteContainer == null)
            {
                throw Error.InvalidOperation(SRResources.MissingODataServices, nameof(IPerRouteContainer));
            }

            IServiceProvider requestContainer = perRouteContainer.GetODataRootContainer(_routeName);

            string controllerName = controllerAction.ControllerName;
            string actionName     = controllerAction.ActionName;

            return
                (routeAttributes
                 .Where(route => string.IsNullOrEmpty(route.RouteName) || route.RouteName == _routeName)
                 .Select(route => GetODataPathTemplate(prefix, route.PathTemplate, requestContainer, controllerName, actionName))
                 .Where(template => template != null));
        }
        /// <summary>
        /// Create a scoped request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="routeName">The route name.</param>
        /// <returns></returns>
        private static IServiceScope CreateRequestScope(this HttpRequest request, string routeName)
        {
            IPerRouteContainer perRouteContainer = request.HttpContext.RequestServices.GetRequiredService <IPerRouteContainer>();

            if (perRouteContainer == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, SRResources.MissingODataServices, nameof(IPerRouteContainer)));
            }

            IServiceProvider rootContainer = perRouteContainer.GetODataRootContainer(routeName);
            IServiceScope    scope         = rootContainer.GetRequiredService <IServiceScopeFactory>().CreateScope();

            // Bind scoping request into the OData container.
            if (!string.IsNullOrEmpty(routeName))
            {
                scope.ServiceProvider.GetRequiredService <HttpRequestScope>().HttpRequest = request;
            }

            return(scope);
        }
Exemple #5
0
        private IServiceProvider GetServiceProvider(IRouteBuilder routeBuilder, string routeName)
        {
            IPerRouteContainer perRouteContainer = routeBuilder.ServiceProvider.GetRequiredService <IPerRouteContainer>();

            return(perRouteContainer.GetODataRootContainer(routeName));
        }
Exemple #6
0
        /// <summary>
        /// Get the OData root container for a given route.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="routeName">The route name.</param>
        /// <returns>The OData root container for a given route.</returns>
        internal static IServiceProvider GetODataRootContainer(this HttpConfiguration configuration, string routeName)
        {
            IPerRouteContainer perRouteContainer = GetPerRouteContainer(configuration);

            return(perRouteContainer.GetODataRootContainer(routeName));
        }