public void Ctor_SetServices()
        {
            // Arrange
            var services = new Mock <IServiceCollection>();

            // Act
            DefaultODataBuilder builder = new DefaultODataBuilder(services.Object);

            // Assert
            Assert.Same(services.Object, builder.Services);
        }
        /// <summary>
        /// Adds the core OData services required for OData requests, excluding the <see cref="ODataOptions"/> configuration.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <returns>The <see cref="IODataBuilder"/> so that additional calls can be chained.</returns>
        private static IODataBuilder AddODataCore(this IServiceCollection services)
        {
            if (services == null)
            {
                throw Error.ArgumentNull(nameof(services));
            }

            services.TryAddEnumerable(
                ServiceDescriptor.Singleton <IODataQueryRequestParser, DefaultODataQueryRequestParser>());
            services.TryAddSingleton <IAssemblyResolver, DefaultAssemblyResolver>();
            services.TryAddSingleton <IODataTypeMappingProvider, ODataTypeMappingProvider>();

            // Configure MvcCore to use formatters. The OData formatters do go into the global service
            // provider and get picked up by the AspNetCore MVC framework. However, they ignore non-OData
            // requests so they won't be used for non-OData formatting.
            services.AddControllers(options =>
            {
                // Add OData input formatters at index 0, which overrides the built-in json and xml formatters.
                // Add in reverse order at index 0 to preserve order from the factory in the final list.
                foreach (ODataInputFormatter inputFormatter in ODataInputFormatterFactory.Create().Reverse())
                {
                    options.InputFormatters.Insert(0, inputFormatter);
                }

                // Add OData output formatters at index 0, which overrides the built-in json and xml formatters.
                // Add in reverse order at index 0 to preserve order from the factory in the final list.
                foreach (ODataOutputFormatter outputFormatter in ODataOutputFormatterFactory.Create().Reverse())
                {
                    options.OutputFormatters.Insert(0, outputFormatter);
                }

                // Add the value provider.
                // options.ValueProviderFactories.Insert(0, new ODataValueProviderFactory());
            })
            .AddJsonOptions(options =>
            {
                // Add the Select expand and other wrapper converter factory
                options.JsonSerializerOptions.Converters.Add(new SelectExpandWrapperConverter());
                options.JsonSerializerOptions.Converters.Add(new PageResultValueConverter());
                options.JsonSerializerOptions.Converters.Add(new DynamicTypeWrapperConverter());
            });

            services.AddODataRouting();

            IODataBuilder builder = new DefaultODataBuilder(services);

            return(builder);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds services required for OData requests.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="setupAction">The OData options to configure the services with.</param>
        /// <returns>The <see cref="IODataBuilder"/> so that additional calls can be chained.</returns>
        public static IODataBuilder AddOData(this IServiceCollection services, Action <ODataOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.TryAddSingleton <IAssemblyResolver, DefaultAssemblyResolver>();
            services.TryAddSingleton <IODataTypeMappingProvider, ODataTypeMappingProvider>();

            // Configure MvcCore to use formatters. The OData formatters do go into the global service
            // provider and get picked up by the AspNetCore MVC framework. However, they ignore non-OData
            // requests so they won't be used for non-OData formatting.
            services.AddControllers(options =>
            {
                // Add OData input formatters at index 0, which overrides the built-in json and xml formatters.
                // Add in reverse order at index 0 to preserve order from the factory in the final list.
                foreach (ODataInputFormatter inputFormatter in ODataInputFormatterFactory.Create().Reverse())
                {
                    options.InputFormatters.Insert(0, inputFormatter);
                }

                // Add OData output formatters at index 0, which overrides the built-in json and xml formatters.
                // Add in reverse order at index 0 to preserve order from the factory in the final list.
                foreach (ODataOutputFormatter outputFormatter in ODataOutputFormatterFactory.Create().Reverse())
                {
                    options.OutputFormatters.Insert(0, outputFormatter);
                }

                // Add the value provider.
                // options.ValueProviderFactories.Insert(0, new ODataValueProviderFactory());
            });

            services.Configure(setupAction);

            services.AddODataRouting();

            IODataBuilder builder = new DefaultODataBuilder(services);

            return(builder);
        }