Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region Codes for backup and use to compare with preview design/implementation
            //services.AddControllers(options => {
            //{
            //    options.Conventions.Add(new MetadataApplicationModelConventionAttribute());
            //    options.Conventions.Add(new MetadataActionModelConvention());
            //});

            /*services.AddConvention<MyConvention>();
             *
             * services.AddOData()
             *  .AddODataRouting(options => options
             *      .AddModel(EdmModelBuilder.GetEdmModel())
             *      .AddModel("v1", EdmModelBuilder.GetEdmModelV1())
             *      .AddModel("v2{data}", EdmModelBuilder.GetEdmModelV2()));
             *
             * services.AddODataFormatter();
             * services.AddODataQuery(options => options.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5));
             */
            #endregion

            services.AddDbContext <MyDataContext>(opt => opt.UseLazyLoadingProxies().UseInMemoryDatabase("MyDataContextList"));

            IEdmModel model0 = EdmModelBuilder.GetEdmModel();
            IEdmModel model1 = EdmModelBuilder.GetEdmModelV1();
            IEdmModel model2 = EdmModelBuilder.GetEdmModelV2();
            IEdmModel model3 = EdmModelBuilder.GetEdmModelV3();

            services.AddControllers()

            /*  If you want to remove $metadata endpoint, you can use ControllerFeatureProvider as follows
             * .ConfigureApplicationPartManager(manager =>
             * {
             *  manager.FeatureProviders.Remove(manager.FeatureProviders.OfType<ControllerFeatureProvider>().FirstOrDefault());
             *  manager.FeatureProviders.Add(new RemoveMetadataControllerFeatureProvider());
             * })
             *
             * or, remove MetadataRoutingConvention in AddOData as
             *   opt.Conventions.Remove(opt.Conventions.First(convention => convention is MetadataRoutingConvention));
             */
            .AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5)
                      .AddRouteComponents(model0)
                      .AddRouteComponents("v1", model1)
                      .AddRouteComponents("v2{data}", model2, services => services.AddSingleton <ODataBatchHandler, DefaultODataBatchHandler>())
                      .AddRouteComponents("v3", model3)
                      .Conventions.Add(new MyConvention())
                      );

            services.AddSwaggerGen();
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddControllers(options => {
            //{
            //    options.Conventions.Add(new MetadataApplicationModelConventionAttribute());
            //    options.Conventions.Add(new MetadataActionModelConvention());
            //});

            /*services.AddConvention<MyConvention>();
             *
             * services.AddOData()
             *  .AddODataRouting(options => options
             *      .AddModel(EdmModelBuilder.GetEdmModel())
             *      .AddModel("v1", EdmModelBuilder.GetEdmModelV1())
             *      .AddModel("v2{data}", EdmModelBuilder.GetEdmModelV2()));
             *
             * services.AddODataFormatter();
             * services.AddODataQuery(options => options.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5));
             */

            services.AddDbContext <MyDataContext>(opt => opt.UseLazyLoadingProxies().UseInMemoryDatabase("MyDataContextList"));

            IEdmModel model0 = EdmModelBuilder.GetEdmModel();
            IEdmModel model1 = EdmModelBuilder.GetEdmModelV1();
            IEdmModel model2 = EdmModelBuilder.GetEdmModelV2();
            IEdmModel model3 = EdmModelBuilder.GetEdmModelV3();

            services.AddControllers()
            .AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5)
                      .AddModel(model0)
                      .AddModel("v1", model1)
                      .AddModel("v2{data}", model2, builder => builder.AddService <ODataBatchHandler, DefaultODataBatchHandler>(Microsoft.OData.ServiceLifetime.Singleton))
                      .AddModel("v3", model3)
                      .Conventions.Add(new MyConvention())
                      );

            services.AddSwaggerGen();
        }