Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the routing configuration class.
        /// </summary>
        /// <returns>A new instance of the routing configuration class.</returns>
        public static HttpRequest Create(IRouteBuilder routeBuilder = null, string routeName = null)
        {
            // Add the options services.
            string useRouteName = (routeName == null) ? "OData" : routeName;

            if (routeBuilder == null)
            {
                routeBuilder = RoutingConfigurationFactory.CreateWithRootContainer(useRouteName);
            }

            // Create a new context and assign the services.
            HttpContext context = new DefaultHttpContext();

            context.RequestServices = routeBuilder.ServiceProvider;

            // Ensure there is route data for the routing tests.
            var routeContext = new RouteContext(context);

            context.Features[typeof(IRoutingFeature)] = new RoutingFeature()
            {
                RouteData = routeContext.RouteData,
            };

            // Assign the route and get the request container, which will initialize
            // the request container if one does not exists.
            context.Request.ODataFeature().RouteName = useRouteName;
            IPerRouteContainer perRouteContainer     = routeBuilder.ServiceProvider.GetRequiredService <IPerRouteContainer>();

            if (!perRouteContainer.HasODataRootContainer(useRouteName))
            {
                Action <IContainerBuilder> builderAction   = ODataRouteBuilderExtensions.ConfigureDefaultServices(routeBuilder, null);
                IServiceProvider           serviceProvider = perRouteContainer.CreateODataRootContainer(useRouteName, builderAction);
            }

            // Add some routing info
            IRouter   defaultRoute = routeBuilder.Routes.FirstOrDefault();
            RouteData routeData    = new RouteData();

            if (defaultRoute != null)
            {
                routeData.Routers.Add(defaultRoute);
            }
            else
            {
                var resolver = routeBuilder.ServiceProvider.GetRequiredService <IInlineConstraintResolver>();
                routeData.Routers.Add(new ODataRoute(routeBuilder.DefaultHandler, useRouteName, null, new ODataPathRouteConstraint(useRouteName), resolver));
            }

            var mockAction = new Mock <ActionDescriptor>();
            ActionDescriptor actionDescriptor = mockAction.Object;

            ActionContext actionContext = new ActionContext(context, routeData, actionDescriptor);

            IActionContextAccessor actionContextAccessor = context.RequestServices.GetRequiredService <IActionContextAccessor>();

            actionContextAccessor.ActionContext = actionContext;

            // Get request and return it.
            return(context.Request);
        }
        /// <summary>
        /// Enables dependency injection support for HTTP routes.
        /// </summary>
        /// <param name="builder">The <see cref="IEndpointRouteBuilder"/> to add the container to.</param>
        /// <param name="configureAction">The configuring action to add the services to the root container.</param>
        public static void EnableDependencyInjection(this IEndpointRouteBuilder builder,
                                                     Action <IContainerBuilder> configureAction)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            IPerRouteContainer perRouteContainer = builder.ServiceProvider.GetRequiredService <IPerRouteContainer>();

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

            if (perRouteContainer.HasODataRootContainer(null))
            {
                throw Error.InvalidOperation(SRResources.CannotReEnableDependencyInjection);
            }

            // Get the per-route container and create a new non-route container.
            perRouteContainer.CreateODataRootContainer(null, ConfigureDefaultServices(builder, configureAction));
        }