Implementation of WEBAPI interface Access frequency control

Source: Internet
Author: User

About the current limit of the article, the blog Park is still quite a lot of. This paper makes an example based on filter current limit, which is a concrete example of using Webapithrottle.

Implementation method:

1. Use NuGet to add webapithrottle references to the WEBAPI project

2, to register, generally in the Webapiconfig register method to add, the code is as follows:

1Config. Filters.add (NewCustomthrottlingfilter ()2             {3Policy =NewThrottlepolicy ()4                 {5                     //IP Configuration Area6Ipthrottling =true,7Clientthrottling =true,8 9                     //The endpoint throttling policy configuration is obtained from the enablethrottling attribute. TenEndpointthrottling =true One                 } A});

Where Customthrottlingfilter is the throttlingfilter that you rewrite, you can also use the default configuration directly. My custom Customthrottlingfilter is as follows:

1      Public classCustomthrottlingfilter:throttlingfilter2     {3         /// <summary>4         ///sets the indentity.5         /// </summary>6         /// <param name= "Request" >The request.</param>7         /// <returns>requestidentity.</returns>8         protected Overriderequestidentity setindentity (httprequestmessage request)9         {Ten             varSessionId =string. Empty; One             Try A             { -                 varRequestcookie =request. Headers.getcookies (). FirstOrDefault (); -                 if(Requestcookie! =NULL) the                 { -                     foreach(varIteminchRequestCookie.Cookies.Where (item = Item). Name = ="session_id")) -                     { -SessionId =item. Value; +                          Break; -                     } +                 } A             } at             Catch(Exception) -             { -SessionId =string. Empty; -             } -             return Newrequestidentity () -             { inClientkey =string. Isnullorwhitespace (sessionId)? SessionId:"Anon", -ClientIP =Base. Getclientip (Request). ToString (), toEndpoint =request. RequestUri.AbsolutePath.ToLowerInvariant () +             }; -         } the}

3, the need to control the interface or controller with a head mark

  )] // Control access frequency up to 12 times per minute

No need to control the frequency of access can not add or add

[Disablethrotting]

This enables the use of filters to control the frequency of access to specific APIs, more use of the method can refer to the following address.

Resources:

Webapithrottle Flow-Limiting framework user manual http://www.cnblogs.com/mushroom/p/4659200.html

Control the frequency and current limit of ASP. http://www.cnblogs.com/Irving/p/4664786.html

control ASP. NET Web API call frequency http://www.cnblogs.com/shanyou/p/3194802.html

Implementation of WEBAPI interface Access frequency control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.