Esempio n. 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>
        /// Creates an action selector, descriptors and route builder based on
        /// the specified controller
        /// </summary>
        /// <param name="controllerType"></param>
        /// <param name="actionName">Action name of the action descriptors to return</param>
        /// <param name="routeBuilder"></param>
        /// <param name="actionSelector"></param>
        /// <param name="actionDescriptors"></param>
        public static void SetupActionSelector(System.Type controllerType,
                                               string actionName,
                                               out IRouteBuilder routeBuilder,
                                               out ODataActionSelector actionSelector,
                                               out IReadOnlyList <ControllerActionDescriptor> actionDescriptors)

        {
            routeBuilder      = RoutingConfigurationFactory.Create();
            actionDescriptors = ControllerDescriptorFactory.Create(routeBuilder, controllerType.Name, controllerType)
                                as IReadOnlyList <ControllerActionDescriptor>;
            actionDescriptors = actionDescriptors.Where(a => a.ActionName == actionName).ToList();
            var serviceProvider = routeBuilder.ServiceProvider;

#if NETCOREAPP2_0
            var actionsProvider           = serviceProvider.GetRequiredService <IActionDescriptorCollectionProvider>();
            var actionConstraintsProvider = serviceProvider.GetRequiredService <ActionConstraintCache>();
            var loggerFactory             = serviceProvider.GetRequiredService <ILoggerFactory>();
            var modelBinderFactory        = serviceProvider.GetRequiredService <IModelBinderFactory>();
            var modelMetadataProvider     = serviceProvider.GetRequiredService <IModelMetadataProvider>();
            actionSelector = new ODataActionSelector(
                actionsProvider,
                actionConstraintsProvider,
                loggerFactory,
                modelBinderFactory,
                modelMetadataProvider);
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the routing configuration class.
        /// </summary>
        /// <returns>A new instance of the routing configuration class.</returns>
        public static HttpResponse Create(HttpStatusCode statusCode, string content = null)
        {
            // Add the options services.
            IRouteBuilder config = RoutingConfigurationFactory.CreateWithRootContainer("OData");

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

            context.RequestServices = config.ServiceProvider;
            //context.ODataFeature().RequestContainer = provider;

            // Get response and return it.
            HttpResponse response = context.Response;

            response.StatusCode = (int)statusCode;

            // Add content
            if (!string.IsNullOrEmpty(content))
            {
                byte[] byteArray = Encoding.ASCII.GetBytes(content);
                using (MemoryStream contentStream = new MemoryStream(byteArray))
                {
                    contentStream.CopyTo(response.Body);
                }
            }

            return(response);
        }
Esempio n. 4
0
        private void InitializeConfiguration(Action <IContainerBuilder> action)
        {
            var    configuration = RoutingConfigurationFactory.Create();
            string routeName     = HttpRouteCollectionExtensions.RouteName;

            _rootContainer = configuration.CreateODataRootContainer(routeName, action);
        }
Esempio n. 5
0
        private void InitializeConfiguration(Action <IContainerBuilder> action)
        {
            var                        configuration     = RoutingConfigurationFactory.Create();
            string                     routeName         = HttpRouteCollectionExtensions.RouteName;
            IPerRouteContainer         perRouteContainer = configuration.ServiceProvider.GetRequiredService <IPerRouteContainer>();
            Action <IContainerBuilder> builderAction     = ODataRouteBuilderExtensions.ConfigureDefaultServices(configuration, action);

            _rootContainer = perRouteContainer.CreateODataRootContainer(routeName, builderAction);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the routing configuration class.
        /// </summary>
        /// <returns>A new instance of the routing configuration class.</returns>
        public static HttpRequestMessage CreateFromModel(IEdmModel model, string uri = "http://localhost", string routeName = "Route", ODataPath path = null)
        {
            var configuration = RoutingConfigurationFactory.Create();

            configuration.MapODataServiceRoute(routeName, null, model);

            var request = RequestFactory.Create(HttpMethod.Get, uri, configuration, routeName);

            if (path != null)
            {
                request.ODataProperties().Path = path;
            }

            return(request);
        }
        /// <summary>
        /// Initializes a new instance of the routing configuration class.
        /// </summary>
        /// <returns>A new instance of the routing configuration class.</returns>
        internal static IWebApiAssembliesResolver Create(MockAssembly assembly = null)
        {
            IRouteBuilder builder = RoutingConfigurationFactory.Create();

            ApplicationPartManager applicationPartManager = builder.ApplicationBuilder.ApplicationServices.GetRequiredService <ApplicationPartManager>();

            applicationPartManager.ApplicationParts.Clear();

            if (assembly != null)
            {
                AssemblyPart part = new AssemblyPart(assembly);
                applicationPartManager.ApplicationParts.Add(part);
            }

            return(new WebApiAssembliesResolver(applicationPartManager));
        }