internal static ODataServiceBuilder AddOData(
            [NotNull] this IServiceCollection services)
        {
            services.AddScoped <ODataProperties>();
            services.AddTransient <ODataOptionsSetup>();
//            services.AddTransient<IConfigureOptions<ODataOptions>, ODataOptionsSetup>();
            services.Configure <MvcOptions>(options =>
            {
                options.InputFormatters.Insert(0, new ModernInputFormatter());

                // BUG: the options.OutputFormatters.Insert(0, new ModernOutputFormatter());
                //      line has been uncommented and as a result odata works without it the
                //      exception shown below in thrown
                //
                //  An exception of type 'Microsoft.OData.Core.ODataContentTypeException' occurred in Microsoft.OData.Core.dll
                //  but was not handled in user code Additional information: A supported MIME type could not be found that matches
                //  the content type of the response. None of the supported type(s)  ...

                var outputFormatters = ODataOutputFormatters.Create();
                foreach (var outputFormatter in outputFormatters)
                {
                    options.OutputFormatters.Insert(0, outputFormatter);
                }
                options.OutputFormatters.Insert(0, new ModernOutputFormatter());
            });

            //services.AddSingleton<IActionSelector, ODataActionSelector>();
            services.AddSingleton <IActionSelector, ODataActionSelector>();
            services.AddSingleton <IODataRoutingConvention, DefaultODataRoutingConvention>();
            services.AddSingleton <IETagHandler, DefaultODataETagHandler>();
            services.AddSingleton <IODataPathHandler, DefaultODataPathHandler>();
            return(new ODataServiceBuilder(services));
        }
Esempio n. 2
0
        /// <summary>
        /// Adds essential OData services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>An <see cref="IODataCoreBuilder"/> that can be used to further configure the OData services.</returns>
        public static IODataCoreBuilder AddOData(this IServiceCollection services)
        {
            if (services == null)
            {
                throw Error.ArgumentNull(nameof(services));
            }

            // add the default OData lib services into service collection.
            IContainerBuilder builder = new DefaultContainerBuilder(services);

            builder.AddDefaultODataServices();

            // Options
            services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <ODataOptions>, ODataOptionsSetup>());

            // SerializerProvider
            services.AddSingleton <IODataSerializerProvider, DefaultODataSerializerProvider>();

            // Deserializer provider
            services.AddSingleton <IODataDeserializerProvider, DefaultODataDeserializerProvider>();

            services.AddMvcCore(options =>
            {
                options.InputFormatters.Insert(0, new ModernInputFormatter());

                foreach (var outputFormatter in ODataOutputFormatters.Create())
                {
                    options.OutputFormatters.Insert(0, outputFormatter);
                }
                //options.OutputFormatters.Insert(0, new ModernOutputFormatter());
            });

            services.AddSingleton <IActionSelector, ODataActionSelector>();
            services.AddSingleton <IETagHandler, DefaultODataETagHandler>();
            services.AddSingleton <FilterBinder, FilterBinder>();
            // Routing
            services.AddSingleton <IODataPathHandler, DefaultODataPathHandler>();
            services.AddSingleton <IODataPathTemplateHandler, DefaultODataPathTemplateHandler>();

            // Assembly
            services.AddSingleton <IAssemblyProvider, DefaultAssemblyProvider>();

            // Serializers
            AddDefaultSerializers(services);

            // Deserializers
            AddDefaultDeserializers(services);

            return(new ODataCoreBuilder(services));
        }
        public static ODataServiceBuilder AddOData(
            [NotNull] this IServiceCollection services)
        {
            services.AddScoped <ODataProperties>();
            services.AddTransient <IConfigureOptions <ODataOptions>, ODataOptionsSetup>();
            services.ConfigureMvc(options =>
            {
                options.InputFormatters.Insert(0, new ModernInputFormatter());

                foreach (var outputFormatter in ODataOutputFormatters.Create())
                {
                    options.OutputFormatters.Insert(0, outputFormatter);
                }
                //options.OutputFormatters.Insert(0, new ModernOutputFormatter());
            });

            services.AddSingleton <IActionSelector, ODataActionSelector>();
            services.AddSingleton <IODataRoutingConvention, DefaultODataRoutingConvention>();
            services.AddSingleton <IETagHandler, DefaultODataETagHandler>();
            services.AddSingleton <IODataPathHandler, DefaultODataPathHandler>();
            return(new ODataServiceBuilder(services));
        }
        internal static ODataServiceBuilder AddOData([NotNull] this IServiceCollection services)

        {
            services.AddScoped <ODataProperties>();
            services.AddTransient <ODataOptionsSetup>();
            var currentServices = services.BuildServiceProvider();

            if (currentServices.GetService <IODataOutputFormatterProvider>() == null)
            {
                services.ConfigureODataOutputFormatter <ModernOutputFormatter>();
            }
            if (currentServices.GetService <ODataSerializerProvider>() == null)
            {
                services.ConfigureODataSerializerProvider <DefaultODataSerializerProvider>();
            }
            //            services.AddTransient<IConfigureOptions<ODataOptions>, ODataOptionsSetup>();
            services.Configure <MvcOptions>(options =>
            {
                options.InputFormatters.Insert(0, new ModernInputFormatter());

                var serviceProvider  = services.BuildServiceProvider();
                var outputFormatters = ODataOutputFormatters.Create(
                    serviceProvider.GetService <DefaultODataSerializerProvider>());
                foreach (var odataOutputFormatter in outputFormatters)
                {
                    options.OutputFormatters.Insert(0, odataOutputFormatter);
                }
                options.OutputFormatters.Insert(0, serviceProvider.GetService <IODataOutputFormatterProvider>().OutputFormatter);
            });

            //services.AddSingleton<IActionSelector, ODataActionSelector>();
            services.AddSingleton <IActionSelector, ODataActionSelector>();
            services.AddSingleton <IODataRoutingConvention, DefaultODataRoutingConvention>();
            services.AddSingleton <IETagHandler, DefaultODataETagHandler>();
            services.AddSingleton <IODataPathHandler, DefaultODataPathHandler>();
            return(new ODataServiceBuilder(services));
        }