<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arash Yazdanibakhsh</title>
    <description>The latest articles on DEV Community by Arash Yazdanibakhsh (@arashyazdani).</description>
    <link>https://dev.to/arashyazdani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F990095%2Ff483a207-f6e0-4e52-8d4b-a4ee51d25c9c.jpeg</url>
      <title>DEV Community: Arash Yazdanibakhsh</title>
      <link>https://dev.to/arashyazdani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arashyazdani"/>
    <language>en</language>
    <item>
      <title>Running a MongoDB Replica Set Locally: Docker Desktop &amp; Native Windows Setup</title>
      <dc:creator>Arash Yazdanibakhsh</dc:creator>
      <pubDate>Thu, 01 May 2025 21:12:57 +0000</pubDate>
      <link>https://dev.to/arashyazdani/running-a-mongodb-replica-set-locally-docker-desktop-native-windows-setup-233</link>
      <guid>https://dev.to/arashyazdani/running-a-mongodb-replica-set-locally-docker-desktop-native-windows-setup-233</guid>
      <description>&lt;p&gt;When developing modern applications, a replica set configuration is essential to simulate production environments that leverage MongoDB’s high availability and redundancy. In this article, I’ll share two approaches:&lt;/p&gt;

&lt;p&gt;Deploying a MongoDB Replica Set Using Docker Desktop&lt;br&gt;
Running a MongoDB Replica Set Natively on Windows Without Docker Desktop&lt;br&gt;
Each method has its merits depending on your environment and constraints.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deploying a MongoDB Replica Set with Docker Desktop
Docker is a great way to quickly spin up isolated environments. In this section, we’ll create a replica set using a custom Docker network and a Docker Compose file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 1: Create a Docker Network&lt;/p&gt;

&lt;p&gt;First, create an external Docker network that all MongoDB containers will share. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create mongo-cluster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Prepare the docker-compose.yml File&lt;/p&gt;

&lt;p&gt;Create a docker-compose.yml file with the following configuration. This file defines three services — one primary and two secondaries. Each service is configured to run with the replica set parameter ( — replSet rs0) and bind to all IP addresses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: ‘3.8’

services:

mongo-primary:

image: mongo:latest

container_name: mongo-primary

restart: always

command: mongod — replSet rs0 — bind_ip_all

ports:

- 27017:27017

volumes:

- E:/Program Files/MongoDB/primary/data:/data/db

networks:

- mongo-cluster

mongo-secondary1:

image: mongo:latest

container_name: mongo-secondary1

restart: always

command: mongod — replSet rs0 — bind_ip_all

ports:

- 27018:27017

volumes:

- E:/Program Files/MongoDB/secondary1/data:/data/db

networks:

- mongo-cluster

mongo-secondary2:

image: mongo:latest

container_name: mongo-secondary2

restart: always

command: mongod — replSet rs0 — bind_ip_all

ports:

- 27019:27017

volumes:

- E:/Program Files/MongoDB/secondary2/data:/data/db

networks:

- mongo-cluster

networks:

mongo-cluster:

external: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Adjust the volume paths (E:/Program Files/MongoDB/…) to your local directories as needed.&lt;/p&gt;

&lt;p&gt;Step 3: Start the Containers&lt;/p&gt;

&lt;p&gt;In the directory containing your docker-compose.yml file, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command starts the three containers in detached mode.&lt;/p&gt;

&lt;p&gt;Step 4: Initialize the Replica Set&lt;/p&gt;

&lt;p&gt;Connect to the Primary Container:&lt;br&gt;
Run the following command to open an interactive Mongo shell (mongosh) session inside the primary container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it mongo-primary mongosh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initiate the Replica Set:&lt;br&gt;
Within the shell, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rs.initiate({_id: “rs0”,members: [{ _id: 0, host: “host.docker.internal:27017”},{ _id: 1, host: “host.docker.internal:27018” },{ _id: 2, host: “host.docker.internal:27019” }]});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify the Setup:&lt;br&gt;
Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rs.status();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command shows the status of your replica set. After the verification Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And go to step 5.&lt;/p&gt;

&lt;p&gt;Step 5: Verify Data Persistence&lt;/p&gt;

&lt;p&gt;Check your Docker volumes to ensure data is being persisted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker volume ls

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connection String&lt;/p&gt;

&lt;p&gt;Connect your application using the following connection string:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mongodb://host.docker.internal:27017,host.docker.internal:27018,host.docker.internal:27019/?replicaSet=rs0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Tip: If host.docker.internal fails to resolve on Windows, add the following entry to your hosts file:&lt;br&gt;
&lt;code&gt;127.0.0.1 host.docker.internal&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Running a MongoDB Replica Set Natively on Windows (Without Docker Desktop)
If you prefer or need to run MongoDB directly on Windows without containerization, follow these steps.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 1: Download and Install mongosh&lt;/p&gt;

&lt;p&gt;Make sure you have mongosh installed. This shell will help you manage and interact with your replica set.&lt;/p&gt;

&lt;p&gt;Step 2: Configure MongoDB via mongod.cfg&lt;/p&gt;

&lt;p&gt;Prepare separate configuration files for each MongoDB instance. Below is an example configuration (mongod.cfg) for one node:&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;h1&gt;
  
  
  mongod.conf
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Where and how to store data.
&lt;/h1&gt;

&lt;p&gt;storage:&lt;/p&gt;

&lt;p&gt;dbPath: C:\Program Files\MongoDB\Server\7.0\data&lt;/p&gt;

&lt;h1&gt;
  
  
  Where to write logging data.
&lt;/h1&gt;

&lt;p&gt;systemLog:&lt;/p&gt;

&lt;p&gt;destination: file&lt;/p&gt;

&lt;p&gt;logAppend: true&lt;/p&gt;

&lt;p&gt;path: C:\Program Files\MongoDB\Server\7.0\log\mongod.log&lt;/p&gt;

&lt;h1&gt;
  
  
  Network interfaces
&lt;/h1&gt;

&lt;p&gt;net:&lt;/p&gt;

&lt;p&gt;port: 27017&lt;/p&gt;

&lt;p&gt;bindIp: 127.0.0.1&lt;/p&gt;

&lt;h1&gt;
  
  
  Replica set configuration
&lt;/h1&gt;

&lt;p&gt;replication:&lt;/p&gt;

&lt;p&gt;replSetName: “rs0”`&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Note: Create a similar configuration file for each MongoDB instance, ensuring that the port numbers and data directories are unique (e.g., 27017, 27018, 27019).&lt;/p&gt;

&lt;p&gt;Step 3: Start the MongoDB Instances&lt;/p&gt;

&lt;p&gt;Run each MongoDB server instance as an administrator. You can launch them manually using a command prompt or create a batch file. For example, to run the instance on port 27017, execute:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
“C:\Program Files\MongoDB\Server\7.1\bin\mongod” — config “C:\Program Files\MongoDB\Server\7.1\bin\mongod.cfg”&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For convenience, you can create a .bat file with the following content:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/echo"&gt;@echo&lt;/a&gt; off&lt;/p&gt;

&lt;p&gt;start “MongoDB 7.1” “C:\Program Files\MongoDB\Server\7.1\bin\mongod” — config “C:\Program Files\MongoDB\Server\7.1\bin\mongod.cfg”&lt;/p&gt;

&lt;p&gt;start “MongoDB 7.2” “C:\Program Files\MongoDB\Server\7.2\bin\mongod” — config “C:\Program Files\MongoDB\Server\7.2\bin\mongod.cfg”&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
*&lt;em&gt;Run the batch file as an administrator.&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
Step 4: Manually Initiate the Replica Set&lt;/p&gt;

&lt;p&gt;Launch the Primary Instance:&lt;br&gt;
If not already running, start the primary node with:&lt;br&gt;
mongod — port 27017 — dbpath “C:\Program Files\MongoDB\Server\7.0\data” — replSet rs0&lt;/p&gt;

&lt;p&gt;Connect with mongosh:&lt;br&gt;
Open mongosh as an administrator and execute:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
rs.initiate({_id: “rs0”,members: [{ _id: 0, host: “127.0.0.1:27017” },{ _id: 1,host: “127.0.0.1:27018” },{ _id: 2, host: “127.0.0.1:27019” }]});&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Step 5: Optionally Install as Windows Services&lt;/p&gt;

&lt;p&gt;For an even smoother development experience, you can install each MongoDB instance as a Windows service. Run the following commands as an administrator:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;p&gt;“C:\Program Files\MongoDB\Server\7.1\bin\mongod.exe” — config “C:\Program Files\MongoDB\Server\7.1\bin\mongod.cfg” — install — serviceName “MongoDB7.1”&lt;/p&gt;

&lt;p&gt;“C:\Program Files\MongoDB\Server\7.2\bin\mongod.exe” — config “C:\Program Files\MongoDB\Server\7.2\bin\mongod.cfg” — install — serviceName “MongoDB7.2”&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, register the services with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
sc.exe create “MongoDB7.1” binPath= “\”C:\Program Files\MongoDB\Server\7.1\bin\mongod.exe\” — service — config=\”C:\Program Files\MongoDB\Server\7.1\bin\mongod.cfg\”” DisplayName= “MongoDB 7.1” start= auto&lt;/p&gt;

&lt;p&gt;sc.exe create “MongoDB7.2” binPath= “\”C:\Program Files\MongoDB\Server\7.2\bin\mongod.exe\” — service — config=\”C:\Program Files\MongoDB\Server\7.2\bin\mongod.cfg\”” DisplayName= “MongoDB 7.2” start= auto&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, start the services:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
net start MongoDB7.1&lt;/p&gt;

&lt;p&gt;net start MongoDB7.2&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: Running MongoDB as a Windows service may present some challenges; evaluate this option based on your project needs.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Both methods enable you to run a MongoDB replica set locally for testing and development:&lt;/p&gt;

&lt;p&gt;Docker Desktop: Provides an isolated, containerized environment that closely mirrors cloud deployments. It’s quick to set up and perfect for multi-node configurations without polluting your host system.&lt;br&gt;
Native Windows Setup: Offers a more traditional approach, especially when containerization is not an option. This method uses configuration files, command-line instructions, and optional Windows service installation.&lt;br&gt;
By understanding and leveraging these approaches, you can develop and test applications that depend on MongoDB’s replica set features with confidence.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Deep Dive into YARP and Ocelot</title>
      <dc:creator>Arash Yazdanibakhsh</dc:creator>
      <pubDate>Sat, 02 Nov 2024 19:51:57 +0000</pubDate>
      <link>https://dev.to/arashyazdani/a-deep-dive-into-yarp-and-ocelot-4dbo</link>
      <guid>https://dev.to/arashyazdani/a-deep-dive-into-yarp-and-ocelot-4dbo</guid>
      <description>&lt;p&gt;Navigating API Gateway Options in .NET Core Microservices: A Deep Dive into YARP and Ocelot&lt;br&gt;
In the evolving landscape of .NET Core microservices, selecting an appropriate API gateway or reverse proxy is fundamental. These tools not only manage routing but also handle authentication, load balancing, and service orchestration, ultimately shaping the scalability and resilience of microservices architectures. Two prominent solutions are YARP (Yet Another Reverse Proxy) and Ocelot, each serving distinct roles within this ecosystem. This article explores the configuration, capabilities, and use cases of both, providing a roadmap for developers and architects in making optimal choices.&lt;br&gt;
YARP and Ocelot at a Glance&lt;br&gt;
YARP—Microsoft’s tailored reverse proxy toolkit—caters to high-performance and scalable microservices architectures. Built with modern .NET Core features, YARP emphasizes high throughput and efficient memory use, making it highly suitable for complex, high-demand systems.&lt;br&gt;
In contrast, Ocelot is a lighter, developer-friendly API gateway crafted specifically for .NET Core environments. Its streamlined setup, focused on routing and middleware, is ideal for less-intensive applications with straightforward traffic requirements and moderate scalability needs.&lt;br&gt;
Configuration Insights and Key Differences&lt;br&gt;
YARP Configuration&lt;br&gt;
YARP’s configuration is notably flexible, offering a high degree of control adaptable to dynamic operational demands. It allows configuration through various mediums, including appsettings.json, environment variables, and inline code.&lt;br&gt;
Key Configuration Features:&lt;br&gt;
• Advanced Routing Rules: Supports complex patterns and rewrites, configurable via JSON or fluent APIs, providing extensive customization for traffic management.&lt;br&gt;
• Load Balancing Strategies: Offers dynamic load balancing with options like Round Robin, Least Requests, and Random, essential for managing high-volume requests.&lt;br&gt;
• Health Checks: Integrated active and passive health checks ensure traffic routes exclusively to healthy instances, enhancing reliability.&lt;br&gt;
Example Configuration Snippet in appsettings.json:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"ReverseProxy": {&lt;br&gt;
  "Routes": {&lt;br&gt;
    "route1": {&lt;br&gt;
      "ClusterId": "cluster1",&lt;br&gt;
      "Match": {&lt;br&gt;
        "Path": "/api/{**catch-all}"&lt;br&gt;
      }&lt;br&gt;
    }&lt;br&gt;
  },&lt;br&gt;
  "Clusters": {&lt;br&gt;
    "cluster1": {&lt;br&gt;
      "Destinations": {&lt;br&gt;
        "destination1": {&lt;br&gt;
          "Address": "http://localhost:5000/"&lt;br&gt;
        }&lt;br&gt;
      }&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ocelot’s configuration is purposefully straightforward, managed through a JSON configuration file that integrates seamlessly with .NET Core’s configuration system. This simplicity makes it an attractive option for microservices novices or projects with limited complexity.&lt;br&gt;
Key Configuration Features:&lt;br&gt;
• Routing: Defined in JSON, enabling intuitive setup for downstream paths, upstream templates, and HTTP methods.&lt;br&gt;
• Middleware Integration: Includes delegating handlers for authentication, caching, and rate-limiting to improve security and performance.&lt;br&gt;
• Service Discovery: Supports dynamic service discovery through registries like Consul, which enables effortless service scaling and management.&lt;br&gt;
Example Configuration Snippet in ocelot.json:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
  "ReRoutes": [&lt;br&gt;
    {&lt;br&gt;
      "DownstreamPathTemplate": "/api/values",&lt;br&gt;
      "UpstreamPathTemplate": "/values",&lt;br&gt;
      "UpstreamHttpMethod": ["Get"],&lt;br&gt;
      "ServiceName": "valueService"&lt;br&gt;
    }&lt;br&gt;
  ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Real-World Use Cases&lt;br&gt;
YARP Use Cases&lt;br&gt;
YARP shines in enterprise-level scenarios, particularly where managing extensive traffic or complex routing is essential. It’s an ideal choice for high-demand environments requiring sophisticated load balancing, multi-cloud support, and advanced configuration capabilities.&lt;br&gt;
Ocelot Use Cases&lt;br&gt;
Ocelot is well-suited for smaller applications or organizations beginning their transition to microservices. Its straightforward configuration and deployment make it perfect for projects that don’t demand the robustness of YARP but benefit from a manageable and effective API gateway solution.&lt;br&gt;
Conclusion&lt;br&gt;
The decision between YARP and Ocelot ultimately depends on your project’s scale and specific requirements. YARP provides advanced control and scalability for enterprise-grade microservices, while Ocelot offers a lightweight and accessible solution for simpler use cases. Both tools empower developers and architects to enhance the efficiency, security, and maintainability of .NET Core microservices architectures, paving the way for future growth and flexibility.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why my mock object not working truly in xUnit test project?</title>
      <dc:creator>Arash Yazdanibakhsh</dc:creator>
      <pubDate>Wed, 14 Dec 2022 10:07:44 +0000</pubDate>
      <link>https://dev.to/arashyazdani/why-my-mock-object-not-working-truly-in-xunit-test-project-4kl3</link>
      <guid>https://dev.to/arashyazdani/why-my-mock-object-not-working-truly-in-xunit-test-project-4kl3</guid>
      <description>&lt;p&gt;I published my question on StackOverFlow help me please.&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/74791761/why-my-mock-object-not-working-truly-in-xunit-test-project" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/74791761/why-my-mock-object-not-working-truly-in-xunit-test-project&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
