public override void OnActionExecuting(ActionExecutingContext context) { //probably not the right way to do this - must be some way to have the container inject IRateLimiterService service = context.HttpContext.RequestServices.GetService <IRateLimiterService>(); Request request = RateLimiterHelper.CreateRequestFromContext(context); State rateLimitResponse = service.checkRequest(request); RateLimiterHelper.HandleResponse(rateLimitResponse, context); }
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Uri uri = new Uri("http://localhost:3001/check-request"); Request request = RateLimiterHelper.CreateRequestFromContext(context); StringContent content = new StringContent(JsonConvert.SerializeObject(request, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }), Encoding.UTF8, "application/json"); HttpClient client = new HttpClient(); HttpResponseMessage httpResponse = await client.PostAsync(uri, content); String httpContent = await httpResponse.Content.ReadAsStringAsync(); State rateLimitResponse = JsonConvert.DeserializeObject <State>(httpContent); RateLimiterHelper.HandleResponse(rateLimitResponse, context); await next(); }