/// <summary>
        /// Gets the dependency injection container for the OData request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The dependency injection container.</returns>
        public static IServiceProvider GetSubServiceProvider(this HttpRequest request)
        {
            if (request == null)
            {
                throw Error.ArgumentNull(nameof(request));
            }

            IServiceProvider requestContainer = request.ODataFeature().SubServiceProvider;

            if (requestContainer != null)
            {
                return(requestContainer);
            }

            IOptions <ODataOptions> odataOptionsOptions = request.HttpContext.RequestServices.GetRequiredService <IOptions <ODataOptions> >();

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

            ODataOptions options = odataOptionsOptions.Value;

            return(options.GetODataServiceProvider(request.ODataFeature().PrefixName));

            // HTTP routes will not have chance to call CreateRequestContainer. We have to call it.
            // return request.CreateSubServiceProvider(request.ODataFeature().PrefixName);
        }
        public void GetODataServiceProvider_ReturnsNull()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            // Act & Assert
            Assert.Null(options.GetODataServiceProvider(null));
        }
        public void GetODataServiceProvider_ReturnsCorrectServiceProvider()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            options.AddModel("odata", edmModel);

            // & Assert
            IServiceProvider sp = options.GetODataServiceProvider("odata");

            Assert.NotNull(sp);
        }
        /// <summary>
        /// Create a scoped request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="prefixName">The prefix name.</param>
        /// <returns></returns>
        private static IServiceScope CreateRequestScope(this HttpRequest request, string prefixName)
        {
            IOptions <ODataOptions> odataOptionsOptions = request.HttpContext.RequestServices.GetRequiredService <IOptions <ODataOptions> >();

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

            ODataOptions options = odataOptionsOptions.Value;

            IServiceProvider rootContainer = options.GetODataServiceProvider(prefixName);
            IServiceScope    scope         = rootContainer.GetRequiredService <IServiceScopeFactory>().CreateScope();

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

            return(scope);
        }