public void cache_singleton_in_pipeline()
        {
            _cache = new Mock<IApiOutputCache>();

            var conf = new HttpConfiguration();
            conf.CacheOutputConfiguration().RegisterCacheOutputProvider(() => _cache.Object);

            conf.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );

            _server = new HttpServer(conf);

            var client = new HttpClient(_server);
            var result = client.GetAsync(_url + "Get_c100_s100").Result;

            _cache.Verify(s => s.Contains(It.Is<string>(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2));

            var result2 = client.GetAsync(_url + "Get_c100_s100").Result;
            _cache.Verify(s => s.Contains(It.Is<string>(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(4));

            _server.Dispose();
        }
Пример #2
0
        /// <summary>
        /// Registers the specified configuration.
        /// </summary>
        /// <param name="config">The configuration.</param>
        public static void Register(HttpConfiguration config)
        {
            var cacheConfig = config.CacheOutputConfiguration();
            cacheConfig.RegisterCacheOutputProvider(() => new MemoryCacheDefault());

            var filters = GlobalConfiguration.Configuration.Filters;
            filters.Add(new WebApiErrorHandlingFilterAttribute());

            var formatters = GlobalConfiguration.Configuration.Formatters;
            var json = formatters.JsonFormatter;
            json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // A resposta será em JSON quando as chamadas tiverem a extensão .json.
            json.AddUriPathExtensionMapping("json", "application/json");

            // Configura JSON como a resposta padrão.
            json.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

            // Permite que as enumerações seja serializadas como string.
            json.SerializerSettings.Converters.Add(new StringEnumConverter());

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });
        }
        public void cache_instance()
        {
            var conf = new HttpConfiguration();
            conf.CacheOutputConfiguration().RegisterCacheOutputProvider(() => new MemoryCacheDefault());

            object cache1;
            conf.Properties.TryGetValue(typeof(IApiOutputCache), out cache1);

            object cache2;
            conf.Properties.TryGetValue(typeof(IApiOutputCache), out cache2);

            Assert.AreNotSame(((Func<IApiOutputCache>)cache1)(), ((Func<IApiOutputCache>)cache2)());
        }
 protected virtual void EnsureCache(HttpConfiguration config, HttpRequestMessage req)
 {
     _webApiCache = config.CacheOutputConfiguration().GetCacheOutputProvider(req);
 }