Very slow EF Core with Azure Functions when try block is added

huangapple go评论69阅读模式
英文:

Very slow EF Core with Azure Functions when try block is added

问题

Here's the translated code part:

这是主要的触发HTTPAzure Function)代码,它访问不同的函数以获取数据,并将其构建成JSON响应,然后通过try块将JSON发送到postman

public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
    try
    {
        var GetCuurency = await GetCurrencyAsync();
        var GetUnderwriters = await GetUnderWritersAsync();
        var GetSubmissionType = await GetSubmissionsTypes();
        var dynamicOptions = new
        {
            Underwriter = GetUnderwriters,
            Currency = GetCuurency,
            Submission_Type = GetSubmissionType
        };

        var response = req.CreateResponse(HttpStatusCode.OK);
        await response.WriteAsJsonAsync(dynamicOptions);

        return response;
    }
    catch (Exception ex)
    {
        var error = new
        {
            Error = ex.Message
        };

        var response = req.CreateResponse(HttpStatusCode.InternalServerError);
        await response.WriteAsJsonAsync(error, response.StatusCode);
        return response;
    }
}

Please note that I've only provided the translation of the code portion, as requested. If you have any specific questions or need further assistance with this code, please let me know.

英文:

I have the main trigger HTTP (Azure Function) code here that goes to different functions to get data and builds it up into a JSON response and sends a JSON to postman via a try block :

public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
        {

        try
        {
            var GetCuurency = await GetCurrencyAsync();
            var GetUnderwriters = await GetUnderWritersAsync();
            var GetSubmissionType = await GetSubmissionsTypes();
            var dynamicOptions = new
            {
                Underwriter = GetUnderwriters,
                Currency = GetCuurency,
                Submission_Type = GetSubmissionType
            };

            var response = req.CreateResponse(HttpStatusCode.OK);
            await response.WriteAsJsonAsync(dynamicOptions);


            return response;
        }
        catch (Exception ex)
        {
            var error = new
            {
                Error = ex.Message
            };

            var response = req.CreateResponse(HttpStatusCode.InternalServerError);
            await response.WriteAsJsonAsync(error, response.StatusCode);
            return response;
        }
    }

So the awaits at the top of the try goes a function that calls a ef core function. And it returns data via a custom DTO model - like below :

public async Task<List<DTO.Currency_DTO>> GetCurrencyAsync()
        { 
             
                return await _ripple_primary_context.RipplCurrencies.Select(p => new DTO.Currency_DTO
                {
                    currency_id = p.CurrencyId,
                    currency_name = p.CurrencyName,
                    currency_alpha_code = p.CurrencyAlphaCode

                    }).AsNoTracking().ToListAsync();
  

        }

As you can see i have put them all in a await and performed them all async. the three methods are generally the same just targeting different tables.

if i dont put the try method in , I get the results back in 3 secs (locally) so thats reasonable. but if i put the try block in and for example change the sql server name to be wrong then the error comes but it takes around 30 seconds which is terrible. How can i optmise this code so the error comes back much quicker.

Considered doing only one call to SQL (i.e only one method but then i have problems with multiple dto's.

Bit confused how to optimise this - any ideas would be amazing.

答案1

得分: 3

问题出在你的测试方式上,而不是你正在测试的代码上。你的代码中没有任何会导致30秒延迟的问题。延迟是在你通过更改 SQL Server 的名称来测试时发生的,因为当它尝试连接到不存在的 SQL Server 实例时,需要30秒才会超时。

英文:

The problem is in how you are testing it, not in the code you are testing. There is nothing in your code that will cause a 30 second delay. The delay is occurring when you test it by changing the name of SQL Server because it is taking 30 seconds to time out when it attempts to connect to a non-existent instance of SQL Server.

huangapple
  • 本文由 发表于 2023年7月6日 17:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定