admin管理员组文章数量:1300135
Code works on local machine in development environment but not on webapp, in azure.
Initially i was loading a json file from a local directoty... didnt work now i a fetch json string and convert it into objects. so its not local file reading issue.
it seems to hang some where when it loads string and converts into list.
it has 344 entries.
I am on Shared D1 with 1GB memory.
public async Task<List<SeruQuestion>> GetSeruQuestions()
{
var builder = QueryBuilder<System.Object>.New
.ContentTypeIs("service")
.FieldEquals("fields.name", "seru");
var result = await _client.GetEntries(builder);
var firstEntry = result.FirstOrDefault();
if (firstEntry == null)
{
return new List<SeruQuestion>(); // Return empty list if no entries are found
}
// Parse JSON data safely
if (!(firstEntry is JObject jsonData) || jsonData["data"] == null)
{
return new List<SeruQuestion>();
}
var items = jsonData["data"].ToObject<List<JObject>>();
var seruQuestions = items.Select(item => new SeruQuestion
{
QuestionText = item["QuestionText"]?.ToString(),
Options = item["Options"]?.ToObject<List<string>>() ?? new List<string>(),
CorrectAnswers = item["CorrectAnswers"]?.ToObject<List<string>>() ?? new List<string>()
}).ToList();
return seruQuestions;
}
Code works on local machine in development environment but not on webapp, in azure.
Initially i was loading a json file from a local directoty... didnt work now i a fetch json string and convert it into objects. so its not local file reading issue.
it seems to hang some where when it loads string and converts into list.
it has 344 entries.
I am on Shared D1 with 1GB memory.
public async Task<List<SeruQuestion>> GetSeruQuestions()
{
var builder = QueryBuilder<System.Object>.New
.ContentTypeIs("service")
.FieldEquals("fields.name", "seru");
var result = await _client.GetEntries(builder);
var firstEntry = result.FirstOrDefault();
if (firstEntry == null)
{
return new List<SeruQuestion>(); // Return empty list if no entries are found
}
// Parse JSON data safely
if (!(firstEntry is JObject jsonData) || jsonData["data"] == null)
{
return new List<SeruQuestion>();
}
var items = jsonData["data"].ToObject<List<JObject>>();
var seruQuestions = items.Select(item => new SeruQuestion
{
QuestionText = item["QuestionText"]?.ToString(),
Options = item["Options"]?.ToObject<List<string>>() ?? new List<string>(),
CorrectAnswers = item["CorrectAnswers"]?.ToObject<List<string>>() ?? new List<string>()
}).ToList();
return seruQuestions;
}
Share
edited Feb 20 at 9:05
qkfang
1,7851 silver badge20 bronze badges
asked Feb 11 at 13:48
AsadAsad
21.9k17 gold badges72 silver badges94 bronze badges
3
|
1 Answer
Reset to default 0Code works on local machine in development environment but not on webapp, in azure.
- The issue you're facing might be due to Shared D1 plan has limited memory and also parsing a large JSON object (344 entries) may cause performance issues.
- The
_client.GetEntries(builder)
call might taking too long that leading to a timeout.
To test if your issue is due to resource limitations, upgrade your Azure App Service Plan to Basic B1 or high and check if your app becomes responsive.
Also, you can optimize it in chunks using JsonSerializer
and StreamReader
instead of loading it all at once.
- Please refer this MSdoc1, Msdoc2 for better understanding about Streamreader, JsonSerializer respectively.
Sample code:
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
public async Task<List<SeruQuestion>> GetSeruQuestions()
{
var builder = QueryBuilder<System.Object>.New
.ContentTypeIs("service")
.FieldEquals("fields.name", "seru");
var result = await _client.GetEntries(builder);
var firstEntry = result.FirstOrDefault();
if (firstEntry == null)
{
return new List<SeruQuestion>();
}
string jsonString = firstEntry.ToString();
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonString));
using var reader = new StreamReader(stream);
using var jsonReader = new Utf8JsonReader(await reader.ReadToEndAsync().AsMemory());
var seruQuestions = new List<SeruQuestion>();
await foreach (var question in JsonSerializer.DeserializeAsyncEnumerable<SeruQuestion>(stream))
{
if (question != null)
{
seruQuestions.Add(question);
}
}
return seruQuestions;
}
- If
_client
is an HttpClient Instance, you can increase the timeout to prevent request failures due to long execution times.
_client.Timeout = TimeSpan.FromSeconds(60);
- If possible, use
caching (Redis, in-memory cache)
to avoid repeated API calls.
本文标签: web applicationsWhy this code block is unresponsive in azureStack Overflow
版权声明:本文标题:web applications - Why this code block is unresponsive in azure - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741659974a2390977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
SeruQuestion
class definition and a snippet of the JSON (maybe just one or two records)? It feels like there's a more efficient way to achieve what you want, and it would be easier to see with those extra details. Thanks. – Andrew B Commented Feb 14 at 20:50