15 October 2023

ConfigureAwait true and false in C# with Example

ConfigureAwait true and false in C# with Example

In C#, the `ConfigureAwait` method is used in asynchronous programming to specify whether to marshal the continuation back to the original context captured at the point of the `await` operation. It affects how the code after the `await` keyword runs in terms of synchronization context. Here are the differences between `ConfigureAwait(true)` and `ConfigureAwait(false)`:


### `ConfigureAwait(true)`:

```csharp

await SomeTask().ConfigureAwait(true);

```

When you use `ConfigureAwait(true)`, it means that after the asynchronous operation is complete, the continuation (the code after the `await` keyword) will run in the original context captured at the point of the `await`. For example, if the `await` is inside a UI event handler, the continuation will run on the UI thread. If the `await` is in an ASP.NET request context, the continuation will be on the ASP.NET request context.

#### Example using `ConfigureAwait(true)`:

```csharp

async Task<string> GetDataAsync()

{

    // Some asynchronous operation

    await Task.Delay(1000).ConfigureAwait(true);


    // Code after await runs in the original context (e.g., UI thread or ASP.NET request context).

    return "Data loaded successfully.";

}

```

### `ConfigureAwait(false)`:


```csharp

await SomeTask().ConfigureAwait(false);

```

When you use `ConfigureAwait(false)`, it means that after the asynchronous operation is complete, the continuation will not marshal back to the original context. Instead, it will run on a thread pool thread. This can be beneficial in situations where you want to avoid deadlocks, especially in UI applications, ASP.NET, or any context where you want to release the captured synchronization context.


#### Example using `ConfigureAwait(false)`:

```csharp

async Task<string> GetDataAsync()

{

    // Some asynchronous operation

    await Task.Delay(1000).ConfigureAwait(false);


    // Code after await runs on a thread pool thread.

    return "Data loaded successfully.";

}

```

In this example, the `ConfigureAwait(false)` is used to avoid potential deadlocks that might occur if the synchronization context is captured and the continuation tries to marshal back to it.

Choosing between `ConfigureAwait(true)` and `ConfigureAwait(false)` depends on the specific context of your application. Use `ConfigureAwait(true)` when you need to continue on the original context (e.g., UI thread). Use `ConfigureAwait(false)` when you want to avoid deadlocks or when the context is not important for the continuation code.

Interface a and b having same method, how you called the method in C#

 Interface a and b having same method, how you called the method in C#

In C#, if two interfaces `A` and `B` have a method with the same signature, and a class implements both interfaces, the class must provide an implementation of the common method. Here's an example demonstrating this scenario:

```csharp

using System;

// Interface A

interface A

{

    void CommonMethod();

}


// Interface B

interface B

{

    void CommonMethod();

}


// Class implementing both interfaces

class MyClass : A, B

{

    // Explicit implementation of the CommonMethod from interface A

    void A.CommonMethod()

    {

        Console.WriteLine("Implementation of CommonMethod from interface A");

    }


    // Explicit implementation of the CommonMethod from interface B

    void B.CommonMethod()

    {

        Console.WriteLine("Implementation of CommonMethod from interface B");

    }

}

class Program

{

    static void Main(string[] args)

    {

        MyClass myClass = new MyClass();

        

        // Calling the CommonMethod through interface A

        ((A)myClass).CommonMethod();

        

        // Calling the CommonMethod through interface B

        ((B)myClass).CommonMethod();

        

        Console.ReadKey();

    }

}

```

In the above example, the `MyClass` class implements both interfaces `A` and `B`. To differentiate between the implementations of the `CommonMethod` from both interfaces, you can use explicit interface implementation syntax.

When you call the `CommonMethod` through interface `A`, you need to cast the object to interface `A`, and similarly, when you call it through interface `B`, you cast the object to interface `B`. This way, you can provide separate implementations for the same method signature in different interfaces.

Method overriding in C# Example

 Method overriding in C# Example

Method overriding in C# allows a derived class to provide a specific implementation of a method that is already defined in its base class. To override a method in C#, you use the `override` keyword. Here's an example demonstrating method overriding in C#:

Let's consider a base class `Shape` with a method `CalculateArea()`:

```csharp

using System;

class Shape

{

    public virtual void CalculateArea()

    {

        Console.WriteLine("Calculating area in the base class (Shape).");

    }

}

```

In the above code, the `CalculateArea()` method is marked as `virtual`, indicating that it can be overridden by derived classes.

Now, let's create a derived class `Circle` that overrides the `CalculateArea()` method:

```csharp

class Circle : Shape

{

    private double radius;


    public Circle(double radius)

    {

        this.radius = radius;

    }


    // Method overriding

    public override void CalculateArea()

    {

        double area = Math.PI * radius * radius;

        Console.WriteLine($"Calculating area of the circle: {area}");

    }

}

```


In the `Circle` class, we use the `override` keyword to indicate that we are providing a specific implementation of the `CalculateArea()` method defined in the base class `Shape`. We calculate the area of the circle in the overridden method.


Now, you can create objects of the `Circle` class and call the `CalculateArea()` method. The overridden method in the `Circle` class will be executed:

```csharp

class Program

{

    static void Main(string[] args)

    {

        Shape shape = new Circle(5.0); // Creating a Circle object as a Shape

        shape.CalculateArea(); // Calls the overridden method in Circle class


        Console.ReadKey();

    }

}

```

In this example, even though the `shape` variable is of type `Shape`, it points to an instance of `Circle`. When `CalculateArea()` is called, it executes the overridden method in the `Circle` class, demonstrating method overriding in C#.

Blue-green deployment in software release management

Blue-green deployment in software release management 

Blue-green deployment is a software release management strategy that aims to reduce downtime and risk by running two identical production environments, known as "blue" and "green." Only one of these environments serves live production traffic at any given time. Here's how it works:

### 1. **Initial Setup:**

   - **Blue Environment:** This represents the current production environment.

   - **Green Environment:** This is an identical environment set up to test and stage the new version of the application.

### 2. **Deployment Process:**

   - **Testing and Deployment:** Deploy the new version of the application to the green environment. This environment is now live for testing purposes.

   - **Testing and Validation:** Run extensive tests, including unit tests, integration tests, and user acceptance tests, on the green environment. This ensures that the new version is functioning correctly and meets the required quality standards.

### 3. **Switching Traffic:**

   - **Gradual Traffic Switch:** Once the green environment is thoroughly tested and validated, switch the traffic from the blue environment to the green environment gradually. This can be done using load balancers or DNS changes.

   - **Monitoring:** Monitor the green environment for any issues. If problems arise, you can quickly switch back to the blue environment, ensuring minimal downtime.

### 4. **Rollback (if necessary):**

   - **Rollback Procedure:** If issues are detected in the green environment after the switch, rollback to the blue environment. This is possible because the blue environment, which represents the previous stable version, is untouched during the deployment process.

   - **Analysis:** Analyze the issues and fix them in the green environment before attempting the deployment again.

### 5. **Benefits:**

   - **Minimal Downtime:** Users experience minimal or no downtime because the switch between environments is quick and controlled.

   - **Quick Rollback:** If issues are detected, rolling back to the previous version is immediate.

   - **Safe Testing:** Extensive testing can be done in the green environment without affecting the live production environment.

   - **Predictable Rollouts:** Deployment becomes predictable and can be scheduled during low-traffic periods.

### 6. **Automation and Tooling:**

   - **CI/CD Integration:** Blue-green deployments are often integrated into Continuous Integration/Continuous Deployment (CI/CD) pipelines. Automated tools can handle the deployment process, making it even more efficient and reliable.

Blue-green deployments are especially popular in environments where continuous availability is crucial, such as web applications and online services. By ensuring that both the blue and green environments are identical, this strategy provides a safety net for deployments, allowing organizations to release new features and updates with confidence.

11 October 2023

Exceptionfilter in Controller level in C#

 In the context of ASP.NET Web API or ASP.NET Core MVC, an exception filter is a mechanism that allows you to handle exceptions globally at the controller level. Exception filters are attributes that you can apply to a controller or a specific action method within the controller. These filters are executed whenever an unhandled exception is thrown during the execution of the controller action methods.

Here's how you can create and use an exception filter at the controller level in ASP.NET Core MVC:


```csharp

// Custom Exception Filter

public class CustomExceptionFilterAttribute : ExceptionFilterAttribute

{

    public override void OnException(ExceptionContext context)

    {

        // Handle the exception here

        // You can log the exception, customize the error response, etc.

        context.ExceptionHandled = true; // Mark the exception as handled

        context.Result = new JsonResult(new { error = "An error occurred" })

        {

            StatusCode = StatusCodes.Status500InternalServerError

        };

    }

}


// Applying the Exception Filter at the Controller Level

[ApiController]

[Route("api/[controller]")]

[CustomExceptionFilter] // Apply the custom exception filter at the controller level

public class SampleController : ControllerBase

{

    // GET api/sample

    [HttpGet]

    public IActionResult Get()

    {

        // Code that might throw an exception

        throw new Exception("This is a sample exception.");

    }

}

```

In the example above, `CustomExceptionFilterAttribute` is a custom exception filter that inherits from `ExceptionFilterAttribute`. It overrides the `OnException` method to handle exceptions. In this case, it marks the exception as handled, creates a custom error response, and sets the response status code to 500 Internal Server Error.

By applying the `[CustomExceptionFilter]` attribute at the controller level, the filter will be applied to all action methods within the `SampleController` class. When an exception occurs in any of the action methods, the `OnException` method of the `CustomExceptionFilterAttribute` will be invoked, allowing you to handle the exception in a centralized manner.

Remember that exception filters are just one way to handle exceptions in ASP.NET Core. Depending on your requirements, you might also consider using middleware or other global error handling techniques provided by the framework.

7 October 2023

Logic app with service bus example

 Logic app with service bus example

A Logic App is a serverless workflow automation service provided by Microsoft Azure that allows you to create workflows and integrate various services and systems. You can easily integrate a Logic App with Azure Service Bus to perform actions based on messages in a Service Bus queue or topic. Here's an example of how to create a Logic App that interacts with Azure Service Bus:


**Scenario:** Let's create a Logic App that listens to a Service Bus queue and sends an email notification whenever a new message arrives in the queue.

**Prerequisites:**

- An Azure subscription.

- An Azure Service Bus namespace with a queue.

- An Office 365 or Outlook.com email account for sending notifications.

**Step 1: Create a Logic App**

1. Go to the [Azure Portal](https://portal.azure.com/).

2. Click on "Create a resource" and search for "Logic App." Click on "Logic App" in the results, and then click the "Create" button.

3. Configure the Logic App settings, such as the resource group, name, location, and tags.

4. Click "Review + Create" and then "Create" to deploy the Logic App.

**Step 2: Create a Trigger for Service Bus Queue**

1. After the Logic App is created, go to the Logic App Designer.

2. Search for "Service Bus" in the triggers section, and select "When a message is received in a queue (auto-complete)." This will be your trigger.

3. Sign in to your Azure account and configure the connection to your Service Bus namespace and specify the queue name.

4. Save the connection settings.

**Step 3: Define the Action**

1. After configuring the trigger, you can now define what action to take when a new message arrives in the queue.

2. Search for "Office 365 Outlook" in the actions section, and select an action like "Send an email (V2)" or "Send an email."

3. Sign in to your Office 365 or Outlook.com account and configure the email details, such as recipient, subject, and body. You can use dynamic content from the Service Bus trigger to populate email details.

4. Save the action settings.

**Step 4: Save and Enable the Logic App**

1. Save your Logic App workflow.

2. Enable the Logic App by clicking the "Run" button.

**Step 5: Testing**

Now, whenever a new message is added to the Service Bus queue you specified, your Logic App will trigger, and an email notification will be sent using the action you defined.

Remember to configure appropriate error handling and logging based on your requirements to ensure the reliability of your workflow.

This example demonstrates a simple integration between a Logic App and Azure Service Bus. You can extend this to include more complex workflows or integrate with other services as needed for your specific use case.

Optimizing performance in SQL Server

 Optimizing performance in SQL Server

SQL Profiler

Using SQL Profiler we can able to do Monitoring Database Activity,Performance Tuning,Debugging and Troubleshooting, Security Auditing,Replaying Traces,Deadlock Analysis,Monitoring Long-Running Processes and Capacity Planning

Execution plan in SQL server

In SQL Server, an execution plan is a detailed and structured representation of how the SQL Server query optimizer intends to execute a query. The execution plan provides insights into how the database engine will access and manipulate data to return the results of a query. Analyzing execution plans is crucial for optimizing query performance. 

Optimizing performance in SQL Server involves various strategies and techniques to ensure that your database queries run efficiently, minimizing response times and resource usage. Here are some best practices and techniques to optimize performance in SQL Server:

1. Design Efficient Database Schema:

- Properly design tables, indexes, relationships, and data types.

- Normalize your database structure to avoid data redundancy.

- Denormalize for performance if necessary, but be cautious about trade-offs.

 2. Indexing:

 Identify and create appropriate indexes based on the queries your application runs. Over-indexing or under-indexing can both be detrimental.

- Regularly update statistics to ensure the query optimizer makes informed decisions about index usage.

- Use covering indexes to include all columns required for a query to avoid key lookups.

 3. Query Optimization:

- Write efficient queries. Avoid using `SELECT *` when you only need specific columns.

- Use appropriate JOINs. Understand the differences between INNER JOIN, LEFT JOIN, and RIGHT JOIN.

- Use EXISTS, IN, and JOINs wisely based on the context of the query.

- Minimize the use of functions in WHERE clauses, as they can prevent index usage.

4. Avoid Cursors:

- Cursors are generally slower in SQL Server. Whenever possible, use set-based operations instead of cursor-based operations.

5. Stored Procedures and Views:

- Use stored procedures for frequently executed queries. Compiled execution plans can lead to improved performance.

- Consider using indexed views (materialized views) for complex queries to improve query performance.

 6. Partitioning:

- Partition large tables and indexes to spread data across multiple filegroups. This can improve query performance for large datasets.

 7. **Regular Maintenance:**

- Regularly update statistics to ensure the query optimizer has up-to-date information for making execution plans.

- Rebuild or reorganize indexes periodically to reduce fragmentation.

- Schedule database backups, integrity checks, and index maintenance tasks during off-peak hours.

8. Memory and Disk Configuration:

- Configure SQL Server’s memory settings appropriately. Allocate enough memory for SQL Server to cache data and execution plans.

- Ensure that the disk subsystem is optimized. Use RAID configurations for fault tolerance and performance.

9. Use Proper Data Types:

- Choose appropriate data types for your columns. Using smaller data types where applicable can save storage and improve query performance.

- Avoid using TEXT, NTEXT, and IMAGE data types as they are deprecated. Use VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX) instead.

 10. Monitoring and Profiling:

- Use SQL Server Profiler to identify slow queries and bottlenecks.

- Set up monitoring and alerts to proactively identify and address performance issues.

 11. Database Maintenance Plans:

- Implement maintenance plans to automate tasks like backups, index rebuilds, and database consistency checks.

 12. Use Query Execution Plans:

- Analyze query execution plans to identify areas for optimization. Use the SQL Server Management Studio (SSMS) to view and understand execution plans.

13. Tempdb Optimization:

- Tempdb is a system database used for temporary storage. Properly configure tempdb and monitor its performance. Multiple data files and appropriate sizing can help distribute I/O load.

14. Upgrade and Patch:

- Keep your SQL Server instance up to date with the latest service packs and cumulative updates. Microsoft often releases performance improvements and bug fixes in these updates.

By following these best practices and constantly monitoring your SQL Server environment, you can optimize performance, improve responsiveness, and enhance the overall efficiency of your database applications. Regular performance tuning and monitoring are essential for maintaining optimal database performance over time.

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...