Пример #1
0
        protected internal virtual IController GetControllerInstance(
            RequestContext requestContext,
            Type controllerType
            )
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }
            if (controllerType == null)
            {
                throw new HttpException(
                          404,
                          String.Format(
                              CultureInfo.CurrentCulture,
                              MvcResources.DefaultControllerFactory_NoControllerFound,
                              requestContext.HttpContext.Request.Path
                              )
                          );
            }
            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              MvcResources.DefaultControllerFactory_TypeDoesNotSubclassControllerBase,
                              controllerType
                              ),
                          "controllerType"
                          );
            }

            return(ControllerActivator.Create(requestContext, controllerType));
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddLogging();

            // configuration
            services.AddOptions();
            services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));

            // relevant for chapter 7
            var ctrlActivator = new ControllerActivator(Configuration);

            services.AddSingleton <IControllerActivator>(ctrlActivator);
            services.AddSingleton <ControllerActivator>(ctrlActivator);

            services.AddSwaggerGen();
            //services.ConfigureSwaggerGen(options =>
            //{
            //   options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info
            //   {
            //      Version = "v1",
            //      Title = "Examples",
            //      Description = "Examples for Functional Programming in C#",
            //   });
            //   //options.IncludeXmlComments(pathToDoc);
            //   options.DescribeAllEnumsAsStrings();
            //});
        }
Пример #3
0
        public static HttpConfiguration Configure(IODataRepository oDataRepository, IMappingLogger logger)
        {
            var config              = new HttpConfiguration();
            var routeFactory        = new RouteFactory(oDataRepository, logger);
            var controllerActivator = new ControllerActivator(
                oDataRepository,
                config.Services.GetService(typeof(IHttpControllerActivator)).As <IHttpControllerActivator>());

            ConfigureServices(config, controllerActivator);
            ConfigureRoutes(config, routeFactory);
            ConfigureBindings(config);

            config.AddODataQueryFilter();
            return(config);
        }
Пример #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ControllerActivator ctrlActivator)
        {
            ctrlActivator.loggerFactory = loggerFactory;

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            //var routeBuilder = new RouteBuilder(app);
            //routeBuilder.MapRoute("echo", (context) =>
            //{
            //   var body = new StreamReader(context.Request.Body).ReadToEnd();

            //   // context.GetRouteData().Values
            //   return context.Response.WriteAsync(body);
            //});

            //app.UseRouter(routeBuilder.Build());

            var useCases = new UseCaseFactory(Configuration, loggerFactory);

            // demonstrates how you can just have all your logic live in functions;
            // but this fails to provide many niceties you get when using a Controller
            app.Map("/api/transferOn", a => a.Run(async ctx =>
            {
                BookTransfer transfer = await Parse <BookTransfer>(ctx.Request.Body);
                IActionResult result  = useCases.PersistTransferOn()(transfer);
                await WriteResponse(ctx.Response, result);
            }));

            app.UseMvcWithDefaultRoute();

            app.UseSwagger();
            app.UseSwaggerUi();
        }