Sometimes requests are denied due to many reasons (like 429 Too Many Request) and it is wise to just retry. The retry can be set on multiple levels, in code (with polly), in service level or in api gateway with a simple policy. In this post I will focus on setting the retry on the Azure Api Gateway.
The following policy retries backend calls up to three times if they fail with certain status codes, waiting 10 seconds between attempts.
The retry block sets the conditions on witch it retries and also how many times and the interval
the forward-request is the important part with the buffer-request-body set to true, as this ensures that when we retry the request, the same body will be sent again. (This was a head-scratcher until we figured it out that is needed)
Adding logging to the retry
To better understand when and why retries happen, you can log each attempt using trace and custom variables. Here’s a more complete example that tracks retry counts and logs the retried operation.
When building an agent with Azure AI Foundry, you’ll often need to look back at the conversation so far. Whether you’re debugging, showing it for reference, or implementing agent “memory” fetching thread message history is essential.
fromazure.ai.projects.aioimportAIProjectClientfromazure.identity.aioimportDefaultAzureCredentialclient=AIProjectClient(endpoint="Endpoint of your Azure Ai Foundry project",credential=DefaultAzureCredential())
Get thread ID
To fetch history, you need a thread ID. You can either persist it when creating threads in code or find it in the Azure AI Foundry portal:
List all messages in a thread
The simplest way is to iterate over all messages with the async list method:
The limit parameter is confusing. It does not cap the total number of messages returned—it only controls how many items are retrieved per API call. For example, limit=3 still fetches the entire history, just in smaller batches.
To truly process a limited number of messages, use paging and break early:
messages=agents_client.messages.list(thread_id=thread_id,limit=3)fori,pageinenumerate(messages.by_page()):print(f"Items on page {i}")formessageinpage:print(message.id)# break after first page if only X items are needed
this will produce something like this:
Items on page 0
msg_1
msg_2
msg_3
Items on page 1
msg_4
msg_5
msg_6
Items on page 2
msg_7
If you only want the first N messages, you can exit the loop after processing the desired count.
Entity Framework (EF) is a powerful Object-Relational Mapping (ORM) framework for .NET applications. It simplifies data manipulation, but without proper query optimization, EF can lead to suboptimal performance. This post explores two examples of EF query optimization in C# to enhance application efficiency and response times.
Making queries better
In the first example, the query is made with Any(), but EF interprets the query more complexly than it should and generates suboptimal SQL code for our case.
After checking the SQL value that shows what the EF will execute against the DB, we can experiment with different ways to write the query. Rewriting this example to Contains() generated a much simpler SQL for our case, hence better performance.
Takeaway: Take a look at the SQL the EF generates from the queries that you use in order to gain performance upgrades.
Best Practices for Query Optimization in Entity Framework
Use Projections: Avoid retrieving entire entities if only specific fields are needed. Use .Select() to retrieve only the necessary data.
Filter at the Database Level: Whenever possible, apply filters directly in the query rather than retrieving data and filtering in memory.
Be Mindful of Lazy Loading: Lazy loading can cause performance issues due to multiple round trips to the database. Consider eager loading (using .Include()) when you know related data will be needed.
Use AsNoTracking for Read-Only Data: If the data you’re retrieving won’t be updated in the current context, using .AsNoTracking() can improve performance because EF doesn’t need to track changes.
Benchmark and Profile: Always measure performance changes after optimizations. Use profiling tools to identify slow queries and bottlenecks.
Călătoria într-o destinație exotică poate fi o aventură palpitantă. De la explorarea unor culturi noi până la descoperirea unor locuri ascunse, există atât de multe lucruri de așteptat. Totuși, o provocare neașteptată poate să îți complice planurile: menținerea conexiunii la rețeaua mobilă. În unele țări, tipurile de rețea disponibile (de exemplu, 5G, 4G sau 3G) pot diferi de ceea ce ești obișnuit acasă. Cunoașterea modului de ajustare a setărilor de rețea ale dispozitivului tău te poate scuti de frustrarea de a rămâne fără semnal.
Înțelegerea compatibilității rețelelor
Operatorii de telefonie mobilă din întreaga lume operează pe benzi și tehnologii diferite. De exemplu:
Unele regiuni se bazează încă pe 3G, în timp ce altele sunt complet echipate cu 5G.
Operatorul tău s-ar putea să nu aibă acorduri cu furnizorii locali pentru anumite tipuri de rețea.
Anumite benzi utilizate în destinația ta ar putea să nu se potrivească cu cele suportate de dispozitivul tău.
Aceste variații înseamnă că setările tale de rețea implicite, proiectate pentru țara ta de origine, s-ar putea să nu funcționeze fără probleme în străinătate.
Cum să ajustezi setările de rețea
Când te afli fără semnal într-o destinație exotică, urmează acești pași pentru a rezolva problema:
Accesează setările de rețea
Pe majoritatea smartphone-urilor, accesează Setări > Rețele mobile sau Conexiuni > Mod rețea.
Schimbă tipul preferat de rețea
Dacă dispozitivul tău este setat să prioritizeze 5G, comută la 4G sau 3G. Unele dispozitive îți permit să selectezi opțiunea „Auto” sau să alegi manual între opțiunile disponibile.
Repornește dispozitivul
Uneori, o simplă repornire este tot ce ai nevoie pentru a te reconecta la o rețea compatibilă.
Selectează manual o rețea
Accesează Setări > Rețele mobile > Operatori de rețea și scanează rețelele disponibile. Alege una care oferă cel mai puternic semnal.
Activează roamingul
Dacă planul tău include roaming internațional, asigură-te că acesta este activat în Setări > Date mobile > Roaming de date.
De ce este important acest lucru
Rămânerea conectat nu ține doar de comoditate; este adesea o problemă de siguranță. Fie că ai nevoie să te orientezi folosind hărțile, să contactezi persoane dragi sau să ceri ajutor în caz de urgență, o conexiune mobilă fiabilă este esențială. Înțelegând și ajustând setările de rețea, poți călători fără întreruperi inutile.
Sfat util: Înainte de călătorie, verifică împreună cu operatorul tău rețelele suportate și acordurile de roaming în țara de destinație. Unii operatori oferă aplicații sau servicii care simplifică aceste ajustări.
Un exemplu specific este operatorul Digi (RCS-RDS). Când călătorești, este posibil să observi că, deși rețelele sunt disponibile, telefonul tău nu se conectează la niciuna. În acest caz, este esențial să setezi manual tipul de rețea și să aștepți să vezi dacă se conectează. De exemplu, încearcă să comuți la 3G dacă 4G nu funcționează. Uneori, această abordare de încercare și eroare este necesară pentru a găsi o rețea compatibilă.
Concluzie
Călătoria într-o țară mai exotică nu înseamnă că trebuie să pierzi legătura cu lumea digitală. Fiind proactiv și știind cum să ajustezi setările de rețea, poți să te asiguri că vei rămâne conectat, indiferent unde te duc aventurile tale. Călătorii plăcute!