英文:
Very slow EF Core with Azure Functions when try block is added
问题
Here's the translated code part:
这是主要的触发HTTP(Azure 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论