25 October 2023

Duplicate Detection in service bus

 Duplicate Detection in service bus

Duplicate Detection in Azure Service Bus is a feature that helps prevent the storage of duplicate copies of messages within a specific timeframe. When you enable duplicate detection, Service Bus ensures that messages with the same `MessageId` property are either discarded or accepted based on your configuration.

Here are the key points to understand about Duplicate Detection in Azure Service Bus:

 1. MessageId Property:

   - Each message sent to a queue or topic in Azure Service Bus can have a `MessageId` property. This property should be set to a unique value for each message.

2. Duplicate Detection Window:

   - When you enable Duplicate Detection, you specify a **Duplicate Detection Window**. This window defines the time duration during which Service Bus examines the `MessageId` property to identify and eliminate duplicates.

 3. How it Works:

   - When a message is sent with a `MessageId`, Service Bus checks the `MessageId` against the messages in the Duplicate Detection Window.

   - If a message with the same `MessageId` is found within the specified window, the new message is treated as a duplicate and is not enqueued.

 4. Enabling Duplicate Detection:

   - You can enable Duplicate Detection when creating a queue or topic.

   - When creating the queue or topic, you can specify the `DuplicateDetectionHistoryTimeWindow`, which is the duration of the detection window.

   Example (using Azure SDK for .NET):

   csharp

   QueueDescription queueDescription = new QueueDescription("MyQueue")

   {

       // Set Duplicate Detection Window to 10 minutes

       DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(10)

   };

  5. Message Expiration and Duplicate Detection:

   - If a message expires before the Duplicate Detection Window, it is removed from the system and won't be considered for duplicate detection even if a duplicate arrives later.

6. Considerations:

   - **Message Ordering:** If you require message ordering and use duplicate detection, ensure that the `MessageId` values are unique for all messages within the detection window. Otherwise, messages with the same `MessageId` might be considered duplicates and could affect the ordering.

7. Use Cases:

   - Duplicate Detection is useful in scenarios where it's crucial to ensure that a message is processed only once, preventing duplicates from causing unintended actions or data inconsistencies in the receiving application.

Enabling Duplicate Detection helps maintain data integrity and prevents unintended processing of duplicate messages within your Azure Service Bus queues and topics.

22 October 2023

19 October 2023

Can multiple datatype support in array and List in c#

 Can multiple datatype support in array and List in c#

In C#, arrays are collections of elements that must all have the same data type. This means that all elements in a C# array must be of a uniform data type. For example, if you create an array of integers, you cannot store other data types such as strings or floats in the same array.

Here's an example of creating an array of integers in C#:

```csharp

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

In this example, `numbers` is an array of integers, and you can only store integer values in it. Attempting to store a different data type in this array would result in a compilation error.

If you need to store multiple data types in a collection, you can use other data structures in C# such as `List<T>` from the `System.Collections.Generic` namespace. `List<T>` allows you to store elements of different data types because it is a generic collection that can be parameterized with any data type.

Here's an example of using `List<T>` to store elements of different data types:

```csharp

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        List<object> mixedList = new List<object>();

        mixedList.Add(1);        // integer

        mixedList.Add("hello");  // string

        mixedList.Add(3.14);     // double

        foreach (var item in mixedList)

        {

            Console.WriteLine(item);

        }

    }

}

In this example, `mixedList` is a `List<object>` that can store elements of different data types by treating them as `object`. However, it's important to note that using `List<object>` can lead to loss of type safety and may require explicit casting when retrieving elements from the list.

18 October 2023

Youtube Tutorial for Dotnet and Azure from Experts

 Youtube Tutorial for Dotnet and Azure from Experts

Dotnet and Azure

Milan Jovanović

Dependency Inversion Principle (DIP)

 Dependency Inversion Principle (DIP)

The Dependency Inversion Principle (DIP) is one of the SOLID principles of object-oriented design. It suggests that high-level modules (e.g., business logic) should not depend on low-level modules (e.g., database access, external services). Instead, both high-level and low-level modules should depend on abstractions (interfaces or abstract classes).

In simpler terms, the Dependency Inversion Principle advocates that the direction of dependency should be toward abstractions, not concretions. This allows for decoupling between components, making the system more flexible, maintainable, and easier to extend.

Let's explore the Dependency Inversion Principle with an example in C#. Consider a scenario where you have a high-level module representing a class `BusinessLogic` that needs to save data to a database. Following DIP, you would define an interface representing the database operations:

```csharp

// Database interface representing the operations needed by BusinessLogic

public interface IDatabase

{

    void SaveData(string data);

}

Now, the `BusinessLogic` class depends on the `IDatabase` interface, not on a specific database implementation. It can work with any class that implements this interface. For example:

```csharp

// High-level module depending on abstraction (IDatabase interface)

public class BusinessLogic

{

    private readonly IDatabase _database;


    public BusinessLogic(IDatabase database)

    {

        _database = database;

    }

    public void ProcessData(string data)

    {

        // Process data

        Console.WriteLine("Processing data: " + data);


        // Save data using the injected database implementation

        _database.SaveData(data);

    }

}

Now, you can have different database implementations that adhere to the `IDatabase` interface. For instance, let's create a `SqlServerDatabase` class:

```csharp

// Low-level module implementing IDatabase interface

public class SqlServerDatabase : IDatabase

{

    public void SaveData(string data)

    {

        Console.WriteLine("Saving data to SQL Server database: " + data);

        // Save data to SQL Server

    }

}

In this example, the `BusinessLogic` class depends on the `IDatabase` interface, allowing for flexibility in the choice of database implementation. This adherence to abstraction instead of concretions is the essence of the Dependency Inversion Principle. It promotes the use of interfaces and abstractions to achieve loose coupling between components, making the system more modular and easier to maintain.

Interface Segregation Principle (ISP)

  Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. It states that no client should be forced to depend on methods it does not use. In simpler terms, it suggests that a class should not be forced to implement interfaces it doesn't use. Instead of having large interfaces with many methods, it's better to have smaller, more specific interfaces.

Let's explore the Interface Segregation Principle with an example in C#. Imagine you have an interface called `IWorker` that represents different tasks a worker can do:

```csharp

public interface IWorker

{

    void Work();

    void Eat();

    void Sleep();

}

In this interface, a worker can work, eat, and sleep. However, consider a scenario where you have different types of workers - regular workers who do all tasks, and part-time workers who only work and eat. If both types of workers are forced to implement the `IWorker` interface as it is, it would violate the Interface Segregation Principle because the part-time workers would be implementing methods they don't use (`Sleep` method).

A better approach would be to segregate the interface into smaller interfaces, each representing a specific functionality. For example:

```csharp

public interface IWorkable

{

    void Work();

}

public interface IEatable

{

    void Eat();

}

public interface ISleepable

{

    void Sleep();

}

Now, the regular workers can implement all three interfaces, while the part-time workers only need to implement `IWorkable` and `IEatable`, avoiding the unnecessary implementation of the `Sleep` method.

Here's an implementation of regular and part-time workers using the segregated interfaces:

```csharp

public class RegularWorker : IWorkable, IEatable, ISleepable

{

    public void Work()

    {

        Console.WriteLine("Regular worker is working.");

    }

    public void Eat()

    {

        Console.WriteLine("Regular worker is eating.");

    }

    public void Sleep()

    {

        Console.WriteLine("Regular worker is sleeping.");

    }

}

public class PartTimeWorker : IWorkable, IEatable

{

    public void Work()

    {

        Console.WriteLine("Part-time worker is working.");

    }

    public void Eat()

    {

        Console.WriteLine("Part-time worker is eating.");

    }

}

By adhering to the Interface Segregation Principle, you create more specialized and cohesive interfaces, leading to more maintainable and flexible code. Classes and objects can implement only the interfaces that are relevant to them, avoiding unnecessary dependencies and ensuring that clients are not forced to depend on methods they don't use.

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...