0
I am facing a situation where in a large part of the methods of my API I need to send a request for another API to account for calls and so invoice them.
Currently this code is contained in these methods, I wonder if it is possible to reduce this code to an attribute leaving the code more or less as below:
[Faturavel] // Atributo que rodará um código para enviar requisição para outra API
[HttpGet]
[Route("Teste")]
public ActionResult Get() { return Ok(); }
This logic is possible?
Or is there another more appropriate way to implement this solution?
I tried to implement the billing attribute, but when the Get() method is called the http request is not sent.
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class Faturavel: Attribute
{
public Faturavel()
{
using HttpClient HttpClient = new();
using HttpRequestMessage Request = new(HttpMethod.Post, new Uri("http://localhost:44326/teste/ok"));
using HttpResponseMessage Response = HttpClient.Send(Request);
if (Response.StatusCode != System.Net.HttpStatusCode.OK)
throw new ApplicationException("Falha ao integrar!");
}
}
This way I would like when the Get() method is called, to execute the code that is in the Invoicing class, but this is not happening. When I run the project the class is being called 4x and when I do GET nothing happens.
it is not very clear your question... as it is a custom attribute it is possible to program whatever you want in it and put your logic there, so at first, without more details, I would say yes
– Ricardo Pontual
Thanks for the answer Ricardo, I will edit the question trying to make clearer.
– user2782509