Skip to content

integration mongodb setup

Brian Greco edited this page Apr 1, 2023 · 2 revisions

IMAGE MongoDB: Plugin Setup

The NetFusion.Integration.MongoDB plugin provides the IMongoDbClient service for injection into a component for a given database settings class. This service is mostly used by repositories.

Adding MongoDB Plugin to Microservice

The following assumes the Microservice template, provided by NetFusion, was used to create the initial microservice based solution. Execute the following to create a solution using the template. First, install the template by executing the following:

dotnet new install NetFusion.Templates

Create a directory for the example:

mkdir Examples.MongoDB

Execute the following to generate the solution within the directory:

cd Examples.MongoDB
dotnet new netfusion-microservice -p 5004
cd src/
dotnet build

The solution containing the completed MongoDB examples can be found here:

NetFusion-Examples/Examples/Source/Examples.MongoDB

Add MongoDB Nuget Package

Since the use of MongoDB is an infrastructure concern, add a reference to the NetFusion.Infrastructure.MongoDB Nuget containing the plugin to the infrastructure project as follows:

dotnet add ./src/Components/Examples.MongoDB.Infra/Examples.MongoDB.Infra.csproj package NetFusion.Integration.MongoDB

Register Plugin

Edit the Program.cs file for the Microservice's WebApi project to add the plugin to the composite-application. Add the following lines as shown below in the complete code listing:

    // Add Plugins to the Composite-Container:
    builder.Services.CompositeContainer(builder.Configuration, new SerilogExtendedLogger())
        .AddSettings()
        .AddMongoDb()		// <-- Add this line

        .AddPlugin<InfraPlugin>()
        .AddPlugin<AppPlugin>()
        .AddPlugin<DomainPlugin>()
        .AddPlugin<WebApiPlugin>()
        .Compose();

Configuration Settings

Define the configuration section within app settings.json used by the plugin:

{
  "logging": {
    "seqUrl": "http://localhost:5341"
  },
  "netfusion": {
    "mongoDB": {
      "geographicDB": {
        "MongoUrl": "mongodb://localhost:27027",
        "DatabaseName": "geographic"
      }
    }
  }
}

Running SEQ and MongoDB in Docker

The above configuration file specifies connections based on instances of SEQ and MongoDB running within Docker. To run the examples locally, the following script can be executed to deploy both services to docker:

cd NetFusion-Examples/Examples/Source/Examples.MongoDB/docker
docker-compose up -d

When completed, the following can be executed to remove the services running in Docker:

cd NetFusion-Examples/Examples/Source/Examples.MongoDB/docker
docker-compose down
docker volume rm dev-seq_data
docker volume rm dev-mongo_data
docker volume rm dev-mongo_logs

The SEQ log Web interface can be found here: http://localhost:8051/

A Web based client for Mongo can be found here: http://localhost:8081 User: admin, Password: admin

The example microservice used within the examples can be executed as follows:

cd Examples.MongoDB/src/Examples.MongoDB.WebApi/
dotnet run

Resources

Once the MongoDB collection instance is obtained, any of the documented features can be used:

Clone this wiki locally