示例#1
0
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

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

            //trace provider
            var traceWriter = new SystemDiagnosticsTraceWriter()
            {
                IsVerbose = true
            };
            config.Services.Replace(typeof(ITraceWriter), traceWriter);
            config.EnableSystemDiagnosticsTracing();

            //Web API throttling
            config.MessageHandlers.Add(new ThrottlingHandler()
            {
                Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 30, perDay: 35)
                {
                    //scope to IPs
                    IpThrottling = true,
                    IpRules = new Dictionary<string, RateLimits>
                    {
                        { "::1/10", new RateLimits { PerSecond = 2 } },
                        { "192.168.2.1", new RateLimits { PerMinute = 30, PerHour = 30*60, PerDay = 30*60*24 } }
                    },
                    //white list the "::1" IP to disable throttling on localhost for Win8
                    IpWhitelist = new List<string> { "127.0.0.1", "192.168.0.0/24" },

                    //scope to clients (if IP throttling is applied then the scope becomes a combination of IP and client key)
                    ClientThrottling = true,
                    ClientRules = new Dictionary<string, RateLimits>
                    {
                        { "api-client-key-1", new RateLimits { PerMinute = 60, PerHour = 600 } },
                        { "api-client-key-9", new RateLimits { PerDay = 5000 } }
                    },
                    //white list API keys that don’t require throttling
                    ClientWhitelist = new List<string> { "admin-key" },

                    //scope to routes (IP + Client Key + Request URL)
                    EndpointThrottling = true,
                    EndpointRules = new Dictionary<string, RateLimits>
                    {
                        { "api/values/", new RateLimits { PerSecond = 3 } },
                        { "api/values", new RateLimits { PerSecond = 4 } }
                    }
                },
                Repository = new CacheRepository(),
                Logger = new TracingThrottleLogger(traceWriter)
            });
        }
        public void Initialize(HttpControllerSettings controllerSettings,
            HttpControllerDescriptor controllerDescriptor)
        {
            var traceWriter =
                new SystemDiagnosticsTraceWriter()
                {
                    MinimumLevel = TraceLevel.Info,
                    IsVerbose = false
                };

            controllerSettings.Services.Replace(typeof(ITraceWriter), traceWriter);
        }
示例#3
0
        /// <summary>
        /// Registers the <see cref="ITraceWriter"/> implementation to use
        /// for this application.
        /// </summary>
        /// <param name="configuration">The <see cref="HttpConfiguration"/> in which
        /// to register the trace writer.</param>
        public static void Register(HttpConfiguration configuration)
        {
            if (configuration == null) {
                throw new ArgumentNullException("configuration");
            }

            var traceWriter = new SystemDiagnosticsTraceWriter() {
                MinimumLevel = TraceLevel.Info,
                IsVerbose = false
            };

            configuration.Services.Replace(typeof(ITraceWriter), traceWriter);
        }
示例#4
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.EnableSystemDiagnosticsTracing();
            SystemDiagnosticsTraceWriter writer = new SystemDiagnosticsTraceWriter()
            {
                MinimumLevel = TraceLevel.Info,
                IsVerbose = false
            };

            GlobalConfiguration.Configuration.Formatters.Insert(0, new TextMediaTypeFormatter());

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
示例#5
0
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

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

            //trace provider
            var traceWriter = new SystemDiagnosticsTraceWriter()
            {
                IsVerbose = true
            };
            config.Services.Replace(typeof(ITraceWriter), traceWriter);
            config.EnableSystemDiagnosticsTracing();

            //Web API throttling handler
            config.MessageHandlers.Add(new ThrottlingHandler(
                policy: new ThrottlePolicy(perMinute: 20, perHour: 30, perDay: 35, perWeek: 3000)
                {
                    //scope to IPs
                    IpThrottling = true,
                    IpRules = new Dictionary<string, RateLimits>
                    { 
                        { "::1/10", new RateLimits { PerSecond = 2 } },
                        { "192.168.2.1", new RateLimits { PerMinute = 30, PerHour = 30*60, PerDay = 30*60*24 } }
                    },
                    //white list the "::1" IP to disable throttling on localhost for Win8
                    IpWhitelist = new List<string> { "127.0.0.1", "192.168.0.0/24" },

                    //scope to clients (if IP throttling is applied then the scope becomes a combination of IP and client key)
                    ClientThrottling = true,
                    ClientRules = new Dictionary<string, RateLimits>
                    { 
                        { "api-client-key-1", new RateLimits { PerMinute = 60, PerHour = 600 } },
                        { "api-client-key-9", new RateLimits { PerDay = 5000 } }
                    },
                    //white list API keys that don’t require throttling
                    ClientWhitelist = new List<string> { "admin-key" },

                    //scope to endpoints
                    EndpointThrottling = true,
                    EndpointRules = new Dictionary<string, RateLimits>
                    { 
                        { "api/search", new RateLimits { PerSecond = 10, PerMinute = 100, PerHour = 1000 } }
                    }
                },
                policyRepository: new PolicyCacheRepository(),
                repository: new CacheRepository(),
                logger: new TracingThrottleLogger(traceWriter),
                ipAddressParser: new CustomIpAddressParser()));

            //Web API throttling handler load policy from web.config
            //config.MessageHandlers.Add(new ThrottlingHandler(
            //    policy: ThrottlePolicy.FromStore(new PolicyConfigurationProvider()),
            //    policyRepository: new PolicyCacheRepository(),
            //    repository: new CacheRepository(),
            //    logger: new TracingThrottleLogger(traceWriter)));

            //Web API throttling filter
            //config.Filters.Add(new ThrottlingFilter(
            //    policy: new ThrottlePolicy(perMinute: 20, perHour: 30, perDay: 35, perWeek: 3000)
            //    {
            //        //scope to IPs
            //        IpThrottling = true,
            //        IpRules = new Dictionary<string, RateLimits>
            //        { 
            //            { "::1/10", new RateLimits { PerSecond = 2 } },
            //            { "192.168.2.1", new RateLimits { PerMinute = 30, PerHour = 30*60, PerDay = 30*60*24 } }
            //        },
            //        //white list the "::1" IP to disable throttling on localhost for Win8
            //        IpWhitelist = new List<string> { "127.0.0.1", "192.168.0.0/24" },

            //        //scope to clients (if IP throttling is applied then the scope becomes a combination of IP and client key)
            //        ClientThrottling = true,
            //        ClientRules = new Dictionary<string, RateLimits>
            //        { 
            //            { "api-client-key-1", new RateLimits { PerMinute = 60, PerHour = 600 } },
            //            { "api-client-key-9", new RateLimits { PerDay = 5000 } }
            //        },
            //        //white list API keys that don’t require throttling
            //        ClientWhitelist = new List<string> { "admin-key" },

            //        //Endpoint rate limits will be loaded from EnableThrottling attribute
            //        EndpointThrottling = true
            //    },
            //    policyRepository: new PolicyCacheRepository(),
            //    repository: new CacheRepository(),
            //    logger: new TracingThrottleLogger(traceWriter)));

            //Web API throttling filter load policy from web.config
            //config.Filters.Add(new ThrottlingFilter(           
            //    policy: ThrottlePolicy.FromStore(new PolicyConfigurationProvider()),
            //    policyRepository: new PolicyCacheRepository(),
            //    repository: new CacheRepository(),
            //    logger: new TracingThrottleLogger(traceWriter)));
        }