How to limit number of requests to ServiceStack per day?


Is there a way to limit number of requests to specific API (or to a server) per day?

Since there is a mechanism that does it for unlicensed users, maybe it’s possible to make use of it?

Thank you

Maybe using a GlobalRequestFilter with a static counter something like…

public class AppHost : AppHostBase
    private static int globalCounter = 0;
    private const int globalCounterMax = 1000;

    public override void Configure(Funq.Container container)
        this.GlobalRequestFilters.Add((req, res, dto) =>
            if(++globalCounter > globalCounterMax)
                throw new HttpError(System.Net.HttpStatusCode.Forbidden, new Exception("Maximum number of daily requests exceeded."));

That’s off the cuff, so I’d test it for sure. :slight_smile: Also, you’ll need some way of resetting it at the start of each day.

1 Like

Thank you, Robert.

Indeed, it can be a good solution. I wander if I can reuse the license protection solution that ServiceStack already has. If not, then this way is probably the most simple and safe.

I can reset the counter by maintaining a date in addition to the counter.

Yep you could reset counter in Roberts example using a Timer that reset the counter at a specified interval.
These posts on API Throttling and Sliding expirations may also be relevant:

No that wont be useful (or is reusable).

1 Like