25 October 2023

Difference between Queue and topics in service bus

Difference between Queue and topics in service bus

Azure Service Bus provides two types of messaging entities: Queues and Topics/Subscriptions. While both serve as communication channels between different components of an application, they have distinct characteristics and use cases.

1. Queues:

- Point-to-Point Communication: Queues implement a one-to-one messaging pattern. A message sent to a queue is processed by a single consumer (receiver). It ensures that each message is consumed by only one receiver, making it suitable for point-to-point communication scenarios.

- Load Balancing: Multiple receivers can compete for messages in a queue. However, each message is processed by only one receiver. This enables load balancing among multiple consumers.

- Sequential Processing: Messages in a queue are processed in the order of arrival, ensuring sequential processing if needed.

- Guaranteed Delivery: Queues provide at-least-once delivery, meaning each message is guaranteed to be delivered to a receiver, ensuring reliable message processing.

- Example Use Case: Order processing system where each order needs to be processed by only one receiver to avoid duplication.

2. Topics/Subscriptions:

- Publish/Subscribe Pattern: Topics and Subscriptions implement a publish/subscribe messaging pattern. A message sent to a topic can be received by multiple consumers (subscribers) who have subscriptions to that topic. Subscriptions act as filters, allowing subscribers to receive specific subsets of messages.

- Message Multicasting: Messages sent to a topic are automatically multicasted to all eligible subscriptions. Each subscription can define rules to filter messages based on message properties.

- Multiple Subscribers: Multiple subscribers can receive the same message if they have subscriptions matching the message's properties. This allows for message broadcasting to interested parties.

- Example Use Case: News updates service where different subscribers might be interested in different categories of news (sports, politics, entertainment), and each category is a separate subscription.

 Key Differences:

- Communication Pattern: Queues facilitate point-to-point communication, while Topics/Subscriptions enable publish/subscribe communication patterns.

- Number of Subscribers: Queues have one receiver per message, whereas Topics/Subscriptions can have multiple subscribers receiving the same message if they match the subscription criteria.

- Filtering: Topics/Subscriptions allow message filtering based on properties, enabling more fine-grained control over which messages subscribers receive.

- Message Multicasting: Topics automatically multicast messages to all eligible subscriptions, allowing for efficient message distribution to multiple subscribers.

- Scalability: Topics/Subscriptions are more suitable for scenarios where messages need to be broadcasted to a large number of subscribers with different interests.

Choose between queues and topics/subscriptions based on your application's messaging requirements. If you need point-to-point communication and guaranteed delivery, use queues. If you need publish/subscribe capabilities and message filtering for multiple subscribers, use topics and subscriptions.

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.

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