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
  • Please provide your error. – Aslesha Kantamsetti Commented Feb 11 at 14:01
  • The Shared D1 plan has only 1GB RAM. If your JSON data is large, it may be causing high memory usage, leading to slow performance or failures. – Aslesha Kantamsetti Commented Feb 12 at 5:34
  • Can you share the 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
Add a comment  | 

1 Answer 1

Reset to default 0

Code 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