<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Riddhi Shah on Medium]]></title>
        <description><![CDATA[Stories by Riddhi Shah on Medium]]></description>
        <link>https://medium.com/@shahriddhi717?source=rss-71e6b4872b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6LqLWymtZOFBdOMw</url>
            <title>Stories by Riddhi Shah on Medium</title>
            <link>https://medium.com/@shahriddhi717?source=rss-71e6b4872b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:24:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shahriddhi717/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Practical Guide to LINQ in .NET: From Queries to Execution]]></title>
            <link>https://medium.com/@shahriddhi717/a-practical-guide-to-linq-in-net-from-queries-to-execution-9bc889472b2b?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bc889472b2b</guid>
            <category><![CDATA[linq]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Tue, 27 Jan 2026 15:02:15 GMT</pubDate>
            <atom:updated>2026-01-27T15:02:15.265Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Practical Guide to LINQ in .NET: From Queries to Execution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TUUCP7NpnuranqtltNCEgQ.png" /><figcaption>Image Created by Nano Banana</figcaption></figure><p>Table of Contents</p><ol><li>What LINQ Is and What It’s Designed For.</li><li>How LINQ Works Under the Hood.</li><li>Deferred Execution in LINQ.</li><li>Performance and Readability Trade-offs in LINQ.</li><li>Common LINQ Anti-Patterns and Final Thoughts</li></ol><h4><strong>What LINQ Is and What It’s Designed For</strong></h4><p>Language Integrated Query (LINQ) is a set of language features and APIs in .NET that allows you to <strong>query data using a consistent, type-safe syntax directly in C#</strong>. Rather than being a single library, LINQ is a programming model that combines <strong>language syntax</strong>, <strong>compiler support</strong>, and <strong>standard query operators</strong>.</p><p>At a high level, LINQ was designed to solve a common problem in application development: <strong>working with data from different sources using different paradigms</strong>. Before LINQ, querying in-memory collections, databases, XML, or other data sources required entirely different APIs and mental models. LINQ unifies these under a single approach.</p><p>LINQ is designed to be used when:</p><ul><li>You want to <strong>filter, project, group, order, or aggregate data</strong></li><li>You want code that expresses <strong>intent</strong> rather than control flow</li><li>You want <strong>composable queries</strong> that can be built incrementally</li><li>You want <strong>compile-time type checking</strong> instead of string-based queries</li></ul><p>Importantly, LINQ is <strong>declarative</strong>, not imperative. Instead of describing <em>how</em> to iterate through data step by step, LINQ lets you describe <em>what</em> result you want. This shift enables clearer code, easier refactoring, and better reasoning about data transformations.</p><p>LINQ operates over abstractions such as IEnumerable&lt;T&gt; and IQueryable&lt;T&gt;, which allows the same query shape to work across:</p><ul><li>in-memory collections (LINQ to Objects)</li><li>databases via ORMs (for example, LINQ to Entities)</li><li>XML, JSON, and other providers</li></ul><p>However, LINQ is not a replacement for all looping or data-processing logic. It is best suited for <strong>query-like operations</strong> — situations where data is being transformed from one shape to another. For algorithms with complex state, heavy mutation, or tight performance constraints, traditional loops may be more appropriate.</p><p>Understanding what LINQ is designed for — and what it is not — sets the foundation for using it intentionally. The sections that follow explain <strong>how LINQ works</strong>, <strong>when queries execute</strong>, and <strong>where misuse can lead to performance or correctness issues</strong>.</p><h4><strong>How LINQ Works Under the Hood.</strong></h4><p>LINQ works by combining <strong>language features</strong>, <strong>compiler transformations</strong>, and <strong>standard query operators</strong> to construct queries in a uniform way across different data sources.</p><p>At the center of LINQ are two core abstractions:</p><ul><li><strong>IEnumerable&lt;T&gt;</strong>, which represents in-memory sequences</li><li><strong>IQueryable&lt;T&gt;</strong>, which represents queryable data sources backed by a provider (such as a database)</li></ul><p>When LINQ operates on IEnumerable&lt;T&gt;, the query logic is implemented as <strong>extension methods</strong> that operate directly in-process. These methods define how elements are filtered, projected, or transformed as they are iterated.</p><p>When LINQ operates on IQueryable&lt;T&gt;, the query is not executed directly. Instead, LINQ builds an <strong>expression tree</strong> that represents the query structure. This expression tree is passed to a provider, which is responsible for translating it into another form—such as SQL—before execution.</p><p>LINQ supports both <strong>query syntax</strong> and <strong>fluent syntax</strong>, but these are purely syntactic differences. The C# compiler rewrites query syntax (from, where, select) into equivalent method calls (Where, Select, etc.). At runtime, there is no distinction between the two forms.</p><p>A key architectural idea in LINQ is the separation between <strong>query construction</strong> and <strong>query execution</strong>. Writing a LINQ query produces a query definition — an object that describes <em>what</em> should be done, not <em>when</em> it should run.</p><p>This design allows LINQ queries to be composed, reused, and adapted across different data sources, while delegating execution details to either the runtime (for in-memory collections) or a provider (for external data sources).</p><p>Understanding this separation is essential before examining when queries execute, how often they run, and how their behavior can impact performance and correctness.</p><h4><strong>Deferred Execution in LINQ</strong></h4><p>One of the most important characteristics of LINQ is <strong>deferred execution</strong>. Most LINQ queries do not execute at the point they are defined. Instead, they execute <strong>only when the results are enumerated</strong>.</p><p>In practice, enumeration happens when you:</p><ul><li>iterate over the query using foreach</li><li>call a terminal operation such as ToList(), ToArray(), First(), Single(), or Count()</li></ul><p>Until one of these actions occurs, a LINQ query is simply a <strong>description of work</strong>, not the work itself.</p><p>This behavior enables powerful composition. Queries can be built incrementally, passed around, combined, and modified before they ever run. However, deferred execution also introduces behavior that is easy to misunderstand if you’re not aware of it.</p><p>Because execution is deferred:</p><ul><li>A query can execute <strong>multiple times</strong> if it is enumerated more than once</li><li>Changes to the underlying data source can affect results between enumerations</li><li>Expensive operations may run later than expected, sometimes far from where the query was defined</li></ul><p>Consider a query that filters a collection. If the collection changes after the query is defined but before it is enumerated, the results will reflect the <strong>current state</strong>, not the state at the time the query was written.</p><p>To force immediate execution, LINQ provides <strong>materialization operators</strong> such as ToList() and ToArray(). These operators execute the query immediately and store the results in memory, preventing further re-execution.</p><p>Deferred execution is one of LINQ’s greatest strengths — it enables flexibility and composability — but it is also one of the most common sources of bugs, unexpected behavior, and performance issues in real applications.</p><p>Understanding deferred execution is essential before using LINQ in performance-sensitive code paths or when working with external data sources.</p><h4>Performance and Readability Trade-offs in LINQ</h4><p>LINQ shines when it expresses <strong>intent clearly</strong> — filtering, projecting, grouping, or aggregating data in a way that reads naturally. In these cases, LINQ often improves both readability and maintainability compared to manual loops.</p><p>However, LINQ is not free. Each operator adds overhead, and certain patterns can introduce hidden costs. Chaining multiple operators may allocate iterators, enumerate sequences multiple times, or perform expensive operations such as sorting or grouping more often than expected.</p><p>From a performance perspective, LINQ is best suited for:</p><ul><li>data transformation and querying</li><li>non–hot paths where clarity matters more than micro-optimizations</li><li>scenarios where correctness and composability outweigh raw speed</li></ul><p>In contrast, traditional loops may be preferable when:</p><ul><li>working in tight loops or performance-critical paths</li><li>mutating state heavily</li><li>debugging step-by-step execution is important</li><li>the LINQ query becomes difficult to read or reason about</li></ul><p>Readability matters just as much as performance. Deeply nested or overly clever LINQ expressions can obscure intent and make debugging harder. When a query requires mental unpacking to understand, that is often a sign it should be simplified or rewritten using imperative code.</p><p>The goal is not to avoid LINQ, but to <strong>use it intentionally</strong> — choosing clarity first, and measuring performance only when it actually matters.</p><h4>Common LINQ Anti-Patterns and Final Thoughts</h4><p>Like any powerful tool, LINQ can be misused. Some common anti-patterns include:</p><ul><li><strong>Using LINQ everywhere by default</strong>, even when a simple loop would be clearer</li><li><strong>Embedding side effects</strong> (such as logging or state mutation) inside queries</li><li><strong>Ignoring deferred execution</strong>, leading to repeated or unexpected query execution</li><li><strong>Mixing LINQ with mutable state</strong>, making behavior hard to predict</li><li><strong>Assuming LINQ to Objects and LINQ to SQL behave the same</strong>, when execution semantics differ significantly</li></ul><p>These issues often don’t show up immediately. Code may compile, tests may pass, and behavior may appear correct — until scale, data size, or execution context changes.</p><p>LINQ is best viewed as a <strong>query language embedded in C#</strong>, not as a replacement for all control flow. Its strength lies in expressing <em>what</em> you want from data, not <em>how</em> to iterate over it.</p><h4>Final Thoughts: Writing Intentional LINQ</h4><p>Effective LINQ usage comes down to intention. Use it when it improves clarity, correctness, and composability. Avoid it when it hides behavior, complicates debugging, or introduces unnecessary overhead.</p><p>Understanding how LINQ works, when it executes, and where it fits best allows you to write code that is not only elegant, but also predictable and maintainable.</p><p>When used deliberately, LINQ becomes one of the most powerful and expressive tools in the .NET ecosystem.</p><blockquote>If you found this useful, feel free to clap 👏, share 🔁, or follow for more practical .NET and software design insights.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bc889472b2b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection in .NET Explained: What It Solves and How to Use It Correctly]]></title>
            <link>https://medium.com/@shahriddhi717/dependency-injection-in-net-explained-what-it-solves-and-how-to-use-it-correctly-c4867fcaaf4d?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4867fcaaf4d</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Mon, 26 Jan 2026 18:58:47 GMT</pubDate>
            <atom:updated>2026-01-26T18:58:47.887Z</atom:updated>
            <content:encoded><![CDATA[<h3>Dependency Injection in .NET Explained: What It Solves and How to Use It Correctly</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KS8iiuevLpsvJEq9SsHgTQ.png" /><figcaption><strong><em>Image Generated by Nano Banana</em></strong></figcaption></figure><h3>Table of Contents</h3><ol><li>Overview: What Problem Dependency Injection Solves.</li><li>Dependency Injection Basics in .NET.</li><li>Service Registration in .NET.</li><li>Service Lifetimes: Transient vs Scoped vs Singleton.</li><li>Dependency Injection Guidelines and Best Practices.</li><li>Common Dependency Injection Anti-Patterns.</li><li>Final Thoughts: Dependency Injection as a Design Tool.</li></ol><p><strong>Overview: What Problem Dependency Injection Solves</strong></p><p>Dependency Injection (DI) is a software design pattern that addresses the problems caused by hard-coded dependencies. Instead of classes creating and managing the objects they depend on, those responsibilities are shifted to the framework.</p><p>In .NET, this is achieved by <strong>abstracting dependencies using interfaces or base classes</strong>, registering those abstractions with concrete implementations in a <strong>service container</strong>, and injecting them into consuming classes — typically through constructors.</p><p>.NET provides a built-in dependency injection container based on IServiceProvider. Services are registered at application startup using an IServiceCollection. Once registration is complete, the container is built and used by the framework to resolve dependencies as needed.</p><p>When a class requires a dependency, it declares that dependency in its constructor. The DI container is then responsible for:</p><ul><li>Creating the appropriate implementation</li><li>Supplying it to the consuming class</li><li>Managing its lifetime and disposal</li></ul><p>By delegating object creation and lifecycle management to the framework, dependency injection reduces coupling, centralizes configuration, and makes applications easier to test and evolve.</p><p>The following sections explore how this model works in practice, starting with the fundamentals of dependency injection in .NET.</p><p><strong>Dependency Injection Basics in .NET</strong></p><p>Dependency Injection (DI) is a design pattern that removes hard-coded dependencies and enables <strong>Inversion of Control (IoC)</strong> between classes and the objects they depend on. Rather than creating dependencies directly, classes declare what they need, and the framework supplies those dependencies at runtime.</p><p>In .NET, DI is built around a small set of core abstractions provided by the Microsoft.Extensions.DependencyInjection.Abstractions package:</p><ul><li><strong>IServiceCollection</strong> — a collection used to register services and their configurations</li><li><strong>IServiceProvider</strong> — the runtime container used to resolve services</li><li><strong>ServiceDescriptor</strong> — a description of a service, including its type, implementation, and lifetime</li></ul><p>The typical DI flow in a .NET application follows a simple pattern:</p><ol><li><strong>Create a service collection</strong><br> An IServiceCollection is instantiated to hold service registrations.</li><li><strong>Register services</strong><br> Services are added to the collection by mapping abstractions (interfaces or base classes) to concrete implementations.</li><li><strong>Build the service provider</strong><br> Calling BuildServiceProvider creates an IServiceProvider, which acts as the DI container.</li><li><strong>Resolve and use services</strong><br> Services are resolved from the container and injected into consuming classes.</li></ol><pre>using Microsoft.Extensions.DependencyInjection;<br><br>// 1. Create the service collection<br>var services = new ServiceCollection();<br><br>// 2. Register services<br>services.AddSingleton&lt;IConsole, DefaultConsole&gt;();<br>services.AddSingleton&lt;IGreetingService, DefaultGreetingService&gt;();<br><br>// 3. Build the service provider<br>var serviceProvider = services.BuildServiceProvider();<br><br>// 4. Resolve a service<br>var greetingService = serviceProvider.GetRequiredService&lt;IGreetingService&gt;();<br><br>// 5. Use the service<br>greetingService.Greet(&quot;David&quot;);</pre><p><strong>Interfaces vs Concrete Types</strong></p><p>Not all services need to be defined as interfaces. Many services are expressed as <strong>contracts</strong> (interface + implementation), while others are registered and resolved as <strong>concrete types</strong>. The choice depends on whether abstraction, substitution, or testability is required.</p><p>Services can also depend on other services. In those cases, dependencies are declared in the constructor, and the container resolves them automatically. This allows application behavior to change through configuration rather than code changes — for example, swapping implementations or altering behavior without modifying consumers.</p><p><strong>Service Descriptors and Registration</strong></p><p>When services are registered using methods such as AddSingleton, AddScoped, or AddTransient, .NET internally creates a <strong>ServiceDescriptor</strong>. This descriptor captures:</p><ul><li>The service type</li><li>The implementation type (or factory)</li><li>The service lifetime</li></ul><p>While most applications rely on the convenience registration methods, understanding that these map to service descriptors helps clarify how the container represents and manages services internally.</p><blockquote><em>In real applications, service lifetimes and registration choices have significant implications. For now, it’s enough to understand that registration defines </em>what<em> gets created and </em>how<em> it is resolved.</em></blockquote><p><strong>Service Registration in .NET</strong></p><p>Service registration is the process of telling the .NET dependency injection container <strong>what services exist and how they should be created</strong>. In .NET, this is done by adding services to an IServiceCollection, typically during application startup.</p><p>The framework follows a clear convention for registering related services: <strong>groups of services are registered through a single extension method</strong>. For example, calling AddOptions() registers all services required for the options pattern. This convention keeps startup code readable and avoids scattered registrations.</p><p>Most applications register services using lifetime-based extension methods such as:</p><pre>services.AddSingleton&lt;IService, ServiceImplementation&gt;();<br>services.AddScoped&lt;IService, ServiceImplementation&gt;();<br>services.AddTransient&lt;IService, ServiceImplementation&gt;();</pre><p>Services can be registered in several ways:</p><ul><li><strong>Interface to implementation</strong> (most common)</li><li><strong>Concrete type only</strong> (when abstraction isn’t required)</li><li><strong>Factory-based registration</strong> (when construction needs logic or configuration)</li></ul><p>Registering only an implementation type is equivalent to registering the same type as both the service and implementation:</p><pre>services.AddSingleton&lt;ExampleService&gt;();</pre><p>This is functionally equivalent to:</p><pre>services.AddSingleton&lt;ExampleService, ExampleService&gt;();</pre><p>The choice of registration style determines <em>how</em> a service is resolved, but <strong>service lifetime determines <em>how long</em> it lives</strong>, which is often more critical.</p><p><strong>Service Lifetimes: Transient vs Scoped vs Singleton</strong></p><p>Every registered service must have a lifetime. In .NET, there are three primary lifetimes, each with different implications for correctness, performance, and memory usage.</p><p><strong>Transient</strong></p><p>Transient services are <strong>created every time they are requested</strong> from the container.</p><pre>services.AddTransient&lt;IService, ServiceImplementation&gt;();</pre><p>They are appropriate for:</p><ul><li>lightweight, stateless services</li><li>services that should not share state</li></ul><p>Because a new instance is created each time, transient services incur more allocations and are disposed at the end of the request (in request-based apps).</p><p><strong>Scoped</strong></p><p>Scoped services are <strong>created once per scope</strong>, typically once per HTTP request in ASP.NET Core applications.</p><pre>services.AddScoped&lt;IService, ServiceImplementation&gt;();</pre><p>They are appropriate for:</p><ul><li>request-specific state</li><li>services that coordinate work within a single request</li></ul><p>A scoped service must always be resolved <strong>within a scope</strong>. Resolving a scoped service from a singleton causes the scoped service to behave like a singleton, which can introduce subtle and hard-to-diagnose bugs</p><blockquote>For example, Entity Framework Core registers DbContext as scoped by default for this reason.</blockquote><p><strong>Singleton</strong></p><p>Singleton services are <strong>created once and reused for the entire lifetime of the application</strong>.</p><pre>services.AddSingelton&lt;IService, serviceImplementation&gt;();</pre><p>They are appropriate for:</p><ul><li>stateless services</li><li>services that cache data</li><li>shared infrastructure components</li></ul><p>Singleton services must be <strong>thread-safe</strong>, as they may be accessed concurrently. They are disposed only when the application shuts down, so memory usage should be carefully considered.</p><p>Singleton behavior should be managed by the container — not by manually implementing the singleton pattern.</p><p><strong>Lifetime Rules (What’s Allowed)</strong></p><p>As a rule of thumb:</p><ul><li>A <strong>longer-lived service should not depend on a shorter-lived service</strong></li><li>It is safe to:</li></ul><blockquote>inject singletons into scoped or transient services</blockquote><blockquote>inject scoped services into other scoped services</blockquote><ul><li>It is <strong>not safe</strong> to inject scoped services into singletons unless an explicit scope is created</li></ul><p>In development, .NET performs <strong>scope validation</strong> to detect these violations early and throw helpful exceptions.</p><p><strong>Dependency Injection Guidelines and Best Practices</strong></p><p>Designing for DI starts with designing services that are easy to replace, test, and evolve. Prefer <strong>small, focused services</strong> with clear responsibilities. If a class needs many injected dependencies, it is often a sign it is doing too much and should be refactored to better follow the Single Responsibility Principle.</p><p>Avoid <strong>stateful static classes</strong> and global state. If you need shared behavior, prefer a container-managed singleton rather than static access patterns. Also avoid directly instantiating dependencies (using new) inside services, because that tightly couples the service to a specific implementation and makes changes and testing harder.</p><p>A key operational rule is <strong>disposal</strong>: if the DI container creates an IDisposable service, the container is responsible for disposing it. Code that resolves services should <strong>not</strong> call Dispose() on them. If you register an already-created instance (for example AddSingleton(new ExampleService())), the container did not create it—so you, as the developer, remain responsible for its cleanup.</p><p>Other practical recommendations that keep DI predictable in production:</p><ul><li>Keep DI factories <strong>fast and synchronous</strong> (avoid async work during registration).</li><li>Use the <strong>options pattern</strong> for configuration instead of storing configuration objects directly in the container.</li><li>Avoid static access to services (for example, storing ApplicationServices in a static field).</li><li>Use singletons intentionally: they must be <strong>thread-safe</strong>, may hold memory for the lifetime of the app, and can complicate testing if they retain shared mutable state.</li></ul><p><strong>Common Dependency Injection Anti-Patterns</strong></p><p>Some DI issues are less about syntax and more about misusing lifetimes or bypassing DI altogether. The following patterns are worth calling out because they often lead to production bugs that are difficult to diagnose.</p><p>1. Disposable Transient Services Captured by the Container</p><p>When you register a <strong>transient service that implements </strong><strong>IDisposable</strong>, the DI container keeps track of it so it can dispose it later. If such a service is resolved from the <strong>root service provider</strong>, it will not be disposed until the application shuts down.</p><p>This can unintentionally cause memory growth.</p><pre>services.AddTransient&lt;ExampleDisposable&gt;();<br><br>var provider = services.BuildServiceProvider();<br><br>for (int i = 0; i &lt; 1_000; i++)<br>{<br>    provider.GetRequiredService&lt;ExampleDisposable&gt;();<br>}</pre><p><strong>What goes wrong:</strong></p><ul><li>1,000 disposable instances are created</li><li>None are disposed until the application shuts down</li><li>The container holds references to all of them</li></ul><p>This looks like a memory leak in production.</p><p><strong>Better approach</strong></p><p>If a disposable truly needs a short lifetime, create it <strong>inside a scope</strong> or use a <strong>factory</strong> that allows explicit disposal.</p><p><strong>2. Async DI Factories Causing Deadlocks</strong></p><p>DI registration methods support factory overloads such as:</p><pre>services.AddSingleton&lt;T&gt;(Func&lt;IServiceProvider, T&gt; factory)</pre><p>These factories <strong>must be synchronous</strong>. Blocking on async work inside them can cause deadlocks.</p><p>Anti-pattern: Blocking async code in DI factory</p><pre>services.AddSingleton&lt;Foo&gt;(sp =&gt;<br>{<br>    // DO NOT DO THIS<br>    return GetFooAsync(sp).Result;<br>});<br><br>static async Task&lt;Foo&gt; GetFooAsync(IServiceProvider sp)<br>{<br>    await Task.Delay(100);<br>    return new Foo();<br>}</pre><p><strong>What goes wrong:</strong></p><ul><li>.Result blocks the thread</li><li>The async continuation cannot resume</li><li>Application deadlocks during startup or resolution</li></ul><p>Better approach</p><p>Resolve the service synchronously, then perform async initialization <strong>after resolution</strong>, using an async method — not the constructor or DI factory.</p><p>3. Captive Dependency (Lifetime Mismatch)</p><p>A <strong>captive dependency</strong> occurs when a longer-lived service depends on a shorter-lived service. The shorter-lived service becomes effectively “captured” for the lifetime of the longer-lived one.</p><p>This term was popularized by Mark Seemann.</p><p>Anti-pattern: Singleton depending on scoped service</p><pre>services.AddSingleton&lt;Foo&gt;();<br>services.AddScoped&lt;Bar&gt;();<br>public class Foo<br>{<br>    private readonly Bar _bar;<br><br>    public Foo(Bar bar)<br>    {<br>        _bar = bar;<br>    }<br>}</pre><p><strong>Why this is broken:</strong></p><ul><li>Foo is created once</li><li>Bar is scoped per request</li><li>The first Bar instance is held for the entire lifetime of Foo</li><li>Subsequent requests reuse stale state.</li></ul><h4>Better approach</h4><ul><li>Align lifetimes (make both scoped), or</li><li>Inject IServiceScopeFactory and create scopes explicitly when needed.</li></ul><h3>Key Takeaway</h3><p>Most DI anti-patterns fall into one of three categories:</p><ul><li><strong>Lifetime mismatches</strong></li><li><strong>Improper disposal</strong></li><li><strong>Blocking or bypassing the container</strong></li></ul><p>They often compile, pass tests, and even work in development — until scale, concurrency, or uptime exposes the flaw. Understanding these patterns helps ensure DI remains a reliability tool rather than a hidden risk.</p><p><strong>Final Thoughts: Dependency Injection as a Design Tool</strong></p><p>Dependency Injection is most valuable when treated as a <strong>design tool</strong>, not as a rule to apply everywhere. Used well, DI encourages loose coupling, clearer boundaries, and code that is easier to test. Used poorly, it can produce over-abstracted systems, lifetime bugs, hidden dependencies, and confusing runtime behavior.</p><p>The practical goal is balance: keep services small and composable, choose lifetimes deliberately, let the container manage disposal, and avoid patterns that bypass DI’s strengths. When DI stays aligned with responsibility-driven design, it becomes an enabling foundation rather than a source of complexity.</p><blockquote>If you found this useful, feel free to <strong>clap 👏, share 🔁, or follow</strong> for more practical .NET and software design insights.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4867fcaaf4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Performance Optimization in .NET using ArrayPool<T>: When Slower Benchmarks Mean Better Production]]></title>
            <link>https://medium.com/@shahriddhi717/performance-optimization-in-net-using-arraypool-t-when-slower-benchmarks-mean-better-production-2b29d612e8b8?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b29d612e8b8</guid>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Wed, 24 Dec 2025 19:34:50 GMT</pubDate>
            <atom:updated>2025-12-24T19:34:50.515Z</atom:updated>
            <content:encoded><![CDATA[<h3>Performance Optimization in .NET using ArrayPool&lt;T&gt;: When Slower Benchmarks Mean Better Production</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nxi_hkEOBfl5Yr7VpwrNgg.jpeg" /></figure><h3><strong>Introduction</strong></h3><p>I’ll be honest — I thought I understood performance optimization. After years of writing. NET applications, I knew the usual suspects: optimize algorithms, reduce database calls, cache aggressively. But memory allocation patterns? That seemed like something the garbage collector should handle automatically. After all, isn’t that what we have a GC for?</p><p>That confidence shattered when I was working on a hardware device reader application. The app ran fine for the first hour. Then users started reporting random stutters — nothing catastrophic, just annoying 20–50ms freezes during device scans. The kind of issue that makes an application feel “janky” but is hard to pin down.</p><p>I fired up a profiler and discovered something embarrassing: my innocent-looking method was creating 500 KB of garbage every few seconds. A simple 5,125-byte buffer allocation, repeated hundreds of times, was triggering Gen0 collections every few minutes. In long-running sessions, it was creating 850 MB of memory churn over a week.</p><p>When I discovered ArrayPool&lt;T&gt;, I was excited. Every article I read promised 2-3x performance improvements. I refactored the code, added proper rent/return patterns, and ran my benchmarks with confidence.</p><p>The result? My code was 50% slower.</p><p>I stared at the benchmark results in disbelief. Ratio: 1.58 meant my &quot;optimized&quot; code was taking longer to execute than the original. Had I completely misunderstood how ArrayPool works? Was I doing something wrong?</p><p>Frustrated, I dug deeper. I created different benchmark scenarios, tested with and without I/O delays, measured not just speed but memory allocations and GC collections. What I discovered changed how I think about performance optimization entirely.</p><p>This article is the story of that journey — from confident optimization attempt to confusing benchmark results, to finally understanding what “performance” really means in production systems. You’ll see the actual benchmark numbers (including the embarrassing ones), learn why ArrayPool can appear slower while making your application better, and discover when memory optimization matters more than raw speed.</p><p>By the end, you’ll understand:</p><ul><li>Why ArrayPool might slow down your benchmarks (and why that’s okay)</li><li>The critical difference between CPU-bound and I/O-bound optimization</li><li>How to eliminate 90%+ of memory allocations without sacrificing performance</li><li>When to use ArrayPool (and when to avoid it)</li><li>Real production metrics showing long-term benefits</li></ul><p>If you’ve ever been confused by conflicting performance advice or wondered whether “micro-optimizations” actually matter in real applications, this article is for you. Let’s dive into what I learned when my optimization made things worse — before making them much, much better.</p><h3><strong>Table of Contents</strong></h3><ul><li>Understanding the Memory Problem</li><li>What is ArrayPool&lt;T&gt;?</li><li>The Implementation: Before and After</li><li>The Benchmark Surprise: Slower Performance?</li><li>The Real Benefits: Memory and GC Elimination</li><li>When to Use (and When NOT to Use) Pooling</li><li>Production Results: Long-Term Impact</li><li>Common Pitfalls and Best Practices</li><li>Key Takeaways</li><li>Conclusion</li></ul><h3>Understanding the Memory Problem</h3><p>The application seemed straightforward enough: scan for connected hardware devices over USB, read their unique identifiers, and return a list of serial numbers. The method would be called whenever the user clicked “Scan Devices” or when the background service checked for new connections.</p><p>Here’s the problematic code:</p><pre>public List&lt;string&gt; GetConnectedDevices(out short portNum, out short portType)<br>{<br>    List&lt;string&gt; serialNums = new List&lt;string&gt;();<br>    <br>    StringBuilder ID_buf = new StringBuilder(40);      // Allocation #1<br>    byte[] stateBuf = new byte[5125];                  // Allocation #2 - 5KB! <br>    short[] ROM = new short[8];<br>    <br>    // ...  hardware communication with stateBuf ... <br>    <br>    while (flag &gt; 0)<br>    {<br>        flag = PInvoke.TMNext(session_handle, stateBuf);<br>        if (flag &gt; 0)<br>        {<br>            PInvoke. TMRom(session_handle, stateBuf, ROM);<br>            StringBuilder sn = new StringBuilder();    // Allocation #3 (per device)<br>            // Build serial number... <br>            serialNums.Add(sn.ToString());<br>        }<br>    }<br>    <br>    return serialNums;<br>}</pre><p>At first glance, this looks reasonable. But here’s what was actually happening: every call allocated a fresh 5,125-byte array for the state buffer, used it for a few milliseconds during hardware communication, then threw it away. Multiply this by hundreds of calls in a long-running application, and you have continuous memory churn.</p><p>Why This Matters for Hardware Communication</p><p>Hardware I/O has timing constraints. The actual USB communication took 1–5 milliseconds. But the accumulated GC pressure from repeatedly allocating and discarding 5KB buffers was causing:</p><ul><li>Unpredictable pauses during device reads</li><li>“Janky” UI experience (users noticed the stutters)</li><li>Inconsistent response times (sometimes 2ms, sometimes 25ms)</li><li>Memory footprint growing over time</li></ul><p>The irony? The hardware I/O took milliseconds. The allocations were sub-millisecond. But the <em>cumulative effect</em> of the garbage collector cleaning up hundreds of discarded buffers was killing performance predictability.</p><p>I needed to eliminate that 5KB allocation without adding complexity or harming reliability. That’s when I discovered ArrayPool&lt;T&gt;.</p><h3><strong>What is ArrayPool&lt;T&gt;?</strong></h3><p>ArrayPool&lt;T&gt; is a memory pooling feature in .NET that lets you borrow and return arrays instead of creating new ones every time. Think of it like a library: instead of buying a new book each time you need one, you check it out, use it, and return it for someone else to use.</p><p>The Core Idea:</p><p>Traditional approach: Create a new array → use it → throw it away → garbage collector cleans it up later.</p><p>ArrayPool approach: Borrow an array from the pool → use it → return it to the pool → same array gets reused by the next caller.</p><p>Key Benefits:</p><ul><li>No heap allocations — Arrays are reused, not recreated</li><li>No garbage collection pressure — Nothing becomes garbage</li><li>Thread-safe — Multiple threads can safely borrow and return arrays</li><li>Minimal overhead — Borrowing takes only 10–20 nanoseconds</li><li>Ready to use — ArrayPool&lt;T&gt;.Shared is pre-configured and available immediately</li></ul><p>The Critical Rule:</p><p>You must always return what you borrow. This is typically done with a try-finally block to ensure the array goes back to the pool even if an exception occurs. Forgetting to return arrays defeats the entire purpose—the pool runs out of inventory and has to create new arrays anyway.</p><p>Important Gotcha:</p><p>When you request an array of a specific size (say, 5,125 bytes), the pool might give you a larger one (like 8,192 bytes). The pool rounds up to standard sizes for efficiency. Always track the size you actually need separately.</p><p>What It’s NOT:</p><p>ArrayPool isn’t magic. It only helps with array allocations, doesn’t work well for tiny objects (&lt; 1KB), and requires discipline — you must explicitly return what you borrow. But for large, frequently-allocated arrays in performance-critical code? It’s perfect.</p><p>For my 5KB buffer that was being allocated hundreds of times, this seemed like the ideal solution. I refactored the code, added proper return logic, and prepared to see the performance improvements everyone talks about.</p><p>That’s when I got a surprise.</p><h3>The Implementation: Before and After</h3><p>Before: The Allocation-Heavy Approach</p><pre>public List&lt;string&gt; GetConnectedDevices(out short portNum, out short portType)<br>{<br>    List&lt;string&gt; serialNums = new List&lt;string&gt;();<br>    <br>    StringBuilder ID_buf = new StringBuilder(40);<br>    byte[] stateBuf = new byte[5125];  // ❌ New allocation every call<br>    short[] ROM = new short[8];<br>    <br>    portNum = 0;<br>    portType = 0;<br><br>    try<br>    {<br>        PInvoke.Get_Version(ID_buf);<br>        <br>        // DoSomething<br>    }<br>    catch (Exception ex)<br>    {<br>        // Error handling<br>    }<br>    <br>    return serialNums;<br>}</pre><p>The Problem: Every call creates a fresh 5,125-byte array that immediately becomes garbage.</p><p>After: ArrayPool in Action</p><pre>public List&lt;string&gt; GetConnectedDevices(out short portNum, out short portType)<br>{<br>    List&lt;string&gt; serialNums = new List&lt;string&gt;();<br>    <br>    StringBuilder ID_buf = new StringBuilder(40);<br>    byte[] stateBuf = ArrayPool&lt;byte&gt;.Shared.Rent(5125);  // ✅ Rent from pool<br>    short[] ROM = new short[8];<br>    <br>    portNum = 0;<br>    portType = 0;<br><br>    try<br>    {<br>        PInvoke.Get_Version(ID_buf);<br>        <br>        // Do Something<br>    }<br>    catch (Exception ex)<br>    {<br>        // Error handling<br>    }<br>    finally<br>    {<br>        ArrayPool&lt;byte&gt;.Shared.Return(stateBuf);  // ✅ Always return to pool<br>    }<br>    <br>    return serialNums;<br>}</pre><p>The changes:</p><ol><li>Replaced new byte[5125] with ArrayPool&lt;byte&gt;.Shared.Rent(5125)</li><li>Added finally block to ensure the buffer is always returned</li><li>That’s it — minimal code change for maximum impact</li></ol><p>The finally block is critical. Even if an exception occurs during hardware communication, the buffer still gets returned to the pool for reuse.</p><p>With this refactoring complete, I was ready to see the performance improvements. I set up BenchmarkDotNet, configured my tests, and hit run. The results were… confusing.</p><h3>The Benchmark Surprise: Slower Performance?</h3><p>I was confident. Every article about ArrayPool promised 2–3x performance improvements. I had refactored the code properly, added the try-finally blocks, and was ready to see impressive results.</p><p>I created a benchmark using BenchmarkDotNet to simulate our hardware scanning scenario and here’s what I saw:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*EkCWwIaEIqzEoTnkz2pMbg.png" /></figure><p>Both methods took 1.560 seconds. The Ratio column showed 1.00 - meaning they were <em>identical</em> in execution time. No speed improvement at all.</p><p>But look at the Allocated column:</p><ul><li>Without Pooling: 515,200 B (~503 KB)</li><li>With Pooling: 0 B (zero allocations!)</li></ul><p>And the Alloc Ratio: 0.00 - a complete 100% elimination of allocations.</p><ul><li>*I was confused. ** Where was my 2–3x speed improvement? Had I implemented something wrong? I checked the code multiple times — everything looked correct. The try-finally was proper, buffers were being returned, no leaks.</li></ul><p>Then I tried different scenarios. I removed the Thread.Sleep() to test pure CPU-bound allocation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*PFvWTsZAe0YOsX2QBXIafQ.png" /></figure><p>The realization hit me: My original benchmark was I/O-bound, not CPU-bound. The 1ms hardware delay (1,000 microseconds) completely dominated the execution time. The ~14 microseconds saved by pooling were invisible next to milliseconds of I/O.</p><pre>I/O-Bound scenario: <br>- Hardware delay: 1,000 μs per operation<br>- Pooling savings:  ~14 μs<br>- Overhead: 1.4% of total time → unmeasurable<br><br>CPU-Bound scenario:<br>- No I/O delay<br>- Pooling savings: ~14 μs<br>- Total operation:  21 μs → 7 μs<br>- Improvement: 66% faster (3x speed!)</pre><p>But here’s what I <em>did</em> gain in the I/O-bound scenario:</p><ul><li>100% memory reduction (503 KB → 0 bytes)</li><li>Zero GC collections (no allocations = no garbage)</li><li>Same execution speed (no performance penalty!)</li></ul><p>This wasn’t the dramatic speed win I expected. But for a long-running hardware application? Zero allocations with zero performance cost is actually perfect.</p><p>The benchmark taught me something crucial: ArrayPool in I/O-bound scenarios isn’t about speed — it’s about memory management and GC elimination. The real benefits would show up in production over hours and days, not in millisecond benchmarks.</p><h3><strong>The Real Benefits: Memory and CG Elimination</strong></h3><p>The value of <strong>ArrayPool</strong> isn’t just about execution speed — it’s about <strong>predictable performance</strong>. In I/O-bound scenarios, the microseconds saved by pooling are small compared to network or hardware wait times, but the memory benefits are massive.</p><p>Every eliminated allocation is one less object for the Garbage Collector (GC) to track. Because you cannot control the timing of a Gen0 or Gen2 collection, these pauses often happen during critical hardware reads or user interactions. By removing the allocation, you remove the <strong>unpredictability</strong>.</p><p>For my hardware device reader, this was the difference between “occasional stutters” and “consistent responsiveness.” While the average speed didn’t change, the <strong>P99 latency</strong> (the worst-case scenario) dropped from <strong>25ms to 4ms</strong> — a 6x improvement.</p><h3>When to Use (and When NOT to Use) Pooling</h3><p>Not every allocation needs pooling. Use this practical framework to decide when the complexity is worth the effort.</p><p>Pool when you have:</p><ul><li><strong>Large Objects:</strong> Anything over <strong>1KB</strong>. Larger objects are costlier to allocate and more valuable to reuse.</li><li><strong>High Frequency:</strong> Code running thousands of times per hour. Pooling doesn’t pay off for rare startup operations.</li><li><strong>Short-Lived Objects:</strong> Best for “rent, use, and return” cycles. Avoid pooling objects held for hours.</li><li><strong>Hot Paths:</strong> Performance-critical areas like tight loops, request handlers, or event callbacks.</li><li><strong>I/O-Bound Operations:</strong> Pooling eliminates memory churn without adding performance overhead to your I/O wait times.</li></ul><p>Don’t pool when you have:</p><ul><li><strong>Cold Paths:</strong> Error handlers, shutdown logic, or initialization code. Keep these paths simple.</li><li><strong>Small Objects:</strong> Under a few hundred bytes, the overhead of managing the pool often exceeds the cost of a new allocation.</li><li><strong>Complex Lifetimes:</strong> If ownership is unclear or objects are passed through many layers, stick to regular allocation to avoid leaks.</li><li><strong>Uncertain Sizes:</strong> Pooling rounds up sizes; requesting wildly different lengths can lead to memory waste.</li></ul><blockquote><strong>The Golden Rule:</strong> Only pool <strong>large, frequently-allocated arrays in hot paths</strong>. Everything else? Keep it simple.</blockquote><h3>Pitfalls &amp; Best Practices</h3><ul><li><strong>Mandatory </strong><strong>try-finally:</strong> Every &quot;rent&quot; must have a guaranteed &quot;return.&quot; Without a finally block, exceptions during communication will leak buffers, eventually forcing the pool to allocate new memory and defeating the optimization.</li><li><strong>Zero Access After Return:</strong> Once a buffer is returned, it is no longer yours. Writing to a returned reference causes impossible-to-debug corruption as other threads begin using that memory.</li><li><strong>Clear Sensitive Data:</strong> Rented buffers are reused. If you handle passwords or keys, use the clearArray parameter when returning to ensure the next caller doesn&#39;t inherit your sensitive data.</li><li><strong>Expect Larger Arrays:</strong> The pool rounds up to bucket sizes (often powers of two). If you request 5,125 bytes, you might get 8,192. Track your actual data length separately; never assume buffer.Length equals your requested size.</li><li><strong>Don’t Pool Everything:</strong> Avoid pooling small objects like StringBuilder. The management overhead often exceeds the cost of a new allocation. Stick to large, high-frequency arrays.</li><li><strong>Profile Before Optimizing:</strong> Don’t guess. Use a profiler to find “hot” paths with high allocation volume. I only optimized my device scanner because it showed up “bright red” in the profiler.</li><li><strong>Realistic Benchmarks:</strong> Synthetic CPU loops are misleading for I/O-bound code. Simulate real-world workloads, including hardware delays, to understand the true impact on memory and latency.</li></ul><h3>Key Insights</h3><ul><li><strong>Predictability Over Speed:</strong> Pooling isn’t about raw execution time; it’s about eliminating memory churn. In I/O-bound apps, the goal is consistent latency and fewer GC pauses.</li><li><strong>Context is King:</strong> CPU-bound tasks get speed and memory wins. I/O-bound tasks get memory stability with zero performance cost. Know which scenario you are in.</li><li><strong>P99 Matters Most:</strong> Users experience worst-case “stutters,” not averages. By removing GC unpredictability, my P99 latency improved 6x, creating a much smoother experience.</li><li><strong>Compounding Value:</strong> Memory benefits grow over time. A 33% reduction after an hour became an 80% reduction after a week. Pooling is essential for long-running services.</li><li><strong>Discipline Required:</strong> ArrayPool demands lifecycle management. It is not a drop-in replacement. You must be diligent with returns, clearings, and size tracking.</li><li><strong>Simulate Reality:</strong> Don’t trust benchmarks that ignore I/O wait times. My real success came from perfect memory elimination, even when raw speed didn’t change.</li></ul><h3>Conclusion</h3><p>I started this journey looking for a speed trick; I finished with a deeper understanding of system reliability. Performance optimization isn’t just about making code faster — it’s about making it sustainable and predictable under load.</p><p>While my initial benchmarks showed a 1.58x CPU slowdown, the production results were transformative: a <strong>93% reduction in allocations</strong> and an <strong>80% smaller memory footprint</strong> over time. For long-running hardware interfaces, these metrics are far more important than raw throughput.</p><p>ArrayPool is a used specific problem: repeated allocation of large temporary buffers. If you understand your workload — whether it’s CPU or I/O bound — you can apply this where it actually delivers value. The goal is to move beyond “cargo-cult” optimization and toward genuine engineering improvements.</p><p>Profile your application, find those hot-path allocations, and measure the results realistically. Just don’t let the initial benchmarks confuse you; the real-world stability is worth the effort.</p><blockquote>Don’t let your memory management knowledge leak! 💧 If you enjoyed this journey through ArrayPool, hit that follow button and show some love with a few claps! 👏👨‍💻</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b29d612e8b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debugging on Autopilot: What I Learned from “.NET Diagnostic Tooling with AI” at .NET Conf 2025]]></title>
            <link>https://medium.com/@shahriddhi717/debugging-on-autopilot-what-i-learned-from-net-diagnostic-tooling-with-ai-at-net-conf-2025-e38168807398?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e38168807398</guid>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Thu, 27 Nov 2025 21:24:08 GMT</pubDate>
            <atom:updated>2025-11-27T21:24:08.515Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Debugging on Autopilot: What I Learned from “.NET Diagnostic Tooling with AI” at .NET Conf 2025</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W9EVMam7sBlhwE0M6WbMFQ.png" /><figcaption>.NET Conf 2025 Digital Swag Image</figcaption></figure><p>On November 18 — 19, I did what many .NET developers quietly look forward to all year: I blocked out my calendar, grabbed a big mug of chai, and tuned into .NET Conf.</p><p>By late afternoon my brain was full of runtime updates, tooling demos, and a steady stream of “AI + .NET” announcements. I was this close to calling it a day — until a session title caught my eye:</p><h3>“.NET Diagnostic Tooling with AI.”</h3><p>As someone who has lost too many evenings to “Why is this app slow on <strong>just </strong>this device?” I had to watch.</p><p>Somewhere between the sessions and the after-hours energy, I also got lucky at the Code Party and won an annual Dometrain Pro subscription. That win made the whole experience feel like a reset button for my learning plans: not only did I walk away with new diagnostic techniques, I also walked away with a concrete way to go deeper through curated learning paths over the next year.</p><p>This article is my recap: what the session was really about, why AI-powered diagnostics matter, and how I’m planning to bring these ideas into my everyday .NET work.</p><h3>Why Diagnostics Suddenly Got Exciting</h3><p>Performance profiling has always been part of .NET — PerfView, Visual Studio Profiler, ETW events, traces, dumps, you name it. The problem is that profiling feels like going to the doctor: you know you should do it, but you only go when something hurts badly enough.</p><p>The session reframed diagnostics in a different way:</p><ul><li>Tracing and dumps become “data exhaust” from your app.</li><li>AI becomes the assistant that reads this exhaust and tells you what’s wrong.</li></ul><p>Instead of staring at endless flame graphs and GC stats, the idea is to let AI highlight suspicious hot paths, memory patterns, and smells, then use traditional tools to verify and fix.</p><p>The key shift is this: diagnostics is no longer just a specialized, rare activity for production incidents. With the current .NET tooling plus AI, it can become part of your everyday development loop.</p><h3>The Core Stack: dotnet-trace, dotnet-gcdump, and Friends</h3><p>The session anchored itself on a concrete set of tools you can install and use today:</p><ul><li>`dotnet-trace`— Collects CPU traces and performance data from running .NET applications.</li><li>`dotnet-gcdump`— Captures managed memory snapshots so you can see what’s in the heap and how it grows.</li><li>`dotnet-dsrouter`— Bridges diagnostics from remote devices (Android, iOS, emulators) back to your machine.</li></ul><p>In recent versions, `dotnet-trace` and `dotnet-gcdump` gained a ` — dsrouter` option that automatically spins up and manages `dotnet-dsrouter` for you. That makes mobile and remote profiling much more approachable: instead of juggling multiple processes and ports manually, you run one command and get back a clean trace or GC dump.</p><p>Once you have that data, you can open it in:</p><ul><li>Visual Studio’s Performance Profiler</li><li>PerfView</li><li>Flame-graph viewers like SpeedScope</li></ul><p>That’s the traditional diagnostics story. The AI twist is what comes next.</p><h3>Where AI Fits: From Raw Traces to Actionable Insight</h3><p>A trace file or GC dump is like a medical report: full of useful numbers that are nearly unreadable without experience.</p><p>The big theme of the session was: AI can act as your junior performance engineer, helping you interpret this data quickly.</p><p>Conceptually, the AI-powered workflow looks like this:</p><ol><li>Collect data . Use `dotnet-trace collect` or `dotnet-gcdump collect` (with ` — dsrouter` for remote or mobile scenarios) to grab a snapshot from your app.</li></ol><p>2. Convert or export . Save traces in formats like `.nettrace` or `speedscope.json`, or open GC dumps in Visual Studio / PerfView to extract summary information.</p><p>3. Let AI “read” the trace for you . Feed the summarized data (top hot paths, allocation-heavy methods, long GC pauses, thread timelines) into an AI assistant or custom agent and ask questions like:</p><blockquote>“Why does startup feel slow on Android but not on Windows?”</blockquote><blockquote>“What patterns do you see in memory growth after navigation?”</blockquote><p>4. Get human-readable guidance</p><p>. The AI can suggest likely causes</p><p>. synchronous I/O on the UI thread</p><p>. repeated layout computations</p><p>. event-handler leaks</p><p>. misconfigured diagnostic properties</p><p>. and propose code-level or configuration changes.</p><p>5. Apply and verify</p><p>. You refactor, re-run the diagnostic pipeline, and confirm improvements with actual numbers from your tools.</p><p>You still need to understand profiling basics, but AI dramatically reduces time-to-insight. Instead of spending an hour just to interpret the trace, you spend that hour fixing the real issues.</p><h3>A Concrete Example: Profiling a .NET MAUI App with AI in the Loop</h3><p>One of my favorite parts of the talk was how they approached mobile diagnostics— an area where I’ve personally done a lot of guessing and hand-waving.</p><p>A typical flow for a .NET MAUI app looks like this:</p><ol><li>Install the diagnostics tools</li></ol><pre>dotnet tool install -g dotnet-trace<br>dotnet tool install -g dotnet-gcdump<br># dsrouter is invoked via the --dsrouter option</pre><p>2. Capture a CPU trace from an Android device</p><pre>dotnet-trace collect --dsrouter android --format speedscope</pre><p>This launches the diagnostics router behind the scenes, connects to your device or emulator, and captures trace telemetry.</p><p>3. Build the app with diagnostic properties</p><p>For example, on Android:</p><pre>dotnet build -t:Run -c Release -f net8.0-android \<br>  -p:DiagnosticAddress=127.0.0.1 \<br>  -p:DiagnosticPort=9000 \<br>  -p:DiagnosticSuspend=false \<br>  -p:DiagnosticListenMode=connect</pre><p>These MSBuild properties wire your app to the diagnostics tooling so traces can flow from the device back to your development machine.</p><p>4. Feed the trace into an AI assistant</p><p>Export the summary stats (top methods by inclusive CPU, allocation-heavy functions, GC stats, thread timelines) and ask your AI tool something like:</p><blockquote>You’re a .NET performance engineer. Here is the profile of a MAUI app that feels slow on startup. Identify the biggest problems and suggest concrete improvements.”</blockquote><p>Instead of manually clicking around flame graphs, you might get insights like:</p><p>. Startup spends a large portion of time in synchronous JSON parsing on the main thread.</p><p>. XAML view construction is allocating a high number of short-lived objects; consider data virtualization.</p><p>. Repeated navigation causes object counts of certain view models to grow, suggesting a leak.</p><p>You still double-check the results in PerfView or Visual Studio — but AI points your attention in the right places from the start.</p><h3>Memory Leaks, Events, and AI as a Pattern Detector</h3><p>The session also highlighted memory leaks — especially in MAUI and cross-platform apps where navigation, event handlers, and platform interop can create subtle issues.</p><p>Common patterns include:</p><ul><li>Events where long-lived publishers keep short-lived subscribers alive</li><li>Circular references between managed objects and native platform objects</li><li>Static caches that unintentionally hold onto view models or services</li></ul><p>Traditionally, you’d:</p><ol><li>Capture a `.gcdump`.</li><li>2. Open it in Visual Studio or PerfView.</li><li>3. Manually hunt for types whose instance count and size keep growing.</li></ol><p>With AI involved, you can:</p><ul><li>Ask: Which object graphs look suspiciously like memory leaks?</li><li>Get grouped explanations like: This view model is referenced via an event handler from a static publisher and never goes out of scope.</li><li>Request remediation suggestions, such as using weak event patterns, proper disposal, or rethinking ownership.</li></ul><p>AI doesn’t replace your understanding of GC and object lifetimes — but it turns leak hunting from black magic into a more approachable, conversational debugging session.</p><h3>My Personal Takeaways (Plus the Dometrain Pro Win)</h3><p>The Code Party was celebration mode — music, trivia, giveaways, and a strong reminder that the .NET community is very much alive and playful.The diagnostics session was <strong>craftsmanship mode</strong> — careful attention to what our apps actually do under the hood.</p><p>Together, they reminded me why I still enjoy being a .NET developer:</p><ol><li>Diagnostics is getting democratized</li></ol><p>You no longer need to be “the performance guru” on the team to get useful insights. Tooling and AI together lower the barrier.</p><p>2. Profiling should move earlier in the lifecycle.</p><p>When collecting traces and dumps is just a command away, and AI can help interpret them, it becomes realistic to profile features while you build them — not only after customers complain.</p><p>3. AI is additive, not magic.</p><p>The real power is in combining:</p><p>. solid diagnostic tools</p><p>. AI-generated hints</p><p>. your own understanding of the app and its constraints</p><p>4. Mobile and cross-platform diagnostics are first-class.</p><p>The remote diagnostics support and MAUI demos signal that mobile performance is not an afterthought any more.</p><p>5. Learning does not stop with the conference.</p><h3>How You Can Start Using .NET Diagnostic Tooling with AI</h3><p>If you want to experiment with the ideas from this session, here’s a practical mini-roadmap you can adapt to your own projects:</p><ol><li>Install the global tools</li></ol><p>Make sure you have `dotnet-trace` and `dotnet-gcdump` installed globally.</p><p>2. Pick one “annoying” scenario in your app.</p><p>For example:</p><p>. startup on mobile</p><p>. a screen that feels sluggish</p><p>. a workflow that seems to leak memory</p><p>3. Capture a trace or dump for that scenario</p><p>Use `dotnet-trace collect` or `dotnet-gcdump collect`. For devices and emulators, pair it with the ` — dsrouter` option and proper diagnostic properties.</p><p>4. Analyze it twice</p><p>. First, with your usual tools (Visual Studio, PerfView, flame graphs).</p><p>. Second, by summarizing key metrics and asking an AI assistant to interpret them.</p><p>5. Compare the insights and act</p><p>Implement the simplest fix that addresses the biggest bottleneck. Re-run your traces and confirm that the numbers improved.</p><p>6. Make it part of your routine</p><p>Add a lightweight “profile and review with AI” step to your workflow for any feature that matters for performance or memory.</p><h3>Closing Thoughts</h3><p>“.NET Diagnostic Tooling with AI” wasn’t just another “look, AI can do things!” talk. It was a glimpse into how diagnostics, telemetry, and AI are converging into an everyday workflow for .NET developers.</p><p>Our apps already emit rich diagnostic data. Our tools make it easier than ever to capture it. AI now helps us turn that raw data into clear, prioritized actions.</p><p>If this article nudged you to run `dotnet-trace` or `dotnet-gcdump` even once on a real app, then the session — and that lucky draw — have already paid off.</p><blockquote>If this article added value to your learning, feel free to drop a clap, share it with your peers, or follow me for more deep dives into .NET and software engineering.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e38168807398" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microsoft Semantic Kernel + Extensions.AI: The Complete .NET AI Integration Guide]]></title>
            <link>https://medium.com/@shahriddhi717/microsoft-semantic-kernel-extensions-ai-the-complete-net-ai-integration-guide-6a97d18fa538?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a97d18fa538</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 04:03:19 GMT</pubDate>
            <atom:updated>2025-09-11T04:03:19.747Z</atom:updated>
            <content:encoded><![CDATA[<h3>Microsoft Semantic Kernel + Extensions.AI: The Complete .NET AI Integration Guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M2T1Qdz4kn1AsDEiyWpJAw.png" /><figcaption>Image generated with ChatGPT</figcaption></figure><h3>Introduction</h3><p>Most .NET developers face the same AI integration dilemma: How do you build production-ready AI features without vendor lock-in or reinventing orchestration patterns? Two Microsoft technologies solve this cleanly.</p><p><strong>Semantic Kernel (SK)</strong> addresses orchestration — an open-source SDK that helps you structure prompts, call functions/plugins, and adopt agentic patterns across .NET (and Python/Java). It’s designed as the glue that coordinates AI steps with your application logic.</p><p><strong>Microsoft.Extensions.AI</strong> covers provider abstraction — it gives .NET a unified interface (IChatClient) for chat/multimodal exchanges, streaming, and embeddings, all within familiar Microsoft.Extensions.* dependency-injection and logging patterns. Your call sites stay stable while you switch providers like Azure OpenAI, OpenAI, or local models.</p><p><strong>Mental model:</strong> SK = orchestration (prompts, plugins, planning/agents); Microsoft.Extensions.AI = transport/primitives (chat, embeddings, options, telemetry).</p><h3>Quick Decision Guide</h3><ul><li><strong>Extensions.AI only</strong>: Simple chat/embedding scenarios, single-step AI calls</li><li><strong>SK + Extensions.AI</strong>: Multi-step workflows, function calling, agent patterns, enterprise orchestration</li><li><strong>SK with local models</strong>: Same as above, but with full data control (Ollama/local hosting)</li></ul><h3>Table of Contents (updated)</h3><ul><li>Microsoft Semantic Kernel (SK) — overview for .NET</li><li>Microsoft.Extensions.AI — overview &amp; packages</li><li>How SK + Microsoft.Extensions.AI work together (why SK matters)</li><li>Code Demo: Automated C# Documentation Generator</li><li>Enterprise Deployment &amp; Data Governance</li></ul><h3>1) Microsoft Semantic Kernel (SK) — overview for .NET</h3><p><strong>What it is.</strong> Semantic Kernel is Microsoft’s open-source SDK for orchestrating AI with your regular .NET code. It helps you wire prompts, functions (“plugins”), connections to model providers (OpenAI, Azure OpenAI, etc.), plus optional planning/memory features — so your app can coordinate AI steps predictably.</p><p><strong>Why it helps.</strong> SK smooths integration, reduces complexity across different AI services, and makes behavior more controllable by structuring how prompts and steps run — useful from classroom demos to enterprise apps.</p><p><strong>Install (CLI).</strong></p><pre># .NET 8/9<br>dotnet add package Microsoft.SemanticKernel</pre><p><strong>Compatibility:</strong> SK 1.0+ works with .NET 6/8/9. Extensions.AI requires .NET 8+.</p><p>Create kernel</p><pre>using Microsoft.SemanticKernel;<br><br>var builder = Kernel.CreateBuilder();<br>// add services/plugins/connections here<br>var kernel = builder.Build();</pre><p>ASP.NET Core registration</p><pre>var builder = WebApplication.CreateBuilder();<br>builder.Services.AddKernel(); // register the SK Kernel<br><br>// add services/plugins/connections here<br><br>var app = builder.Build();</pre><p>These follow the official overview: the <strong>Kernel</strong> is the DI container that holds services and plugins SK orchestrates for you.</p><p><strong>Connect to a model (example: Azure OpenAI chat).</strong></p><pre>builder.Services.AddAzureOpenAIChatCompletion(<br>    &quot;your-resource-name&quot;,<br>    &quot;your-endpoint&quot;,<br>    &quot;your-resource-key&quot;,<br>    &quot;deployment-model&quot;);</pre><p>(Representative of the “connections” pattern shown in the Learn overview.)</p><p><strong>Core building blocks (at a glance).</strong></p><p><strong>=&gt; Connections</strong> — adapters to AI services &amp; data.</p><p><strong>=&gt;Plugins</strong> — functions the model can call (semantic prompts or native C#).</p><p><strong>=&gt; Planner</strong> — constructs/execut es plans (optional).</p><p><strong>=&gt; Memory</strong> — embeddings/vector-store abstractions for retrieval.</p><h3>2) Microsoft.Extensions.AI — the unified .NET AI abstractions</h3><p><strong>What it is.</strong> Microsoft.Extensions.AI gives .NET a <strong>provider-agnostic</strong> way to talk to AI services, using familiar Microsoft.Extensions.* patterns (DI, middleware). It centers on two key abstractions: <strong>IChatClient</strong> (chat/multimodal) and <strong>IEmbeddingGenerator</strong> (embeddings). On top, it offers ready-made middleware for <strong>tool calling</strong>, <strong>caching</strong>, <strong>telemetry</strong>, and more.</p><p><strong>Packages (which to reference).</strong></p><p>=&gt;<strong>Microsoft.Extensions.AI.Abstractions</strong> — core types/interfaces (e.g., IChatClient).</p><p>=&gt; <strong>Microsoft.Extensions.AI</strong> — adds higher-level helpers and pipeline/middleware.<br>Most apps take Microsoft.Extensions.AI <strong>plus</strong> one provider library (OpenAI, Azure, Ollama, etc.).</p><p><strong>Thread-safety.</strong> The API docs note that all IChatClient members are <strong>thread-safe</strong> for concurrent use—ideal for web apps and background services.</p><p><strong>Typical usage (one-shot &amp; streaming).</strong></p><pre>using Microsoft.Extensions.AI;<br><br>// One-shot<br>ChatResponse r = await chatClient.GetResponseAsync(&quot;What is AI?&quot;);<br>Console.WriteLine(r.Text);<br><br>// Streaming<br>await foreach (var u in chatClient.GetStreamingResponseAsync(&quot;Stream a haiku.&quot;))<br>    Console.Write(u);</pre><p>These patterns — request/streaming, plus tool-calling, caching, telemetry, DI pipelines — are all covered in the Learn article with full examples.</p><p><strong>Why it pairs with SK.</strong> Keep <strong>orchestration</strong> (prompts/plugins/flow) in SK, and keep <strong>model I/O</strong> behind IChatClient. That clean boundary lets you swap providers without refactoring your app logic and layer cross-cutting concerns (logging/Otel, caching, rate-limits) in a single place.</p><h3>3) How SK and Microsoft.Extensions.AI help each other (and why SK matters)</h3><p><strong>What each layer does .</strong></p><p><strong>=&gt; Microsoft.Extensions.AI</strong> gives you a <strong>unified, provider-agnostic client</strong> (IChatClient) with pipelines for streaming, tool-calling, caching, telemetry, DI, etc. It standardizes “how to talk to models.”</p><p><strong>=&gt; Semantic Kernel (SK)</strong> gives you <strong>orchestration</strong>: a Kernel (DI container), <strong>plugins</strong> (functions your app exposes to the model), optional memory, and planning guidance — i.e., “how AI steps fit together” around your .NET code.</p><h3>Why SK makes an Ext.AI–based app better</h3><ol><li><strong>Organized function exposure</strong><br> Instead of hand-rolling JSON tool specs or scattering helper methods, SK lets you register <strong>plugins</strong> (native C# or imported) the model can call. You get a consistent way to expose capabilities, reuse them, and keep them discoverable.</li><li><strong>A real orchestration home</strong><br> Ext.AI standardizes the <strong>call</strong>; SK standardizes the <strong>flow</strong>. SK’s <strong>Kernel</strong> hosts your services and plugins, so multi-step interactions (prompt → tool → prompt) live in one place instead of ad-hoc code paths.</li><li><strong>Modern “planning” without legacy planners</strong><br> The current guidance is to favor <strong>function calling</strong> (with SK driving the loop) over legacy planners; Microsoft documents a <strong>migration</strong> from Stepwise Planner to <strong>Auto Function Calling</strong> because it’s more reliable and token-efficient. SK bakes that orchestration pattern in so you don’t re-implement it.</li><li><strong>Memory &amp; retrieval when you scale</strong><br> When your prompts outgrow a single file, SK’s <strong>memory</strong> abstractions let you plug in vector stores (Azure AI Search, Redis, etc.) without hard-wiring your app to one backend approach.</li><li><strong>Works alongside Ext.AI’s pipelines</strong><br> Keep Ext.AI’s strengths — <strong>streaming</strong>, <strong>telemetry (OpenTelemetry)</strong>, <strong>caching</strong>, and <strong>middleware</strong> — for the transport layer, while SK focuses on orchestration and plugin lifecycle. Together you get clean separation of concerns <em>and</em> observability.</li></ol><h3>If you skip SK (Ext.AI only), what changes?</h3><p>You can ship with Ext.AI alone — but you’ll likely end up rebuilding orchestration glue:</p><ol><li><strong>Ad-hoc tool surface</strong>: You’ll describe and manage callable functions yourself (naming, arguments, versioning), and wire the call/return loop manually.</li><li><strong>Scattered prompt/flow logic</strong>: Multi-step chains (prompt → tool → follow-up) sit in controllers/services without a common orchestration model.</li><li><strong>Harder to expand</strong>: Adding retrieval (“memory”), importing external tools (OpenAPI/MCP), or growing into multi-step agent-like behavior becomes bespoke work.</li><li><strong>Planning migration is on you</strong>: You’ll need to emulate the newer <strong>auto function calling</strong> approach and its retries/guards yourself.</li></ol><p><strong>Rule of thumb:</strong></p><p>If your app is <strong>single-step</strong> (ask → answer) with some middleware, <strong>Ext.AI alone</strong> is fine.</p><p>If you need <strong>repeatable multi-step</strong> flows, <strong>standardized plugins</strong>, optional <strong>memory</strong>, or a path to <strong>agentic</strong> patterns, <strong>add SK</strong> as the orchestration layer.</p><h3>Bonus: structured outputs fit both layers</h3><p>For machine-readable results (like code-doc JSON) you can request <strong>JSON / JSON-schema</strong> at the client layer (Ext.AI), then let SK consume/route the results as another step. Azure’s <strong>Structured outputs</strong> explain the schema guarantees vs. older “JSON mode.”</p><h3>4) Code Demo: Automated C# Documentation with SK + Extensions.AI</h3><p><strong>What the demo does:</strong> This demo showcases a practical “code-to-docs” workflow that takes any C# file as input and generates clean, structured Markdown documentation. It combines Semantic Kernel’s orchestration capabilities with Extensions.AI’s provider-agnostic chat interface to create a tool that automatically documents classes, methods, parameters, and return types.</p><p><strong>The workflow:</strong></p><ol><li><strong>Parse C# source</strong> — A custom SK plugin uses regex patterns to extract symbols (namespace, classes, public methods) from any .cs file</li><li><strong>Structure as JSON</strong> — The parser converts code symbols into structured JSON data</li><li><strong>AI analysis</strong> — SK orchestrates a prompt template that receives the JSON and generates professional documentation</li><li><strong>Markdown output</strong> — The result is clean, consistent documentation ready for wikis, README files, or onboarding materials</li></ol><p><strong>Architecture highlights:</strong></p><ul><li><strong>SK Plugin</strong>: CodeParserPlugin exposes a GetSymbols function that the kernel can invoke</li><li><strong>Prompt orchestration</strong>: SK manages the template that transforms raw code symbols into documentation</li><li><strong>Extensions.AI</strong>: Provides the underlying chat client abstraction, keeping the code provider-agnostic</li></ul><p><strong>Example input</strong> (RcaAgent.cs):</p><pre>public sealed class RcaAgent<br>{<br>    public RcaResult AnalyzeIncident(IncidentData incident, IEnumerable&lt;LogEntry&gt; logs) { ... }<br>    public LogAnalysis AnalyzeLogs(IEnumerable&lt;LogEntry&gt; logs) { ... }<br>    // ... more methods<br>}</pre><h4>Test Output:</h4><pre>dotnet run -- &quot;/path/to/RcaAgent.cs&quot;</pre><h4>Generated Output:</h4><pre># Summary for RcaAgent<br><br>## Classes<br>- **RcaAgent** — Root Cause Analysis Agent that analyzes logs and incidents to identify potential causes<br><br>## Methods<br><br>### AnalyzeIncident<br>`public RcaResult AnalyzeIncident(IncidentData incident, IEnumerable&lt;LogEntry&gt; logs)`<br>- Analyzes the provided incident together with a sequence of log entries and returns an RcaResult.<br>- Parameters:<br>  - incident (IncidentData) — the incident instance to be analyzed<br>  - logs (IEnumerable&lt;LogEntry&gt;) — the sequence of log entries to use in the analysis<br>- Returns: RcaResult — the result of the root-cause analysis<br><br>### AnalyzeLogs<br>`public LogAnalysis AnalyzeLogs(IEnumerable&lt;LogEntry&gt; logs)`<br>- Analyzes the provided sequence of log entries and returns a LogAnalysis.<br>- Parameters:<br>  - logs (IEnumerable&lt;LogEntry&gt;) — the sequence of log entries to analyze  <br>- Returns: LogAnalysis — the outcome of the log analysis</pre><p><strong>Why this demonstrates SK + Extensions.AI value:</strong></p><ul><li><strong>Clean separation</strong>: Extensions.AI handles the model communication, SK handles the orchestration</li><li><strong>Reusable plugins</strong>: The CodeParserPlugin can be reused across different documentation workflows</li><li><strong>Provider flexibility</strong>: Swap from OpenAI to Azure OpenAI to local models without changing business logic</li><li><strong>Structured prompting</strong>: SK’s template engine makes the documentation format consistent and maintainable</li></ul><p><strong>Real-world impact:</strong> This type of automated documentation generation can dramatically improve developer onboarding, code reviews, and knowledge sharing — especially for large codebases where manual documentation lags behind development.</p><h3>5) Enterprise Deployment &amp; Data Governance</h3><h3>The Enterprise Reality</h3><p>Many companies have blocked AI usage due to data governance concerns and the risk of sensitive code being sent to external providers. While this demo uses an OpenAI API key for simplicity, <strong>the entire workflow can run completely offline using Ollama and open-source models</strong> like Llama, CodeLlama, or Mistral.</p><h3>Full Data Control</h3><p>With Ollama, your code never leaves your environment — you maintain full control over your data while still getting the productivity benefits of AI-assisted documentation. The Extensions.AI abstraction makes this provider swap trivial: just change the client configuration, and your orchestration logic remains identical.</p><h3>Enterprise Benefits</h3><ul><li><strong>40–60% reduction</strong> in AI integration code</li><li><strong>Provider switching</strong> in &lt;5 lines of code changes</li><li><strong>Built-in telemetry</strong> reduces debugging time</li><li><strong>Standardized patterns</strong> improve team velocity</li><li><strong>Complete offline capability</strong> for sensitive codebases</li></ul><h3>Common Gotchas</h3><ul><li>SK prompt templates use {{$variable}} not {{variable}}</li><li>Extensions.AI requires List&lt;ChatMessage&gt; not raw strings</li><li>Plugin methods need [KernelFunction] attributes</li></ul><h3>Ready to Try It Yourself?</h3><p><strong>Ready to try it yourself?</strong> The complete working example is available on GitHub: <a href="https://github.com/Ashahet1/AIPoweredLearninwithCSharp/tree/63bc00d4fcea99ca95bbeb4b435db8854aad4b6f/charp-summarizer"><strong>charp-summarizer</strong></a>. Simply clone the repo, plug in any .cs file, and get instant method and class summaries that make onboarding new developers significantly easier. Whether you use OpenAI for quick experiments or Ollama for production privacy, the choice is yours.</p><h3>Conclusion</h3><p>Semantic Kernel + Extensions.AI gives .NET developers a production-ready path to AI integration without vendor lock-in or architectural complexity. SK handles orchestration and workflow patterns, while Extensions.AI provides clean provider abstraction and enterprise-grade middleware.</p><p>For teams building anything beyond simple chat scenarios, this combination delivers the structured, maintainable foundation needed to scale AI features across enterprise applications — with full control over where your data goes.</p><blockquote>💡 If this article added value to your learning, feel free to drop a clap, share it with your peers, or follow me for more deep dives into .NET and software engineering.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a97d18fa538" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Async in C#: Implementing and Consuming TAP (Part 2)]]></title>
            <link>https://medium.com/@shahriddhi717/mastering-async-in-c-implementing-and-consuming-tap-part-2-6cd234d9d937?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cd234d9d937</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 15:29:17 GMT</pubDate>
            <atom:updated>2025-08-18T15:29:17.681Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yFgNtJ0CErAURNB4W_RM5w.png" /><figcaption>Image Generated by ChatGPT</figcaption></figure><p><strong>Intro</strong><br> Once you understand the principles of TAP, the next step is learning how to implement and consume async methods effectively. In this second part, we’ll move from theory into practice — covering compiler-generated magic, cancellation strategies, combinators, and real-world patterns that make async code powerful and expressive.</p><p><strong>Table of Contents</strong></p><ol><li>Implementing TAP Methods: Compiler Magic, Manual Control, or Both</li></ol><blockquote>— Using async/await</blockquote><blockquote>— Using TaskCompletionSource</blockquote><blockquote>— Hybrid Approach</blockquote><p>2. Choosing the Right Tool: CPU-bound vs I/O-bound Workloads.</p><p>3. Consuming TAP: Using Tasks Effectively in Your Code</p><blockquote>— Await semantics</blockquote><blockquote>— ConfigureAwait and context</blockquote><blockquote>— Continuations and Callbacks</blockquote><p>4. Handling Multiple Async Operations: WhenAll and WhenAny</p><p>5. Timeouts with Task.Delay and WhenAny</p><p>6. Built-in Combinators Recap</p><p>7. Rolling Your Own Combinators (Retries, FirstSuccess, Interleaving, etc.)</p><p>8. Async Workflows and Data Structures with Tasks (AsyncCache, AsyncProducerConsumerCollection)</p><p>9. Conclusion: The TAP Story</p><h3>Implementing TAP Methods: Compiler Magic, Manual Control, or Both</h3><p>There are a few ways to implement an async method following TAP:</p><p><strong>-&gt; Use the </strong><strong>async/</strong><strong>await keywords (Compiler-generated state machine)</strong> – This is the most straightforward for many scenarios. You mark a method async Task or async Task&lt;TResult&gt;, write your code with await inside, and the C# compiler transforms it into a state machine that handles the task creation, continuations, exceptions, etc. The compiler does a lot of heavy lifting to ensure your method conforms to TAP.</p><p><strong>-&gt;Manually using </strong><strong>TaskCompletionSource</strong> – In some cases, you might not have await-able operations, or you want more control over the timing and completion of the Task. TaskCompletionSource&lt;TResult&gt; is a utility class that lets you create a Task and complete it later explicitly. You can start some operation (maybe an old-style APM or an event or a timer), and when that operation finishes, you call SetResult or SetException or SetCanceled on the TaskCompletionSource. The Task property of the TaskCompletionSource gives you the Task to return to the caller. This approach is common when wrapping event-based or callback-based APIs into TAP.</p><p><strong>-&gt;A hybrid approach</strong> — Sometimes you combine the two: do some validation or setup manually, then delegate to an inner async method for the core logic. This way you can catch argument errors early (so they throw immediately, as discussed in the Exceptions section) or perhaps return a cached completed task in some cases, while using await for the rest of the work.</p><p>Let’s look at examples of each approach:</p><h3>Using async/await (Compiler-generated TAP)</h3><p>Starting with .NET 4.5, any method marked with async that returns Task or Task&lt;TResult&gt; is transformed by the compiler into a TAP-conformant method. You simply write it as if you were writing synchronous code that waits for other tasks:</p><pre>public async Task&lt;string&gt; GetDataAsync(string url)<br>{<br>    if (url == null) <br>        throw new ArgumentNullException(nameof(url));  // usage error throws immediately<br><br>    HttpClient client = new HttpClient();<br>    string result = await client.GetStringAsync(url);<br>    // The above line asynchronously waits for the HTTP GET to complete.<br>    // Meanwhile, this method is not blocking the calling thread.<br><br>    return result;  // The string result will be available via the returned Task&lt;string&gt;<br>}</pre><p>When you await another task (like GetStringAsync), the compiler generates code to <strong>pause</strong> your method at that point, return a Task to the caller, and resume the method later when the awaited task completes. Any unhandled exception inside the method (after it has returned the task) will be caught and stored into the Task. If an OperationCanceledException bubbles up (and matches the cancellation token, if any), the Task is marked Canceled. This is all automatic – you don’t call SetResult or anything; the async state machine does it.</p><p>The compiler approach is ideal for I/O-bound work where you have naturally async APIs to await. It makes the code linear and easy to write.</p><h3>Manually using TaskCompletionSource</h3><p>Sometimes you might be writing an async method that needs to interface with older asynchronous patterns or other events. In such cases, TaskCompletionSource&lt;TResult&gt; is your friend. It essentially lets you <strong>create a Task that you control</strong>. You get a TaskCompletionSource&lt;TResult&gt; object, from which you get a Task (let’s call it tcs.Task). You then start whatever async operation you need (maybe a timer or a network request using an event or a callback). And when that operation finishes, you call:</p><p>-&gt;tcs.SetResult(resultValue) if it succeeded (which transitions the task to RanToCompletion with that result),</p><p>-&gt;tcs.SetException(exception) if it failed (transitions task to Faulted with that exception), or</p><p>-&gt;tcs.SetCanceled() if it was canceled (transitions task to Canceled).</p><p>If you’re not sure whether an operation might complete synchronously or asynchronously, or possibly complete multiple times, TaskCompletionSource also has TrySetResult/Exception/Canceled variants that return false if the task was already completed by a previous call.</p><p><strong>Example:</strong> Wrapping an APM-style BeginRead/EndRead into a TAP method:</p><pre>public static Task&lt;int&gt; ReadAsync(Stream stream, byte[] buffer, int offset, int count)<br>{<br>    var tcs = new TaskCompletionSource&lt;int&gt;();<br><br>    // Start the old-style asynchronous operation<br>    stream.BeginRead(buffer, offset, count, asyncResult =&gt;<br>    {<br>        try <br>        {<br>            // Complete the operation<br>            int bytesRead = stream.EndRead(asyncResult);<br>            tcs.SetResult(bytesRead);<br>        }<br>        catch (Exception ex) <br>        {<br>            tcs.SetException(ex);<br>        }<br>    }, null);<br><br>    return tcs.Task; // Return the Task&lt;int&gt; that represents the async read<br>}</pre><h3>Hybrid Approach</h3><p>The hybrid approach is basically using a small non-async method as a wrapper for an inner async method. A common use case is <strong>fast-path optimizations or pre-checks</strong>. For example:</p><pre>public Task&lt;int&gt; ComputeValueAsync(string input)<br>{<br>    if (input == null)<br>        throw new ArgumentNullException(nameof(input)); // synchronous validation<br><br>    // Maybe we have a cache of results:<br>    if (_cache.TryGetValue(input, out int cached))<br>    {<br>        // return completed task if available (fast-path)<br>        return Task.FromResult(cached);<br>    }<br><br>    // Otherwise, delegate to the actual async implementation<br>    return ComputeValueAsyncInternal(input);<br>}<br><br>private async Task&lt;int&gt; ComputeValueAsyncInternal(string input)<br>{<br>    // ... perform the asynchronous computation (e.g. I/O calls, awaits, etc.)<br>    int result = await ReallyComputeAsync(input);<br>    return result;<br>}</pre><p>Here, the public method does quick checks (null input, cache lookup). If it can resolve immediately, it returns a completed Task via Task.FromResult. Otherwise, it calls the inner method which is implemented with async. This pattern ensures that certain errors throw immediately and certain results are returned immediately, without the overhead of the async state machine if not needed. But any heavy lifting is done in the inner method asynchronously.</p><p>Another scenario for hybrid is when implementing <strong>overloads</strong>. Perhaps you have Task DoStuffAsync() and you want to implement Task DoStuffAsync(CancellationToken ct) by calling the parameterless version internally but attaching a continuation or something. Usually, you’d implement one in terms of the other to avoid code duplication.</p><h3>Choosing the Right Tool: CPU-bound vs I/O-bound Workloads</h3><p>Not all asynchronous operations are created equal. Broadly, we categorize async work as either <strong>CPU-bound</strong> (compute-intensive tasks) or <strong>I/O-bound</strong> (waiting on external resources like disk, network, database, etc.). TAP can handle both, but the approach to implement them differs:</p><p><strong>I/O-bound tasks</strong> are the poster child for async/await. In I/O, often the thread spends a lot of time just <em>waiting</em> for data to arrive or be sent (during which the CPU can do other things). Using TAP with true asynchronous I/O (like network streams, HttpClient, file streams in async mode, etc.) means you free up threads during the wait. The actual work is largely happening in hardware or the OS. For I/O-bound tasks, you’ll typically rely on naturally async APIs or use TaskCompletionSource as shown, rather than chewing up threads.</p><p><strong>CPU-bound tasks</strong> actually consume CPU cycles to perform calculations. If you have something computationally heavy (image processing, large loops, etc.), using await doesn’t make the CPU go any faster – you’re actually going to use another thread to do that work so that the original thread can be released (keeping the app responsive). Here, TAP is useful to offload work to a background thread and to easily get the result back when it’s done, but it doesn’t save resources the way async I/O does. You still pay for a thread to do the work.</p><p><strong>Implementing CPU-bound work:</strong> The easiest way is to use Task.Run to spin up a task on the thread pool. For example:</p><pre>public Task&lt;long&gt; SumLargeArrayAsync(int[] array, CancellationToken cancellationToken = default)<br>{<br>    return Task.Run(() =&gt; {<br>        long sum = 0;<br>        for(int i = 0; i &lt; array.Length; i++)<br>        {<br>            cancellationToken.ThrowIfCancellationRequested();<br>            sum += array[i];<br>        }<br>        return sum;<br>    }, cancellationToken);<br>}</pre><p>In this snippet, we create a task on the thread pool that computes the sum of a large array. We also periodically call ThrowIfCancellationRequested() to stop if cancellation is signaled. Wrapping the loop in Task.Run means the work runs on a background thread from the pool, and the calling thread is free immediately to do other things (like update a progress bar or remain responsive).</p><p>Under the hood, Task.Run uses TaskFactory.StartNew with some defaults to queue the work. You could also use TaskFactory.StartNew directly if you need more control (like specifying a custom TaskScheduler or different TaskCreationOptions). However, in modern code, Task.Run covers most needs for launching CPU work.</p><p><strong>Implementing I/O-bound work:</strong> If an API is naturally awaitable (like any method that already returns Task, e.g., Stream.ReadAsync or HttpClient.SendAsync), just call and await it. If not, and if it uses events/callbacks or has a synchronous version only, you might use the TaskCompletionSource approach to wrap it.</p><p>Another neat use of TaskCompletionSource for I/O scenarios is creating things like a delay without blocking threads (essentially a sleep that doesn’t tie up a thread). The .NET library actually provides Task.Delay which does exactly that by using a timer internally. To illustrate, here’s how you could implement a simple delay with a timer:</p><pre>public Task DelayAsync(int millisecondsDelay)<br>{<br>    var tcs = new TaskCompletionSource&lt;bool&gt;();<br>    var timer = new Timer(_ =&gt; {<br>        // Timer callback fires on a ThreadPool thread after the delay<br>        timer.Dispose();<br>        tcs.TrySetResult(true);<br>    }, null, Timeout.Infinite, Timeout.Infinite);<br><br>    timer.Change(millisecondsDelay, Timeout.Infinite);<br>    return tcs.Task;<br>}</pre><p>We create a Timer (from System.Threading) that after the specified timeout will dispose itself and set the result of the TaskCompletionSource to true (we don’t actually care about the boolean value, it’s just a dummy). The Task we return will complete after the given delay, without blocking any threads in the meantime. (Again, you’d normally just use Task.Delay, but this shows how TCS can turn an event-based callback into a task.)</p><p><strong>Mixing both worlds:</strong> Many real operations involve a bit of I/O and a bit of processing. For example, downloading data (I/O) and then parsing it (CPU). With TAP, you can seamlessly mix them: use await for the I/O-bound parts, and if needed use Task.Run for the CPU-bound parts (or simply execute them if they’re not too heavy, as they’ll be on a background thread if you awaited something before on the thread pool). The result is you can compose these operations as a sequence of awaits:</p><pre>public async Task&lt;Bitmap&gt; DownloadAndProcessImageAsync(string url, CancellationToken ct)<br>{<br>    // I/O-bound: download image data from a URL<br>    byte[] imageData = await httpClient.GetByteArrayAsync(url, ct);<br>    // CPU-bound: process the image data (on thread pool by Task.Run)<br>    Bitmap image = await Task.Run(() =&gt; ProcessImage(imageData), ct);<br>    return image;<br>}</pre><p>In this example, after the GetByteArrayAsync await, the method resumes on some thread pool thread (because HttpClient continuations by default don’t go back to the context, or if called from a context where you used .ConfigureAwait(false)). We then do a CPU-heavy ProcessImage on the thread pool explicitly. We use the same cancellation token for both steps. If cancellation is requested early, the download may cancel; even if it isn’t requested until the processing step, the ThrowIfCancellationRequested inside Task.Run (as shown earlier) would catch it.</p><h3>Consuming TAP: Using Tasks Effectively in Your Code</h3><p>Now that we have a good handle on writing TAP methods, let’s flip to the perspective of someone <em>calling</em> these methods. The introduction of tasks and the await keyword made consuming async operations as simple as writing synchronous code, but understanding what goes on under the hood helps you avoid pitfalls.</p><h3>await – Syntactic Sugar with Powerful Semantics</h3><p>The await keyword is the primary way to consume tasks in an asynchronous manner. When you await someTask, a few things happen:</p><p>-&gt;If the task is not yet completed, the <em>current async method</em> is <strong>paused</strong>. Control is returned to the caller of the async method, but not as a blocking wait — instead, the remainder of the method is scheduled to resume later.</p><p>-&gt;While paused, the state (local variables, etc.) of your method is preserved (thanks to the compiler-generated state machine).</p><p>-&gt;When the task completes, the async method is resumed. If the task completed successfully (RanToCompletion), the await expression gives you the result (for Task&lt;TResult&gt;) or just continues if it&#39;s a Task. If the task was Faulted, the exception is re-thrown at the point of the await. If Canceled, an OperationCanceledException is thrown at that point.</p><p>-&gt;By default, await also captures the <strong>SynchronizationContext</strong> (or the current TaskScheduler if no context) that was present at the moment of awaiting, and it uses that to resume the method. This means in a UI app, after awaiting an asynchronous operation, you resume on the UI thread automatically (very convenient for updating UI with results). In a library or backend context where there’s no special context, it resumes on a thread pool thread (which might end up being the same thread that completed the task).</p><p>Because of this behavior, await helps you write code that looks sequential but doesn’t block threads. For example:</p><pre>// UI context here (e.g., button click handler on UI thread)<br>await Task.Delay(5000); <br>// UI context is captured, resume here on UI thread after 5 seconds without blocking that thread.<br>MessageBox.Show(&quot;5 seconds passed!&quot;);</pre><p>During those 5 seconds, the UI thread was free to handle other events. await made it possible.</p><p>One nuance: if a Task is already completed by the time you await it, await will not actually pause at all – it will continue synchronously. This is an optimization so you don’t incur overhead if the data was, say, in cache and returned immediately (the Task would be complete and awaiting it is near-instant).</p><h3>ConfigureAwait and Context</h3><p>Sometimes, you <em>don’t want</em> to capture the context with await. For example, in library code or server-side code, hopping back to a specific context (that might not even exist or might just be the thread pool anyway) is unnecessary. In these cases, you use ConfigureAwait(false) on a task before awaiting it:</p><pre>string data = await httpClient.GetStringAsync(url).ConfigureAwait(false);</pre><p>By specifying false, you’re telling the await: “Don’t bother capturing the context – just resume on whatever thread is available.” This can improve performance by avoiding context switches. It’s generally recommended to use ConfigureAwait(false) in library code (so that you don’t tie your library’s internals to the consumer’s context), and in any scenario where you don’t need to resume on the original context. On UI code, though, you typically omit it so that after awaiting a background operation you can safely update UI controls (which require the UI thread).</p><h3>Waiting Without await: Continuations and Callbacks</h3><p>While await is the cleanest way to consume tasks, the Task Parallel Library also offers methods like ContinueWith for attaching continuations. For example:</p><pre>Task&lt;int&gt; computeTask = ComputeSumAsync();<br>computeTask.ContinueWith(t =&gt; {<br>    if (t.IsFaulted) { /* handle error */ }<br>    else { int result = t.Result; /* use result */ }<br>});</pre><p>Continuations can specify TaskScheduler (to control what thread or context runs the continuation), and you can chain multiple tasks in interesting ways. However, in modern code, explicit ContinueWith is less common except for advanced scenarios — await essentially compiles into a continuation under the hood.</p><h3>Handling Multiple Async Operations: WhenAll and WhenAny</h3><p>Often you might have to kick off multiple async operations and wait for all or any of them. The Task.WhenAll and Task.WhenAny static methods are combinators that help with these scenarios.</p><p><strong>-&gt; Task.WhenAll</strong>: Accepts a collection of tasks and returns a new Task that completes when <em>all</em> of them have completed. If all succeed, the resulting Task is RanToCompletion, and if it’s the Task&lt;TResult[]&gt; overload, it will contain an array of all results. If any fail, the resulting task is Faulted (with an AggregateException of all errors). If any are canceled (and none faulted), the resulting task is Canceled.</p><p>Example — waiting for several independent web downloads in parallel:</p><pre>Task&lt;string&gt; t1 = DownloadStringAsync(url1);<br>Task&lt;string&gt; t2 = DownloadStringAsync(url2);<br>Task&lt;string&gt; t3 = DownloadStringAsync(url3);<br>string[] results;<br>try<br>{<br>    results = await Task.WhenAll(t1, t2, t3);<br>    // Now use results[0], results[1], results[2]<br>}<br>catch(Exception ex)<br>{<br>    // If any of t1, t2, t3 failed or was canceled, we end up here<br>    // ex might be an AggregateException; if awaited, ex would be the first inner exception.<br>}</pre><p>One thing to note: if multiple tasks fault, and you await WhenAll, only one exception is rethrown. If you need to see all, you’d have to catch AggregateException or inspect each task’s Exception property. In most cases, one failure is enough to know the whole operation failed, but advanced handling might require looking at all.</p><p><strong>-&gt;Task.WhenAny</strong>: Returns a Task that completes when <em>any one</em> of the given tasks completes. It doesn’t unwrap the result; instead, the result of WhenAny is a Task&lt;Task&gt; (or specifically Task&lt;Task&lt;T&gt;&gt; for tasks with result) — essentially, it gives you the task that finished first. You can then check which one it was, and what its result or error was.</p><p>Example — you want to fetch data from redundant servers and use whichever responds first:</p><pre>Task&lt;string&gt; primary = DownloadStringAsync(primaryUrl);<br>Task&lt;string&gt; backup = DownloadStringAsync(backupUrl);<br>Task&lt;string&gt; primary = DownloadStringAsync(primaryUrl);<br>Task&lt;string&gt; backup = DownloadStringAsync(backupUrl);<br><br>Task&lt;string&gt; firstFinished = await Task.WhenAny(primary, backup);<br>// firstFinished is either &#39;primary&#39; or &#39;backup&#39; task, whichever completed first.<br><br>try<br>{<br>    string data = await firstFinished;  // Await the winner to either get result or exception<br>    // Use the data from the faster source<br>}<br>catch(Exception ex)<br>{<br>    // The first task that finished actually failed (e.g., threw WebException).<br>    // You might decide to await the other one instead, or handle the error.<br>}<br>finally<br>{<br>    // Optionally, cancel the slower task if it&#39;s still running<br>    cts.Cancel(); // assuming you used a CancellationTokenSource for both<br>}</pre><p>This pattern is useful for <strong>racing tasks</strong> or implementing timeouts (racing an operation against Task.Delay). We’ll see an example of that soon.</p><p><strong>Processing tasks as they finish (Interleaving):</strong> If you have a large collection of tasks and you want to handle results as each task completes (rather than waiting for all at once), you might loop using WhenAny:</p><pre>List&lt;Task&lt;Image&gt;&gt; imageDownloads = urls.Select(u =&gt; DownloadImageAsync(u)).ToList();<br>while(imageDownloads.Any())<br>{<br>    Task&lt;Image&gt; finished = await Task.WhenAny(imageDownloads);<br>    imageDownloads.Remove(finished);<br>    try <br>    {<br>        Image img = await finished;<br>        DisplayImage(img);<br>    }<br>    catch(Exception ex)<br>    {<br>        Console.WriteLine($&quot;One download failed: {ex.Message}&quot;);<br>    }<br>}</pre><p>This way, the UI (or processing) gets each image as soon as it’s available, without waiting for the slowest download to finish.</p><p>However, as the number of tasks grows, looping with WhenAny can become inefficient because each iteration registers continuations on all remaining tasks. For large sets, a more efficient approach uses techniques beyond this article’s scope (like the custom Interleaved combinator using TaskCompletionSource to avoid O(N^2) behavior).</p><p><strong>Throttling concurrency:</strong> Sometimes you have a huge number of operations and want only a certain number in flight at once (to avoid saturating network or hitting resource limits). A pattern for that is:</p><ol><li>Start a fixed number of tasks (say 5 downloads).</li><li>Each time one finishes, start the next one from the queue.</li><li>Continue until all are done.</li></ol><p>The code would combine WhenAny with tracking an index or using something like a SemaphoreSlim to limit concurrency. For brevity, we won’t show the full code here, but .NET’s System.Threading.Channels or Dataflow library can also help implement producer/consumer patterns for throttling. The key is to balance work so as not to overload the system or violate any concurrent usage limits.</p><h3>Timeouts with Task.Delay and WhenAny</h3><p>Implementing a timeout for an async operation is a great demonstration of combinators. Since Task.WhenAny can wait for either of two tasks, you can race an operation against a delay:</p><pre>Task&lt;string&gt; downloadTask = DownloadStringAsync(url);<br>Task timeoutTask = Task.Delay(3000);<br>if (downloadTask == await Task.WhenAny(downloadTask, timeoutTask))<br>{<br>    // The downloadTask completed before the timeoutTask<br>    string result = await downloadTask; // This will immediately yield result since it&#39;s completed<br>    Console.WriteLine(&quot;Download succeeded within 3s&quot;);<br>}<br>else<br>{<br>    // The timeoutTask finished first (after 3 seconds), meaning downloadTask is still running<br>    Console.WriteLine(&quot;Operation timed out. Cancelling download...&quot;);<br>    // Ideally cancel the download task if you can<br>    cts.Cancel(); // assuming the downloadTask is cancelable with this token<br>}</pre><p>In a UI context, you might use this pattern to re-enable a button or update status if something takes too long, while still allowing the background task to complete or get canceled.</p><p>The .NET library doesn’t provide a direct Task.WhenAny with timeout, but the above is basically how you implement it. Note: if the operation times out and you cancel it, you might still want to observe its completion (to handle any exceptions or just to know when it actually stops). Ensure that you don&#39;t just abandon a running task without either canceling it or handling its completion in some way (like attaching a continuation to log if it eventually errors out, as shown with ContinueWith examples in documentation).</p><h3>Built-in Combinators Recap</h3><p>We’ve touched on Task.WhenAll, Task.WhenAny, and Task.Delay. Another handy one is Task.FromResult (or Task.FromException, Task.FromCanceled) to create already-completed tasks. These are useful for quick returns or in testing.</p><p>Example:</p><pre>if (cache.TryGetValue(key, out var value))<br>    return Task.FromResult(value);<br>// else do async fetch...</pre><p>This returns a completed task immediately if the value was cached, allowing the caller to still use await consistently.</p><h3>Rolling Your Own Combinators</h3><p>Because tasks are so flexible, developers often write helper methods to orchestrate them in common patterns:</p><p><strong>-&gt; Retry logic:</strong> Suppose you want to retry an operation a few times if it fails. You can write an RetryAsync method that takes a function returning a Task and a retry count. It can loop calling the function and catching exceptions, returning when one attempt succeeds or after exhausting retries.</p><pre>public static async Task&lt;T&gt; RetryOnFaultAsync&lt;T&gt;(Func&lt;Task&lt;T&gt;&gt; operation, int maxTries)<br>{<br>    for(int attempt = 1; attempt &lt;= maxTries; attempt++)<br>    {<br>        try<br>        {<br>            return await operation().ConfigureAwait(false);<br>        }<br>        catch when (attempt == maxTries)<br>        {<br>            // If it&#39;s the last attempt, let the exception bubble out<br>            throw;<br>        }<br>        // Otherwise, swallow the exception and loop to retry<br>    }<br>    return default; // we&#39;ll never actually hit this<br>}</pre><p>You could even add a delay between retries or a backoff strategy by awaiting a Task.Delay in the loop. This combinator can be used like:</p><pre>string page = await RetryOnFaultAsync(() =&gt; DownloadStringAsync(url), 3);</pre><p>to try downloading up to 3 times.</p><ul><li><strong>First-success wins:</strong> We discussed using WhenAny for racing tasks. A helper could wrap that pattern of launching multiple tasks with a shared cancellation token and canceling the losers when one succeeds:</li></ul><pre>public static async Task&lt;T&gt; FirstSuccessAsync&lt;T&gt;(IEnumerable&lt;Func&lt;CancellationToken, Task&lt;T&gt;&gt;&gt; operations, CancellationToken externalToken = default)<br>{<br>    using var cts = CancellationTokenSource.CreateLinkedTokenSource(externalToken);<br>    var token = cts.Token;<br>    var tasks = operations.Select(op =&gt; op(token)).ToList();<br>Task&lt;T&gt; firstFinished;<br>    try<br>    {<br>        firstFinished = await Task.WhenAny(tasks);<br>    }<br>    catch<br>    {<br>        // If WhenAny somehow throws (shouldn&#39;t normally), cancel all and rethrow<br>        cts.Cancel();<br>        throw;<br>    }<br><br>    // Cancel all other operations<br>    cts.Cancel();<br><br>    // Observe any failures of the other tasks to avoid unobserved exceptions<br>    foreach(var t in tasks)<br>    {<br>        if (t != firstFinished)<br>            t.ContinueWith(task =&gt; { var ignored = task.Exception; }, TaskContinuationOptions.OnlyOnFaulted);<br>    }<br><br>    // Now await the winning task to get its result or exception<br>    return await firstFinished;<br>}</pre><p>This is a bit heavy, but it ensures that once one task succeeds, the others are canceled and any exceptions from those others are handled (logged or observed). You’d use it like:</p><pre>double price = await FirstSuccessAsync(new [] {<br>    (CancellationToken ct) =&gt; GetStockPriceFromServer1Async(&quot;MSFT&quot;, ct),<br>    (CancellationToken ct) =&gt; GetStockPriceFromServer2Async(&quot;MSFT&quot;, ct),<br>    (CancellationToken ct) =&gt; GetStockPriceFromServer3Async(&quot;MSFT&quot;, ct)<br>});</pre><p>Whichever server responds first gives the price; the others are canceled.</p><p><strong>-&gt; Custom interleaving:</strong> As mentioned, the naive WhenAny loop can scale poorly for many tasks. An advanced combinator can create an enumerable of tasks that yield results in the order completed, without quadratic behavior. This typically uses a TaskCompletionSource for each original task and attaches continuations that complete them in order. The end result is you get a collection of tasks that you can await in sequence to get the fastest result each time. This is an advanced scenario – if you find yourself needing it, you likely know enough to implement or search for “interleaved tasks pattern”.</p><p><strong>-&gt; WhenAllOrFirstException:</strong> Another combinator pattern is waiting for all tasks, but if any task fails early, you want to short-circuit and get the error immediately rather than waiting for the rest. This can be done with a combination of Task.WhenAny and Task.WhenAll, or a manual approach: create a TaskCompletionSource, attach continuations to each task that either propagate the first exception or mark completion when all have succeeded. Again, an advanced scenario but possible to implement using the building blocks of tasks.</p><p>The key point with all these custom patterns is: <strong>Tasks are building blocks.</strong> Because a Task represents an operation, you can mix them, race them, chain them, and group them with great flexibility. TAP, combined with await, gives you a high-level way to use these patterns without getting lost in callbacks.</p><h3>Async Workflows and Data Structures with Tasks</h3><p>Tasks aren’t just for individual operations; they can also be part of <strong>asynchronous workflows and data structures</strong>:</p><p><strong>-&gt;Async Cache:</strong> Suppose you want to cache results of an expensive async call so it’s only done once. You can store a Task&lt;TResult&gt; in the cache instead of the raw result. That way, if a second caller wants the same data while the first call is still in progress, they can just await the same Task rather than starting a duplicate operation. This ensures you don’t kick off redundant work.</p><p>For example, an async cache could use a ConcurrentDictionary of keys to Lazy&lt;Task&lt;TValue&gt;&gt; entries. The first time a key is requested, you add a Lazy that, when evaluated, will run the async factory function. Further callers reuse that same Task. A simplified version:</p><pre>class AsyncCache&lt;TKey, TValue&gt;<br>{<br>    private readonly ConcurrentDictionary&lt;TKey, Lazy&lt;Task&lt;TValue&gt;&gt;&gt; _cache = new ConcurrentDictionary&lt;TKey, Lazy&lt;Task&lt;TValue&gt;&gt;&gt;();<br>    private readonly Func&lt;TKey, Task&lt;TValue&gt;&gt; _valueFactory;<br>    public AsyncCache(Func&lt;TKey, Task&lt;TValue&gt;&gt; valueFactory)<br>    {<br>        _valueFactory = valueFactory;<br>    }<br><br>    public Task&lt;TValue&gt; GetValueAsync(TKey key)<br>    {<br>        var lazy = _cache.GetOrAdd(key, k =&gt; <br>            new Lazy&lt;Task&lt;TValue&gt;&gt;(() =&gt; _valueFactory(k))<br>        );<br>        return lazy.Value;<br>    }<br>}</pre><p>Now, if two calls come in for the same key before the first has completed, the second will wait on the same Task. Once it’s done, both get the result, and it’s cached for future. This technique uses tasks as a form of <strong>memoization</strong> for async operations.</p><p><strong>Producer/Consumer with Tasks:</strong> In concurrency, producer-consumer is a common pattern. We can make an async version using a queue and tasks. One approach:</p><p>-&gt; Producers call an Add(item) method to add work.</p><p>-&gt; Consumers call a TakeAsync() method that returns a Task which completes when an item is available (or immediately if one is already in the queue).</p><p>-&gt; Using TaskCompletionSource, we can implement TakeAsync such that if the queue is empty, it gives back a task that will be completed when an item is added.</p><p>For instance:</p><pre>class AsyncQueue&lt;T&gt;<br>{<br>    private readonly Queue&lt;T&gt; _items = new Queue&lt;T&gt;();<br>    private readonly Queue&lt;TaskCompletionSource&lt;T&gt;&gt; _waiters = new Queue&lt;TaskCompletionSource&lt;T&gt;&gt;();<br><br>    public void Enqueue(T item)<br>    {<br>        TaskCompletionSource&lt;T&gt; waiter = null;<br>        lock (_items)<br>        {<br>            if (_waiters.Count &gt; 0)<br>            {<br>                waiter = _waiters.Dequeue();<br>            }<br>            else<br>            {<br>                _items.Enqueue(item);<br>            }<br>        }<br>        // Fulfill a waiting consumer if present<br>        waiter?.SetResult(item);<br>    }<br><br>    public Task&lt;T&gt; DequeueAsync()<br>    {<br>        lock (_items)<br>        {<br>            if (_items.Count &gt; 0)<br>            {<br>                // Immediate availability<br>                return Task.FromResult(_items.Dequeue());<br>            }<br>            else<br>            {<br>                // No item, return a task that will complete later<br>                var tcs = new TaskCompletionSource&lt;T&gt;();<br>                _waiters.Enqueue(tcs);<br>                return tcs.Task;<br>            }<br>        }<br>    }<br>}</pre><p>.NET actually provides higher-level constructs like BufferBlock&lt;T&gt; in the Dataflow library (part of TPL Dataflow) which do similar things with more features (like bounding, etc.), but the above illustrates how tasks and TCS enable custom async coordination structures.</p><h3>Conclusion: The TAP Story</h3><p>We started with the motivations behind the Task-based Asynchronous Pattern — a unification and simplification of earlier async models. We saw how a single method returning a Task can elegantly represent an asynchronous operation, with language support making it simple to consume. We walked through designing TAP methods with proper naming and signatures, handling cancellations and progress, and ensuring tasks are used properly (hot tasks, minimal sync work). We then dug into implementing asynchronous methods either with the help of C#’s async/await or with lower-level primitives like TaskCompletionSource for cases where we need more control.</p><p>On the consuming side, we explored how await makes asynchronous calls look like sequential code and how tasks can be combined and orchestrated – waiting for all, for any, handling exceptions, canceling, and even building higher-level patterns like timeouts, retries, and first-wins logic. We also glimpsed how tasks can form the backbone of async data structures like caches and queues.</p><p>The <strong>Task-based Asynchronous Pattern (TAP)</strong> has truly transformed how .NET developers write concurrent and asynchronous code. It enables writing code that is both efficient and easier to understand, avoiding the “callback hell” of previous patterns. By mastering TAP, you’re well-equipped to handle the complexities of modern applications — whether it’s keeping a UI fluid or scaling a high-throughput server — in a manageable and composable way.</p><blockquote>💡 If this article added value to your learning, feel free to drop a clap, share it with your peers, or follow me for more deep dives into .NET and software engineering.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cd234d9d937" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Async in C#: A Deep Dive into TAP (Part 1)]]></title>
            <link>https://medium.com/@shahriddhi717/mastering-async-in-c-a-deep-dive-into-tap-part-1-b300071a43fd?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b300071a43fd</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 15:27:51 GMT</pubDate>
            <atom:updated>2025-08-18T15:27:51.855Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4JYUacDSYaAU310tN4VnQ.png" /><figcaption>Image was generated with ChatGPT</figcaption></figure><p><strong>Intro</strong><br> Asynchronous programming has come a long way in .NET, evolving from complex callback-based approaches to the elegant Task-based Asynchronous Pattern (TAP). For seasoned developers, understanding TAP isn’t just about writing non-blocking code — it’s about designing scalable, maintainable, and intuitive systems. In this first part, we’ll explore the foundations of TAP, its conventions, and its building blocks before diving into implementation in Part 2.</p><p><strong>Table of Contents</strong></p><ol><li>The Evolution of Async Patterns: From APM/EAP to TAP</li><li>TAP Method Naming Conventions: The “Async” Suffix and Friends.</li><li>Initiating Async Operations: The Quick Launch.</li><li>Exceptions: Dealing with Errors in Async Methods.</li><li>Where Does the Work Run? Understanding Task Execution Context.</li><li>The Life of a Task: Hot vs Cold and TaskStatus.</li><li>Cancellation: Giving Up on an Async Operation.</li><li>Reporting Progress: Are We There Yet?</li><li>Designing Overloads: Cancellation and Progress</li></ol><h3>The Evolution of Async Patterns: From APM/EAP to TAP</h3><p>Picture a .NET developer circa 2008. To read from a stream asynchronously, you might write BeginRead and EndRead methods (the APM pattern) or fire off a ReadAsync method with a corresponding completion event (the EAP pattern). These older patterns worked but were <strong>cumbersome and error-prone</strong>. Enter TAP – introduced with .NET Framework 4 – which uses the Task type to represent the entire asynchronous operation in one fell swoop. With TAP, <strong>one method does it all</strong>: you call an Async method and get back a Task (or Task&lt;TResult&gt;) that represents the ongoing work. No more pairing methods or custom event handlers; just <strong>await the task</strong>, and you’re done.</p><p><strong>Why TAP became the go-to pattern:</strong></p><p><strong>-&gt; Simplicity:</strong> TAP condenses async operations into a single method that returns a Task. This unification makes code easier to read and write, especially with language support like the async/await keywords.</p><p><strong>-&gt; Consistency:</strong> Since .NET Framework 4, TAP is the <em>recommended</em> approach for new async APIs. It leverages the System.Threading.Tasks library, which provides a robust infrastructure for concurrency.</p><p><strong>-&gt; Language Integration:</strong> C# (async/await), VB (Async/Await), and F# have built-in support for TAP, making asynchronous code look almost like synchronous code, without explicit callbacks or thread management.</p><p><strong>A quick comparison</strong> — Suppose we want to provide an asynchronous Read operation in a class:</p><p><strong>-&gt; APM (IAsyncResult model):</strong> Expose BeginRead(...) and EndRead(...) methods. The caller begins the operation and later completes it by calling End with the returned IAsyncResult.</p><p><strong>-&gt; EAP (Event-based model):</strong> Expose a ReadAsync(...) method (void return) and a ReadCompleted event (with an EventArgs carrying results). The caller initiates and handles completion via the event.</p><p><strong>-&gt; TAP (Task-based):</strong> Expose a single ReadAsync(...) that returns Task&lt;int&gt;. The caller awaits the task to get the result when it’s ready.</p><p>TAP clearly wins on clarity and ease of use. Now, let’s dive deeper into how to name TAP methods and what their signatures look like.</p><h3>TAP Method Naming Conventions: The “Async” Suffix and Friends</h3><p>One hallmark of TAP methods is the <strong>Async suffix</strong> in their names. If you have a method GetData that synchronously returns a string, the asynchronous version should be called GetDataAsync and return a Task&lt;string&gt;. This naming makes it immediately obvious to callers that the method is asynchronous and needs to be awaited or otherwise handled asynchronously.</p><p>A few guidelines for naming and signatures of TAP methods:</p><p><strong>-&gt; Use </strong><strong>Async suffix</strong> for methods returning awaitable types (Task, Task&lt;TResult&gt;, ValueTask, etc.) E.g., ReadAsync, SaveAsync, CalculateAsync.</p><p><strong>-&gt; Avoid </strong><strong>Async suffix</strong> on methods that <em>start</em> an async operation but don’t return a task. Those rare cases should use verbs like Begin or Start to indicate they won&#39;t directly return a result to await. For example, a method that simply fires off work and returns void (not common in modern APIs) might be named BeginUpload rather than UploadAsync (since there’s nothing to await).</p><p><strong>-&gt; Task vs Task&lt;TResult&gt;:</strong> If the synchronous counterpart returns void, the TAP version returns a Task (with no result). If the sync method returns a type TResult, the TAP version returns Task&lt;TResult&gt; of that type. This way, the result of the operation (if any) will be available as the Result of the task or via await.</p><p><strong>-&gt; Match parameters of sync methods:</strong> Generally, the async method should take the same parameters as the synchronous version, in the same order. But there’s <strong>one big exception</strong>: any out or ref parameters. Since tasks can return only one object, multiple outputs should be bundled into a tuple or a custom class as the TResult of a Task&lt;TResult&gt;. TAP avoids out and ref entirely by using richer return types.</p><p><strong>-&gt; Cancellation tokens are encouraged:</strong> Even if the synchronous version doesn’t have a cancellation mechanism, consider adding a CancellationToken parameter to the async method. This gives callers the ability to cancel the operation if needed (more on cancellation below).</p><p><strong>-&gt; Progress reporting (optional):</strong> If your operation is lengthy and can report intermediate progress (say download percentage or bytes processed), you might offer an overload that accepts an IProgress&lt;T&gt; to publish progress updates. We’ll discuss this in detail in the Progress section.</p><p><strong>What about methods that combine tasks?</strong> Those are often called <strong>combinators</strong> — for example, Task.WhenAll or Task.WhenAny don’t represent a single asynchronous operation but rather operate on multiple tasks. Combinators typically have &quot;all&quot; or &quot;any&quot; in their name and don’t necessarily follow the Async suffix rule because their intent is obvious by context. If you create utility methods that work only with tasks (like a custom RetryAsync method that retries a given task operation), you have flexibility in naming – clarity trumps the suffix rule in those cases.</p><h3>Initiating Async Operations: The Quick Launch</h3><p>A TAP method often does a <strong>little bit of work synchronously</strong> at the start, then promptly kicks off the real asynchronous operation. This is by design — you want the method to return quickly with a task, especially if it’s called on a UI thread. Any <strong>lengthy preparation or validation should be minimized</strong> in the method body, because:</p><p><strong>-&gt;UI responsiveness:</strong> If an async method is invoked on the UI thread (common in client apps), doing too much before returning the task can freeze the interface. Users won’t appreciate a button that stutters because your DoWorkAsync method is crunching numbers <em>before</em> it actually goes async.</p><p><strong>-&gt; Concurrency:</strong> Often you might launch several async operations in parallel. If each of them bogs down doing prep work, you lose the benefit of concurrency. Quick returns let other operations start without delay.</p><p><strong>-&gt; Fast-path completions:</strong> Sometimes the operation might be so fast it can complete synchronously <em>inside</em> the async method. For example, imagine a ReadAsync that finds the requested data already in memory buffer – it could just set up the result and complete the task immediately, saving the overhead of context switching. TAP allows for this by returning an <em>already-completed</em> Task if the work is done immediately. From the caller’s perspective it’s still an async method, but it may complete almost instantaneously.</p><p><strong>How to do a quick launch?</strong> Usually, you’ll perform basic argument checks (throwing ArgumentNullException for invalid input, etc.), maybe log something, and then start the asynchronous work. As soon as the async operation is initiated (say by calling an asynchronous I/O method or queuing a work item), you return the Task representing that operation. The heavy lifting happens behind the scenes (often on other threads or by the OS), while your method has already handed a task to the caller.</p><h3>Exceptions: Dealing with Errors in Async Methods</h3><p>Error handling in TAP follows a simple principle: <strong>usage errors throw immediately, operational errors go to the Task</strong>. What does that mean? If a caller misuses your method (e.g., passing null where it isn’t allowed), you should throw an exception <em>synchronously</em>, just as a regular method would. Those exceptions (often ArgumentException, ArgumentNullException, etc.) indicate a bug or misuse and should be caught during development – they shouldn’t be part of normal runtime flow.</p><p>All other exceptions that occur <strong>during the asynchronous operation</strong> should be captured by the task rather than thrown directly. In practice, any exception that happens after the initial synchronous portion will be stored in the returned task:</p><p>-&gt; If the async operation fails, the task transitions to the <strong>Faulted</strong> state and holds the exception (accessible via task.Exception). If you await the task, this exception (or one of them) will be re-thrown in your code to handle.</p><p>-&gt; In typical scenarios, a task contains at most one exception (the first error that caused failure). However, if you’re doing something like Task.WhenAll on multiple tasks, multiple failures can aggregate. In such cases, the resulting task might hold an AggregateException containing all inner exceptions. But when awaiting, by default only the first exception is re-thrown (you can still inspect the others via the exception’s InnerExceptions).</p><p>-&gt;If an OperationCanceledException is thrown during the async operation (and it’s tied to the cancellation token that was passed in), the task ends in the <strong>Canceled</strong> state rather than Faulted. From the perspective of await, a canceled task also throws (it will throw an OperationCanceledException to signal cancellation).</p><p>A key takeaway: as the implementer of an async method, <strong>do not let exceptions escape outright after the task is returned</strong>. Always catch them within your operation and assign them to the task (or let them bubble up inside an async method, which does this automatically). The only exceptions you should throw directly out of the method call are those that indicate “you did it wrong” (like passing bad args). This way, production code can assume: calling an Async method rarely blows up immediately – any runtime issues show up when awaiting the task.</p><h3>Where Does the Work Run? Understanding Task Execution Context</h3><p>One powerful aspect of TAP is that it doesn’t dictate <em>how</em> you perform the async work — it just standardizes <em>how you represent</em> it (with a Task). As the developer of an async method, you get to choose the <strong>target environment</strong> for the work:</p><p>-&gt; You might run the operation on the <strong>thread pool</strong> (typical for compute-bound work or for offloading I/O without blocking a specific thread).</p><p>-&gt;You might use <strong>async I/O</strong> operations provided by the OS or framework that don’t occupy any thread while waiting (e.g., using Stream.ReadAsync which uses the OS overlapped I/O; in this case the work happens in the kernel and via interrupts, not on a dedicated .NET thread).</p><p>-&gt;You could force execution to a specific thread or context if needed (for example, some UI frameworks require certain operations on the UI thread — though usually you wouldn’t expose such a TAP method publicly).</p><p>-&gt;You can even have an async method that doesn’t do any work at all itself, but simply returns a Task that is signaled by some other event in the system (for example, returning a task that completes when a message arrives on a queue elsewhere).</p><p>From the caller’s perspective, all they have is a Task to await. They might not know or care where the work is actually happening, and that’s a good thing. The <strong>asynchrony is an implementation detail</strong> hidden behind the Task abstraction.</p><p>One more thing about calling tasks: The consumer can choose to wait in different ways. They might await it (which will by default capture the context and resume on, say, the UI thread). Or they might use ContinueWith to specify a callback when the task finishes, possibly even on another thread or without returning to the original context. Or they might block on task.Wait() (not recommended in most cases, but possible). The point is, the Task gives flexibility: it’s an object that represents the operation and can be checked or waited on from anywhere. As an implementer, you should ensure the task you return is “hot” (already started, see next section) and let the callers decide how they want to handle its completion.</p><h3>The Life of a Task: Hot vs Cold and TaskStatus</h3><p>Every Task in .NET goes through a life cycle defined by the TaskStatus enum, like <strong>Created</strong>, <strong>Running</strong>, <strong>RanToCompletion</strong>, <strong>Faulted</strong>, or <strong>Canceled</strong>. Most tasks you encounter in TAP are <strong>hot tasks</strong> – meaning they’re already started and running (or completed) by the time you get them. For example, when you call an async method, it begins execution and usually hits an await (at which point it may return a not-yet-completed Task to you). You don’t manually start these tasks; they’re hot out of the oven.</p><p>However, Task <em>does</em> have a way to be created without starting: if you directly instantiate a Task using its constructor, it begins in the <strong>Created</strong> state and will not run until you call Start(). These are sometimes called <strong>cold tasks</strong>. In TAP, you should almost never encounter cold tasks from a public API. In fact, <strong>all tasks returned from TAP methods must be active (hot)</strong> by the time you get them. If a TAP method internally uses the Task constructor (perhaps for some advanced scenario), it <strong>must call </strong><strong>Start() on that task before returning it</strong>. Otherwise, if a caller awaited a cold task, it would never complete – definitely not what we want! And if a caller mistakenly tried to call Start on a task that’s already running or finished, it would throw an InvalidOperationException. So, as a rule, <strong>never expose a cold task from a TAP method</strong> – always ensure it’s started.</p><p>Typically, you won’t manually create Task objects in TAP unless doing something special. Most of the time, you either:</p><p>-&gt;Write your method with the async keyword and let the C# compiler generate the Task (hot by default as soon as the method is called), or</p><p>-&gt;Use helpers like Task.Run or TaskCompletionSource (which produce already-started tasks or tasks that you control completion of, respectively).</p><p>So, think of a Task’s life like this: It’s either waiting to be run (if you explicitly created it cold), running, or finished (with one of three end states: RanToCompletion, Faulted, Canceled). TAP tasks skip the “waiting to be run” stage for you — they go straight to running or at least scheduled. And once a task is finished (any of the three final states), it stays in that state forever (tasks are single-use). At that point, IsCompleted becomes true, and if it’s Faulted or Canceled, IsFaulted or IsCanceled will be true respectively. Any continuations attached will have been fired, and awaiting the task will either give a result, throw an exception, or throw a cancellation exception as appropriate.</p><h3>Cancellation: Giving Up on an Async Operation</h3><p>Real-world operations might need to be <strong>canceled</strong> — maybe the user hit a Stop button, or the work is obsolete by the time it would finish. TAP supports cancellation through the use of CancellationToken. It’s an optional feature: not every async method supports cancellation, but many do.</p><p>If you want your TAP method to support cancellation:</p><p><strong>-&gt; Add a </strong><strong>CancellationToken parameter</strong>, typically optional with a default value (e.g., CancellationToken cancellationToken = default). By convention, name it cancellationToken for clarity.</p><p>-&gt;In your implementation, you’ll need to periodically check cancellationToken.IsCancellationRequested or use the token in async operations that accept one (many framework async methods accept a token). If a cancellation is requested, you should stop work and <strong>signal cancellation</strong>.</p><p>-&gt; How to signal? If you’re inside an async method, you can simply throw an OperationCanceledException (passing it the token) when you detect cancellation. The C# compiler recognizes this pattern and will translate it to a canceled task (i.e., task ends in Canceled state, not Faulted).</p><p>-&gt; If you’re using lower-level APIs or TaskCompletionSource, you might call tcs.SetCanceled() to transition the task to Canceled state.</p><p>It’s important that you <strong>only mark the task as Canceled if you really stopped the operation</strong> due to cancellation. If the operation finishes normally or with a different error despite a cancellation request, then the task should end in RanToCompletion or Faulted. Cancellation is cooperative — just because a token was signaled doesn’t mean the work <em>must</em> cancel. But if your method can and does honor the request, it ends in Canceled (which, again, will cause await to throw OperationCanceledException).</p><p>What if the cancellation token was already signaled <em>before</em> you start the operation? A well-behaved TAP method will check <strong>at the very beginning</strong>: if cancellationToken.IsCancellationRequested is true at the start, you can short-circuit by returning an <strong>already Canceled task</strong> (or throwing OperationCanceledException immediately in an async method, which results in the same outcome). This saves doing any work that the caller wanted to cancel anyway.</p><p>From the <em>consumer</em> side, using cancellation is straightforward. Create a CancellationTokenSource, pass its Token to the async method, and later call Cancel() on the source if you want to abort. All tasks that were given that token will observe the cancellation and cancel themselves if they honor it. For example:</p><pre>var cts = new CancellationTokenSource();<br>try <br>{<br>    string result = await DownloadStringTaskAsync(url, cts.Token);<br>    // use the result<br>}<br>catch (OperationCanceledException) <br>{<br>    Console.WriteLine(&quot;Download was canceled.&quot;);<br>}<br>// ... at some later point from another context:<br>cts.Cancel();</pre><p>You can reuse the same token for multiple tasks (to cancel a group of operations together), or use separate tokens for separate operations. And if you have an API that doesn’t accept cancellation, you can pass CancellationToken.None to indicate “I’m not cancelling this one.”</p><p>One more thing: If you call Cancel(), any <em>awaiting</em> code will get an OperationCanceledException. If code was instead blocking on Task.Wait() or similar, that call will throw AggregateException containing an OperationCanceledException. The key is that canceling propagates an exception to signal the cancellation. Just remember to catch that specific exception type if you need to handle cancellation differently from other errors.</p><h3>Reporting Progress: Are We There Yet?</h3><p>Long-running operations often have intermediate progress to report (for example, a file download can report how many bytes or what percentage is done). TAP doesn’t use events for progress like some older patterns did; instead, it relies on the IProgress&lt;T&gt; interface for a producer -&gt; consumer callback mechanism.</p><p>If your async method can report progress:</p><p>-&gt; Add a parameter of type IProgress&lt;T&gt; (commonly named progress). The generic type T is whatever data you want to send to report progress. It could be a simple value like int (percent complete), a number of bytes, or even a complex type carrying multiple pieces of info.</p><p>-&gt;The caller will supply an implementation of IProgress&lt;T&gt; – often you can just use the built-in Progress&lt;T&gt; class. That class takes a callback (or event) and is responsible for dispatching progress updates, usually capturing the synchronization context (so progress handlers are invoked on the original context, e.g., the UI thread).</p><p>-&gt; From inside your async method, whenever you have something to report, you call progress.Report(value). This will internally call the callback provided by the consumer.</p><p>One advantage of IProgress&lt;T&gt; is that it handles the threading concerns for you. The default Progress&lt;T&gt; implementation will make sure that the ProgressChanged event or provided delegate runs on the captured context (e.g., UI thread), so your async method can report progress from any thread without issues. If no context is captured (say you created Progress&lt;T&gt; on a thread without a sync context), it just raises progress events on a thread pool thread.</p><p>Another benefit is flexibility: The consumer can choose how to handle progress. They might only care about the latest value (and drop older ones), or buffer them, or update a UI element each time. The IProgress&lt;T&gt; abstraction means your method doesn’t care – it just sends updates.</p><p><strong>Example:</strong> Suppose we have a ReadAsync method that can report how many bytes have been read so far:</p><pre>public Task ReadAsync(byte[] buffer, int offset, int count, IProgress&lt;long&gt; progress);</pre><p>If the operation reads data in chunks, after each chunk it could do progress?.Report(bytesReadSoFar);. If progress is null, it simply does nothing (meaning the caller wasn’t interested in progress). Always allow progress to be optional (null), so callers who don’t need progress can call the simpler overload or pass null.</p><p>Another example: a FindFilesAsync method might report not just percentage but also partial results (list of files found so far). You could define a custom class FindFilesProgressInfo or use a tuple like (double percent, IReadOnlyCollection&lt;FileInfo&gt; resultsSoFar) for the progress reports. The pattern is flexible – define a progress data type that makes sense for your operation.</p><p>Progress reporting should typically be done <strong>synchronously from within the async method</strong> when the progress event happens. This might sound odd — raising an event (Report) synchronously — but it ensures that the update is delivered promptly and in order. The Progress&lt;T&gt; implementation takes care of offloading that to the right context asynchronously if needed, so your async method isn’t stalled by a slow UI update. In summary, if you provide progress, just call Report and let the consumer’s IProgress&lt;T&gt; handle how to marshal that update.</p><h3>Designing Overloads: Cancellation and Progress (Mix and Match)</h3><p>By now, you might wonder: if I support both cancellation and progress in an API, how many overloads do I need? Potentially:</p><p>-&gt; A simple MethodAsync(...); (no cancel, no progress).</p><p>-&gt;MethodAsync(..., CancellationToken cancellationToken);</p><p>-&gt; MethodAsync(..., IProgress&lt;T&gt; progress);</p><p>-&gt;MethodAsync(..., CancellationToken cancellationToken, IProgress&lt;T&gt; progress);</p><p>That’s four in total, which is a lot to maintain. Many TAP APIs that support cancellation and progress will provide just the extremes: one without either, and one with both. In .NET’s own libraries, you’ll often see:</p><pre>Task OperationAsync(params);<br>Task OperationAsync(params, CancellationToken cancellationToken, IProgress&lt;T&gt; progress);</pre><p>and they may omit the two “intermediate” overloads. Why? Because in C#, you can always call the full one by passing CancellationToken.None for cancellation if you don’t care, or null for progress if you don’t want it. This covers all scenarios without a proliferation of method overloads.</p><p>If you expect that essentially <strong>everyone</strong> will use cancellation or progress, you might decide to only have the full method (force them to pass a token or progress, even if it’s a default). Conversely, if cancellation or progress doesn’t make sense for your operation, don’t include parameters for them at all — keep the API surface clean.</p><p>The takeaway: Offer what’s useful, but don’t go overboard. Two overloads (none vs all) often strike a good balance. The ones without cancellation/progress can internally just call the full method with default token and/or null progress for convenience.</p><p>At this point, we’ve covered how to <strong>design</strong> a TAP method — naming, what it returns, optional cancellation and progress parameters, etc. Now let’s shift perspective: how do we actually implement these asynchronous methods? And after that, how do consumers use them effectively? The next sections will walk through implementing TAP methods (with and without the compiler’s help), and then consuming tasks with await and combinators.</p><p>Here is <a href="https://medium.com/p/6cd234d9d937/edit">Part 2</a>.</p><blockquote>💡 <em>If this article added value to your learning, feel free to drop a clap, share it with your peers, or follow me for more deep dives into .NET and software engineering.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b300071a43fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering IOptions in .NET]]></title>
            <link>https://medium.com/@shahriddhi717/mastering-ioptions-in-net-84672704001a?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/84672704001a</guid>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Sat, 09 Aug 2025 23:17:15 GMT</pubDate>
            <atom:updated>2025-08-09T23:17:15.873Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering IOptions in .NET — The Complete Guide to IOptions&lt;T&gt;, IOptionsSnapshot&lt;T&gt;, and IOptionsMonitor&lt;T&gt;🚀</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IeApiywjzEG0LvJ2tOBzHQ.png" /></figure><p>A few days ago, I was watching an NDC conference talk, and the speaker casually mentioned something I’d seen before but never really explored:<br> IOptions&lt;T&gt;, IOptionsSnapshot&lt;T&gt;, and IOptionsMonitor&lt;T&gt;.</p><p>I’d used IOptions&lt;T&gt; in my ASP.NET Core projects plenty of times, but I’ll admit it — I never stopped to think:</p><ul><li>Why are there <em>three</em> different “options” interfaces?</li><li>How exactly do they differ?</li><li>When should I use one over the others?</li></ul><p>That talk made me realize how many developers (my past self included) just pick one, make it work, and move on — without understanding what’s going on behind the scenes.</p><p>So, I decided to dig in. And here’s the practical guide I wish I had years ago.</p><h3>Why the Options Pattern Exists</h3><p>ASP.NET Core introduced the <strong>Options pattern</strong> to avoid hardcoding configuration values and instead bind them to strongly typed C# classes.</p><p>Instead of this:</p><pre>var apiKey = Configuration[&quot;MyConfig:ApiKey&quot;];</pre><p>You do this:</p><pre>public class MyConfig<br>{<br>    public string ApiKey { get; set; }<br>    public int Timeout { get; set; }<br>}<br>builder.Services.Configure&lt;MyConfig&gt;(<br>    builder.Configuration.GetSection(&quot;MyConfig&quot;));</pre><p>Type-safe, cleaner, and less error-prone. But here’s the twist:<br> What happens if your configuration <strong>changes while the app is running</strong>?</p><p>Do you need a restart?<br>Do you want updates per request?<br>Do you want instant notifications?</p><p>That’s where the three flavors come in.</p><h4>1. IOptions&lt;T&gt; — The Immutable Classic</h4><p>Key Facts:</p><p>-&gt; Configuration is read once at startup.</p><p>-&gt; No updates after app starts.</p><p>-&gt; Works anywhere-singleton scoped or transient services.</p><pre>public class MyStaticService<br>{<br>    private readonly MyConfig _config;<br><br>    public MyStaticService(IOptions&lt;MyConfig&gt; options)<br>    {<br>        _config = options.Value;<br>    }<br><br>    public string GetApiKey() =&gt; _config.ApiKey;<br>}</pre><p><strong>When to use:</strong><br>When your settings are fixed for the entire app lifetime (e.g., app name, fixed API base URL).</p><h4>2. IOptionsSnapshot&lt;T&gt; — Scoped Refresh</h4><p>Key Facts:</p><p>-&gt; Reads configuration <strong>once per request</strong>.</p><p>-&gt;Only works in <strong>scoped</strong> or <strong>transient</strong> services.</p><p>-&gt;Doesn’t send change notifications.</p><pre>public class MyScopedService<br>{<br>    private readonly MyConfig _config;<br><br>    public MyScopedService(IOptionsSnapshot&lt;MyConfig&gt; options)<br>    {<br>        _config = options.Value;<br>    }<br><br>    public int GetTimeout() =&gt; _config.Timeout;<br>}</pre><p><strong>When to use:</strong><br>For web apps where configuration can change between requests (e.g., per-tenant settings, feature toggles).</p><h4>3. IOptionsMonitor&lt;T&gt; — Real-Time Updates + Notifications</h4><p>Key Facts:</p><p>-&gt;Reads configuration whenever it changes.</p><p>-&gt;Works in <strong>singleton</strong> and <strong>scoped</strong> services.</p><p>-&gt;Supports OnChange() for callbacks.</p><pre>public class MySingletonService<br>{<br>    private MyConfig _config;<br><br>    public MySingletonService(IOptionsMonitor&lt;MyConfig&gt; options)<br>    {<br>        _config = options.CurrentValue;<br>        options.OnChange(updated =&gt;<br>        {<br>            _config = updated;<br>            Console.WriteLine(&quot;Configuration changed!&quot;);<br>        });<br>    }<br><br>    public string GetApiKey() =&gt; _config.ApiKey;<br>}</pre><p><strong>When to use:</strong><br>When you need real-time updates in long-running services (e.g., background jobs, live feature switches).</p><h3>Final Thoughts</h3><p>When I first heard about IOptionsSnapshot&lt;T&gt; and IOptionsMonitor&lt;T&gt; in that NDC talk, I realized I’d been treating all “options” the same. But in reality:</p><ul><li><strong>IOptions&lt;T&gt;</strong> → static settings.</li><li><strong>IOptionsSnapshot&lt;T&gt;</strong> → per-request updates.</li><li><strong>IOptionsMonitor&lt;T&gt;</strong> → real-time changes + notifications.</li></ul><p>Choosing the right one isn’t just about code correctness — it’s about making your app flexible without adding complexity where it’s not needed.</p><p>Next time you wire up configuration in ASP.NET Core, you’ll know exactly which “options” to pick.</p><blockquote>If you found this article helpful and insightful, please consider giving it a clap, sharing it within your network, or following me for more articles.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84672704001a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Code Navigation Server with C# and .NET: An MCP Case Study with AICodeNavigator]]></title>
            <link>https://medium.com/@shahriddhi717/building-a-code-navigation-server-with-c-and-net-an-mcp-case-study-with-aicodenavigator-883f059d938e?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/883f059d938e</guid>
            <category><![CDATA[model-context-protocol]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Tue, 22 Apr 2025 17:14:01 GMT</pubDate>
            <atom:updated>2025-04-22T17:57:23.102Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a Code Navigation Server with C# and .NET: An MCP Case Study with AICodeNavigator</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*Wk29pXicpj63mxMErnHSNA.png" /></figure><p>Modern software development relies heavily on powerful tooling for navigating complex codebases. As projects grow and encompass diverse technologies, the need for flexible, external services capable of providing specialized code analysis and navigation becomes apparent. The Model Context Protocol (MCP) offers a solution by standardizing communication between developer tools and backend services.</p><p>This article explores the process of building an MCP server using C# and the robust .NET platform. We present <strong>AICodeNavigator</strong> as a case study — a project designed to offer intelligent code navigation features for a specified code root. Through this example, we detail the server architecture, leveraging libraries like `McpDotNet`, configuring communication via Standard I/O, defining server capabilities as “Tools,” and understanding the practical considerations of deployment environments and configurations like `mcp.json`. The aim is to demonstrate how to construct a modular, accessible code navigation service ready for integration with various client tools.</p><h3>Table of Content</h3><ul><li>Introduction: The Need for Modular Code Tooling</li><li>What is the Model Context Protocol (MCP)?</li><li>Why Choose C# and .NET for an MCP Server?</li><li>AICodeNavigator: Our Code Navigation Case Study</li><li>Building the MCP Server in C#</li><li>Launching Configuration: Understanding `mcp.json`</li><li>Challenges and Considerations</li><li>Future Directions for AICodeNavigator and .NET MCP Servers</li><li>Conclusion: Towards Accessible Code Intelligence</li><li>Resources.</li></ul><h3><strong>Introduction: The Need for Modular Code Tooling</strong></h3><p>The landscape of software development tooling is constantly evolving. While IDEs provide comprehensive, integrated experiences, the increasing complexity of modern projects, the adoption of polyglot architectures, and the desire for highly customized workflows highlight the limitations of monolithic tools. Developers often need specialized services — perhaps one tailored for analyzing a unique domain-specific language, another for interacting with a proprietary version control system, or one like our <em>AICodeNavigator</em>, focused on providing deep navigation within a specific, potentially large, code repository.</p><p>Creating these specialized capabilities as standalone services that can be accessed by *any* tool reduces redundancy and fosters innovation. The Model Context Protocol(MCP) provides the necessary contract, enabling a clear separation between the user-facing client tool and the backend analysis or navigation service.</p><h3>What is the Model Context Protocol (MCP)?</h3><p>MCP serves as a standardized communication layer between development frontend tools (clients) and backend processes (servers). It defines a common language for requesting tasks (like “find definition” or “search for symbol”) and receiving structured responses.</p><p>Key characteristics of MCP include:</p><p>Tool-Agnosticism: Clients don’t need to know the server’s internal implementation details, only the protocol. Servers don’t need to know the client’s UI.</p><p>Transport Flexibility: While Standard Input/Output (Stdio) is a common transport, others like TCP sockets can also be used, allowing for different deployment scenarios.</p><p>Defined Capabilities: Servers expose a set of <em>“Tools,”</em> each corresponding to a specific operation or set of operations the server can perform. Clients query the server to discover available tools.</p><p>This protocol allows developers to build a powerful backend service once and integrate it into various IDEs, text editors via plugins, or command-line utilities, promoting reusability and fostering a modular tooling ecosystem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*2mT9sG85zZTO-PQKHVwXNw.png" /><figcaption>Image Reference from <a href="https://www.descope.com/learn/post/mcp">What Is the Model Context Protocol (MCP) and How It Works</a></figcaption></figure><h3>Why Choose C# and .NET for an MCP Server?</h3><p>C# and the modern .NET platform offer compelling advantages when building backend services like an MCP server:</p><p>Performance: .NET is known for its high performance, making it suitable for computationally intensive tasks like code analysis and search.</p><p>Cross-Platform Support: .NET applications can run on Windows, macOS, and Linux, ensuring your server is accessible across major operating systems.</p><p>Developer Productivity: C#’s strong typing, modern language features (async/await, LINQ, etc.), and the comprehensive tooling provided by Visual Studio, VS Code, and the .NET CLI accelerate development.</p><p>Robust Ecosystem: Access to a vast collection of libraries via NuGet, including those that can help with file system operations, parsing, and potentially implementing the MCP protocol itself.</p><p>Maintainability: C#’s structured and type-safe nature leads to more maintainable codebases compared to scripting languages for complex projects.</p><p>These factors combine to make .NET a highly effective platform for developing performant, reliable, and cross-platform backend services for developer tooling.</p><h3><strong>AICodeNavigator: Code Navigation Case Study</strong></h3><p>AICodeNavigator, is an example of a specialized service implemented as an MCP server. Its primary function is to provide intelligent code navigation and search capabilities within a designated root directory of a codebase.</p><p>By exposing these capabilities via MCP, AICodeNavigator can serve as a backend for various code editors or custom tools. A developer could, for instance, integrate AICodeNavigator into their preferred editor to gain advanced search features tailored to their specific project needs or use it in a script to automate code analysis tasks. The server’s operation is centered around a configured root directory, allowing it to be pointed at different code repositories.</p><h4>Building the MCP Server in C#</h4><p>Implementing an MCP server in C# involves setting up the application host, integrating an MCP framework library (like `McpDotNet`), defining the communication transport, and implementing the specific functionalities as MCP “Tools.”</p><pre> [McpToolType] // Fixed attribute usage to match the provided type signature  <br>    public class CodebaseTools<br>    {<br>        [McpTool, Description(&quot;Search C# files for a specific function, class, or keyword.&quot;)]<br>        public static async Task&lt;string&gt; SearchCodebase(CodeSearchService codeSearchService, string keyword)<br>        {<br>            var results = await codeSearchService.Search(keyword);<br>            return JsonSerializer.Serialize(results);<br>        }<br><br>        [McpTool, Description(&quot;Read and return the content of a specific file.&quot;)]<br>        public static async Task&lt;string?&gt; ReadFile(CodeSearchService codeSearchService, string filePath)<br>        {<br>            var content = await codeSearchService.ReadFile(filePath);<br>            return content;<br>        }<br><br>        [McpTool, Description(&quot;Summarize the content of a C# file.&quot;)]<br>        public static async Task&lt;string?&gt; SummarizeFile(CodeSearchService codeSearchService, string filePath)<br>        {<br>            var summary = await codeSearchService.SummarizeFile(filePath);<br>            return summary;<br>        }<br>    }</pre><h3>Launching Configuration: Understanding mcp.json</h3><p>The mcp.json file serves as a configuration file for frontend tools or launchers that want to interact with your MCP server. It tells the client <em>how</em> to start the server process.</p><p>A typical mcp.json for a .NET Stdio server looks like this:</p><pre>{<br>  &quot;servers&quot;: [<br>    {<br>      &quot;type&quot;: &quot;stdio&quot;, // Communication type<br>      &quot;command&quot;: &quot;dotnet&quot;, // Executable to run<br>      &quot;args&quot;: [          // Arguments for the executable<br>        &quot;run&quot;,<br>        &quot;--project&quot;,<br>        &quot;path/to/AICodeNavigator.csproj&quot; // Path to your project file relative to where dotnet is run, or absolute<br>      ]<br>    }<br>  ]<br>}</pre><h3>Challenges and Considerations</h3><p>Building an MCP server, while rewarding, comes with practical considerations:</p><ul><li><strong>Environment Restrictions:</strong> As discussed, the hosting environment (the client tool or launcher) might impose limitations, such as restricting file system access to a defined “workspace.” Ensuring your server can access the target rootCodeDirectory requires understanding and potentially configuring the environment running your server process.</li><li><strong>Error Handling:</strong> Robust error handling is crucial, both within your CodeSearchService and within your MCP &quot;Tools,&quot; to provide meaningful feedback to the client tool when operations fail.</li><li><strong>Performance Optimization:</strong> For large codebases, optimizing the performance of your code analysis and search algorithms within CodeSearchService is paramount.</li><li><strong>Protocol Evolution:</strong> If you define custom MCP “Tools,” consider how the protocol for those tools might evolve over time and how you’ll handle versioning.</li></ul><h3>Future Directions for AICodeNavigator and .NET MCP Servers</h3><p>The foundation of an MCP server opens up many possibilities:</p><ul><li><strong>Expanded Toolset:</strong> Adding more sophisticated code navigation and analysis features as new MCP Tools (e.g., finding dead code, analyzing dependencies, integrating with AI models for code suggestions).</li><li><strong>Alternative Transports:</strong> Implementing support for TCP or other transports for different deployment scenarios (e.g., a persistent server process accessible over a network).</li><li><strong>Configuration Flexibility:</strong> Making the rootCodeDirectory and other settings more easily configurable via command-line arguments or separate configuration files read by the server itself, rather than hardcoding.</li><li><strong>Performance Enhancements:</strong> Exploring caching strategies, parallel processing, and optimized data structures for the CodeSearchService.</li></ul><p>An MCP-based architecture sets the stage for building highly capable and widely usable developer services.</p><h3>Conclusion: Towards Accessible Code Intelligence</h3><p>The Model Context Protocol offers a powerful paradigm shift for developer tooling, enabling the creation of modular, reusable backend services. By leveraging the strengths of C# and the .NET platform, developers can build high-performance, cross-platform MCP servers like <strong>AICodeNavigator</strong>.</p><p>Implementing an MCP server involves structuring your application with a host, integrating a library to handle the protocol, defining your capabilities as discoverable “Tools,” and understanding the environmental factors affecting deployment and file access. The result is a backend service capable of providing specialized code intelligence that can be integrated into diverse frontend tools, democratizing access to powerful code analysis capabilities.</p><h3>Resources</h3><ul><li>My <a href="https://github.com/Ashahet1/C-Project/tree/master/MCPServer/AICodeNavigator">GitHub for AICodeNavigator</a></li><li><a href="https://www.linkedin.com/in/riddhishah65/">Follow me on Linkedin</a></li><li><a href="https://docs.github.com/en/copilot/copilot-in-the-cli/using-github-copilot-in-the-cli">GitHub Copilot Agent Mode</a></li><li><a href="https://devblogs.microsoft.com/dotnet/build-a-model-context-protocol-mcp-server-in-csharp/">MCP Blog by Microsoft</a></li></ul><blockquote>If you found this article helpful and insightful, please consider giving it a clap, sharing it within your network, or following me for more articles exploring AI, software engineering, and cutting-edge technology. Your support and feedback are greatly appreciated! ✌🏻</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=883f059d938e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How LLMs Are Changing the Way We Think About Software Design Patterns]]></title>
            <link>https://medium.com/@shahriddhi717/how-llms-are-changing-the-way-we-think-about-software-design-patterns-70c3b1ebca19?source=rss-71e6b4872b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/70c3b1ebca19</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Riddhi Shah]]></dc:creator>
            <pubDate>Tue, 22 Apr 2025 01:38:38 GMT</pubDate>
            <atom:updated>2025-04-22T01:38:38.544Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NStNdQqi1hTgFAU2IdDeA.png" /><figcaption>Image created by Gemini 2.5 Pro</figcaption></figure><p>It all started with a perfectly well-behaved system. Last year, I built a backend where every component did exactly what it was told. Strategy pattern here, Command there — everything ran smoothly and predictably. If something went wrong, I could trace it back to a single decision point. There was structure. There was order. There was peace.</p><p>Then LLM got added.</p><p>Suddenly, responses weren’t just “Yes” or “No” — they were “Yes, but only if it’s Tuesday and the temperature is above 70°F.”</p><p>What used to be control flow logic turned into <em>probabilistic reasoning</em> wrapped in natural language.</p><blockquote>It didn’t break the system — it ‘redefined how the system thought’.</blockquote><p>That’s when I realized: classic design patterns aren’t obsolete. But the assumptions behind them? They’re definitely under review.</p><p><strong>Table of Contents</strong></p><ol><li>Introduction</li><li>What Makes LLMs Different from Traditional Logic</li><li>Strategy Pattern — Adaptation Over Configuration</li><li>Command Pattern — Prompting as an Execution Model</li><li>Interpreter Pattern — The Rise of AI as the Interpreter</li><li>What I Learned While Exploring This Shift</li><li>Takeaway: Should You Still Learn Design Patterns?</li><li>Final thoughts on relevance, mindset shifts, and hybrid design</li><li>References</li></ol><p><strong>Introduction</strong></p><p>“Just give me a list of commands and I’ll follow them like a good little module”, said no Large Language Model ever.</p><p>Two years ago, I was happily coding away in C#, applying design patterns like Strategy and Command like they were law — which, in software design circles, they basically are. Patterns made my code modular, reusable, and clean enough to not get roasted in code reviews. Life was good.</p><p>Then came LLMs— models that don’t just process inputs, but interpret, reason, and sometimes even negotiate with themselves mid-prompt. Suddenly, the neat boxes and arrows from classic pattern diagrams started to feel… limiting, almost like trying to capture the dynamic and probabilistic nature of cloud logic in the static lines of UML.</p><p>And so, I started asking:</p><blockquote>What happens to our beloved design patterns when AI enters the chat?</blockquote><blockquote>Do they hold up, evolve, or quietly retire to a tropical GitHub archive?</blockquote><p>In this article, we’ll explore how LLMs are reshaping traditional software design patterns. We’ll zoom in on three classics — Strategy, Command, and Interpreter— and look at how their roles shift when you’re building systems that think more like humans than machines.</p><blockquote>Spoiler: the patterns aren’t dead. But they might be getting a ‘semantic facelift’.</blockquote><p><strong>What Makes LLMs Different from Traditional Logic</strong></p><p>Classic software systems are built on deterministic logic. You give an input, apply a series of clearly defined rules, and get a predictable output. That’s what design patterns were made for: to organize this logic, reduce complexity, and make systems easier to maintain.</p><p>But LLMs don’t work like that.</p><p>Instead of following a fixed rule set, large language models operate on probabilities. They generate outputs based on what they *think* is most appropriate in context — not what a developer explicitly told them to do. They reason in a way that feels more like conversation than computation. It’s not just “if this, then that” — it’s “if this, then… maybe that, but it depends on the intent, the history, and how the prompt was worded.”</p><p>This fundamental shift introduces three key differences that directly impact how we think about design patterns:</p><ol><li>Probabilistic reasoning replaces hard logic — With LLMs, outcomes aren’t binary. You’re not always guaranteed the same output for the same input. That’s both powerful and unpredictable — which means pattern boundaries have to be looser, more tolerant of variance.</li><li>LLMs collapse roles — In a traditional system, you might have a parser, a router, and a handler — each separated by design. An LLM can blur all of these roles. It might interpret a command, choose the handler, and generate the response — all in one prompt. This forces us to rethink separation of concerns.</li><li>Context becomes architecture — LLMs rely heavily on prompts and context windows. That means “state” isn’t just stored in variables — it’s embedded in language, in prior responses, in how the prompt is framed. Designing for LLMs requires treating prompts like first-class citizens — just like functions or data contracts.</li></ol><p>This is where design patterns begin to shift. They’re not replaced, but they have to adapt — because the way decisions are made in AI-enhanced systems is fundamentally different.</p><p>Given these fundamental differences, how might our established design patterns fare in this new era of probabilistic reasoning?</p><p><strong>Strategy Pattern — Adaptation Over Configuration</strong></p><p>In traditional software design, the Strategy Pattern is all about choosing the right algorithm at runtime. You define a family of algorithms, encapsulate each one, and make them interchangeable depending on the context. It’s used everywhere — from sorting logic to payment processing, to test execution.</p><p>The idea is: you don’t change the logic of the system; you just swap in a different behavior.</p><p>But with LLMs, the behavior isn’t chosen — it’s generated.</p><pre>public interface IValidator {<br>    bool Validate(string input);<br>}<br><br>public class LengthValidator : IValidator {<br>    public bool Validate(string input) =&gt; input.Length &lt; 10;<br>}<br><br>public class RegexValidator : IValidator {<br>    public bool Validate(string input) =&gt; Regex.IsMatch(input, @&quot;^\d+$&quot;);<br>}<br><br>// Usage<br>IValidator validator = new LengthValidator();<br>bool isValid = validator.Validate(&quot;123456&quot;);</pre><p>Here, we configure our system by selecting a specific validator strategy. But with LLMs, the concept of a “strategy” takes on a different flavor. Instead of explicitly choosing a pre-defined algorithm, we are essentially prompting the LLM to adopt a certain strategy through natural language.</p><p>Consider a scenario: Imagine you’re building a content generation tool. Traditionally, you might have different strategy classes for generating different types of content (e.g., a ShortBlogPostStrategy, a DetailedArticleStrategy, a SocialMediaPostStrategy). You’d instantiate the appropriate strategy based on the user’s choice.</p><p>With an LLM, the “strategy” becomes embedded in the prompt. Instead of selecting a class, you might prompt the LLM with: “Write a short, engaging blog post about the benefits of using design patterns for software development.” The LLM then dynamically adopts the “short,” “engaging,” and “blog post” strategies based on your instruction.</p><p>The shift here is subtle but significant. The strategy isn’t a discrete, pre-coded unit anymore. It’s a set of instructions and constraints communicated through natural language, allowing for a much more fluid and context-dependent approach to behavior. The LLM itself acts as a highly adaptable strategy executor, capable of understanding and implementing a wide range of implicit and explicit instructions. This moves us from a model of configuration to one of dynamic generation guided by language.</p><p><strong>Command Pattern — Prompting as the New Invoker</strong></p><p>The Command Pattern traditionally decouples the object that invokes an operation from the one that knows how to perform it. You encapsulate an action as an object (the command), allowing you to parameterize clients with different requests, queue or log requests, and support undoable operations. Think of a remote control: each button (command) triggers an action on a device (receiver) without the remote knowing the specifics of that action.</p><p><strong>How does this change with LLMs?</strong> Consider the prompt itself as the “command.” The user provides a natural language instruction, and the LLM acts as both the invoker and, often, the receiver and executor.</p><p><strong>Example:</strong> In a traditional system, you might have a CreateDocumentCommand, an EditDocumentCommand, and a SaveDocumentCommand. A user interface button would be mapped to a specific command object.</p><pre>interface Command { execute() }<br>class CreateDocumentCommand implements Command { execute() { // logic to create doc } }</pre><p><strong>With an LLM, a user might simply type: “Create a new document summarizing the key points of the last meeting and save it as ‘Meeting Summary’.”</strong> The LLM interprets this entire prompt as a complex command. It understands the intent (“create a document,” “summarize,” “save”), identifies the necessary steps, and potentially executes them internally or by interacting with other parts of the system.</p><p><strong>The key difference is the level of abstraction and the medium of communication.</strong> Instead of invoking specific, pre-defined command objects, we are using natural language prompts to convey our desired actions. The LLM takes on the responsibility of parsing, understanding, and executing these high-level commands. This shifts the focus from explicit command objects to the expressive power of natural language as the primary means of interaction and instruction.</p><p><strong>Interpreter Pattern — The Rise of AI as the Ultimate Interpreter</strong></p><p>The Interpreter Pattern defines a grammatical representation for a language and provides an interpreter to deal with this grammar. 1 It’s often used for things like parsing expressions in a programming language or interpreting rules in a business logic system.</p><pre>interface Expression { interpret(context) }<br>class NumberExpression implements Expression { interpret(context) { return number } }</pre><p><strong>Enter the LLM, a natural language interpreter on steroids.</strong> Traditional interpreters are designed for formal languages with strict grammars. LLMs, on the other hand, excel at understanding and generating human language, which is often ambiguous, context-dependent, and full of nuances.</p><p><strong>Consider a scenario:</strong> Imagine a system where users can ask questions about a complex dataset. Traditionally, you might need to build a specific query language and an interpreter to translate those queries into database operations.</p><p><strong>With an LLM, you can often bypass this entire process.</strong> The user can ask their question in plain English: “What was the average sales revenue for the last quarter in the Northeast region?” The LLM can interpret this question, understand the underlying data requirements, and potentially generate the appropriate database query or directly retrieve and format the answer.</p><p><strong>The LLM acts as a universal interpreter, capable of understanding a vast range of natural language inputs and translating them into meaningful actions or information.</strong> It can handle ambiguity, infer intent, and even engage in clarifying dialogues if needed. This represents a significant shift from building specialized interpreters for specific formal languages to leveraging the inherent language understanding capabilities of AI.</p><p><strong>What I Learned While Exploring This Shift</strong></p><p>Design patterns are more important than ever, even in the age of LLMs. <strong>I</strong> might hear arguments on Reddit posts that with natural language prompting, the need for traditional design patterns diminishes. After all, <strong>I</strong> can simply ask an AI to generate code. However, even when an LLM spits out code, understanding design patterns is crucial for <strong>me</strong> to evaluate its quality, suitability, and maintainability for <strong>my</strong> specific use case. Knowing which pattern is best for a particular problem remains a fundamental skill.</p><p>Design patterns provide a fundamental vocabulary for discussing software architecture. Understanding patterns gives <strong>me</strong> a common language to communicate design ideas effectively with other developers. They embody proven solutions to common software design problems. The principles behind patterns — like reducing coupling and increasing cohesion — are still crucial for building maintainable and scalable systems, whether or not LLMs are involved.</p><p>They offer a framework for thinking about system design. Even if the direct implementation differs with LLMs, the mental models provided by design patterns help <strong>me</strong> structure <strong>my</strong> thinking about how different components of <strong>my</strong> system should interact. While the focus might shift from strict adherence to implementation details to understanding the underlying principles and how they can be applied in conjunction with LLMs, the foundational knowledge of design patterns remains invaluable.</p><p>Ultimately, this exploration highlights a “semantic facelift” for design patterns. The underlying concepts endure, but our understanding and application of them are being reshaped by the unique capabilities of large language models.</p><p><strong>Takeaway: Should You Still Learn Design Patterns?</strong></p><p>Absolutely, yes! Design patterns are not relics of the past; they are evolving alongside the tools we use to build software. In the age of LLMs, their relevance persists, but our mindset needs to adapt. We need to embrace the probabilistic nature of AI and understand how to guide these powerful models effectively.</p><p>The future of software design might lie in a hybrid approach. We’ll likely see systems that combine the predictability and structure of traditional design patterns with the flexibility and intelligence of LLMs. This could involve using patterns to structure the overall architecture while leveraging LLMs for specific tasks like natural language processing, content generation, or dynamic decision-making.</p><p><strong>Interestingly, as we navigate this new era, we might even see the emergence of what could be called “AI design patterns” — new ways of structuring and interacting with AI models themselves.</strong> The key is to remain adaptable, continuously learn, and understand how these fundamental design principles can be applied and reinterpreted in the exciting new landscape shaped by artificial intelligence.</p><p>And this makes me wonder, what exactly will these “AI Design Patterns” look like? Perhaps that’s a journey for another article. <a href="https://medium.com/@shahriddhi717/ai-design-patterns-engineering-modular-ml-pipelines-and-agentic-systems-c5f9f7ca29db">Read Here</a>.</p><blockquote><strong>Thanks for reading! If you enjoyed this article, please consider clapping and leaving a comment. Your feedback is valuable</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70c3b1ebca19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>