Multiple requests for an API result in 500 Internal Server error

Asked

Viewed 19 times

0

I have an API that makes two requests on Microsoft services (POST and GET) that receive a code and bring some information about employees.

public async Task<string> AdquirirEmail(string codeAD)
    {
        var AcToken = await HttpClientPost(codeAD);

        var emailAdquirido = await HttpClientGet(AcToken);

        return emailAdquirido;
    }


 private static async Task<string> HttpClientPost(string codeAD)
    {
        var url = "https://login.microsoftonline.com/...";

        var dic = new Dictionary<string, string>();

        //PRODUÇÃO
        dic.Add("client_id", "MeuClientID");
        dic.Add("scope_user", "user.read%20mail.read");
        dic.Add("code", codeAD);
        dic.Add("redirect_uri", "URL");
        dic.Add("grant_type", "authorization_code");
        dic.Add("client_secret", "MeuClientSecret");

        var content = new FormUrlEncodedContent(dic);

        try
        {
            var response = await clientPost.PostAsync(url, content);

            var responseString = await response.Content.ReadAsStringAsync();

            var convertModel = JsonConvert.DeserializeObject<PostResponse>(responseString);

            var acToken = convertModel.Access_token;

            return acToken;
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

 //Request GET
    private static async Task<string> HttpClientGet(string acToken)
    {
        if (clientGet.BaseAddress == null)
        {
            clientGet.BaseAddress = new Uri("https://graph.microsoft.com/v1.0/");
        }
        
        var token = acToken;
        clientGet.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        var responseTask = clientGet.GetAsync("me");
        responseTask.Wait();
        var result = responseTask.Result;

        try
        {
            var readTask = await result.Content.ReadAsStringAsync();
            var convertModel = JsonConvert.DeserializeObject<GetResponse>(readTask);

            var email = convertModel.Mail;

            return email;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

With this code I can perform multiple requests in development and homologation servers without problems, but when it arrives at the production server, I can do the first one, already in the second one it takes approximately 1m40s and brings "The Operation Was Canceled"it only works if I have an interval of approximately 1 minute from one to the other: Requisição 1

Requisição 2

Does anyone know what can be prevented from multiple requests on the server?

  • already looked if it has application logs with some error or any hint of the problem?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.