/// <summary>
        /// Create required components to use Flatwhite cache for WebApi
        /// </summary>
        /// <param name="config"></param>
        /// <param name="flatwhiteConfig"></param>
        public static HttpConfiguration UseFlatwhiteCache(this HttpConfiguration config, FlatwhiteWebApiConfiguration flatwhiteConfig = null)
        {
            _fwConfig = flatwhiteConfig ?? new FlatwhiteWebApiConfiguration();
            Global.CacheStrategyProvider = new WebApiCacheStrategyProvider();
            Global.BackgroundTaskManager = new RegisteredTasks();

            var allHandlers = config.DependencyResolver
                .GetServices(typeof(ICachControlHeaderHandler))
                .OfType<ICachControlHeaderHandler>()
                .ToList();

            if (allHandlers.All(h => h.GetType() != typeof(EtagHeaderHandler)))
            {
                var cacheResponseBuilder = config.DependencyResolver.GetService(typeof(ICacheResponseBuilder)) as ICacheResponseBuilder
                                         ?? new CacheResponseBuilder();
                var etagHeaderHandler = new EtagHeaderHandler(cacheResponseBuilder);
                allHandlers.Add(etagHeaderHandler);
            }

            var handlerProvider = config.DependencyResolver.GetService(typeof(ICachControlHeaderHandlerProvider)) as ICachControlHeaderHandlerProvider
                                ?? new CachControlHeaderHandlerProvider();
            allHandlers.ForEach(h => handlerProvider.Register(h));

            config.MessageHandlers.Add(new CacheMessageHandler(handlerProvider));
            if (_fwConfig.EnableStatusController)
            {
                config.Routes.MapHttpRoute(
                    name: "_FlatwhiteStatus",
                    routeTemplate: "_flatwhite/{action}/{id}",
                    defaults: new {id = RouteParameter.Optional, controller = "FlatwhiteStatus"}
                    );
            }
            return config;
        }
Esempio n. 2
0
        /// <summary>
        /// Create required components to use Flatwhite cache for WebApi
        /// </summary>
        /// <param name="config"></param>
        /// <param name="flatwhiteConfig"></param>
        public static HttpConfiguration UseFlatwhiteCache(this HttpConfiguration config, FlatwhiteWebApiConfiguration flatwhiteConfig = null)
        {
            _fwConfig = flatwhiteConfig ?? new FlatwhiteWebApiConfiguration();
            Global.CacheStrategyProvider = new WebApiCacheStrategyProvider();
            Global.BackgroundTaskManager = new RegisteredTasks();

            var allHandlers = config.DependencyResolver
                              .GetServices(typeof(ICachControlHeaderHandler))
                              .OfType <ICachControlHeaderHandler>()
                              .ToList();

            if (allHandlers.All(h => h.GetType() != typeof(EtagHeaderHandler)))
            {
                var cacheResponseBuilder = config.DependencyResolver.GetService(typeof(ICacheResponseBuilder)) as ICacheResponseBuilder
                                           ?? new CacheResponseBuilder();
                var etagHeaderHandler = new EtagHeaderHandler(cacheResponseBuilder);
                allHandlers.Add(etagHeaderHandler);
            }

            var handlerProvider = config.DependencyResolver.GetService(typeof(ICachControlHeaderHandlerProvider)) as ICachControlHeaderHandlerProvider
                                  ?? new CachControlHeaderHandlerProvider();

            allHandlers.ForEach(h => handlerProvider.Register(h));

            config.MessageHandlers.Add(new CacheMessageHandler(handlerProvider));
            if (_fwConfig.EnableStatusController)
            {
                config.Routes.MapHttpRoute(
                    name: "_FlatwhiteStatus",
                    routeTemplate: "_flatwhite/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional, controller = "FlatwhiteStatus" }
                    );
            }
            return(config);
        }