Programming Languages on Power

 View Only

Write a console application that connects from a .NET C# program to MongoDB

By Janani Janakiraman posted Sat January 14, 2023 01:10 PM


Running .NET applications on Power seems to be the need of hour. With the recent announcement that .NET 7 is supported on Linux on Power, you can now run your .NET applications on Power.

In this blog, you will learn how to write a console application that connects from a .NET C# program to a back-end MongoDB server (an open source, non relational database management system) to retrieve a list of databases.


To complete the steps in this blog, you need to have a Power virtual machine with RHEL 8.7 installed. If you need access to Power hardware, see, Accelerate your open source development with access to IBM Power resources, which lists several free and low-cost options.

You should be able to complete the following exercise in under an hour.


Complete the following steps to successfully write a console application that connects a .NET C# program to a back-end MongoDB server.

  1. Set up the Community or Enterprise version of MongoDB Server. This example shows the setup of the MongoDB Enterprise using RPM.

    • Download the RPM from:

    • Install and start the MongoDB server. It is a good practice to do a dnf update first to make sure everything is up-to-date before installing packages, otherwise you may see varied results in what gets installed.

      # Download the RPM
      #Install the RPM
      dnf update
      dnf install mongodb-enterprise-server-6.0.3-1.el8.ppc64le.rpm

      The sample output below shows some of the dependencies for the mongodb-enterprise-server.

       # dnf install mongodb-enterprise-server-6.0.3-1.el8.ppc64le.rpm
      Package Architecture Version Repository Size
      mongodb-enterprise-server ppc64le 6.0.3-1.el8 @commandline 31 M
      Installing dependencies:
      cyrus-sasl ppc64le 2.1.27-6.el8_5 beaker-BaseOS 101 k
      cyrus-sasl-gssapi ppc64le 2.1.27-6.el8_5 beaker-BaseOS 51 k
      cyrus-sasl-plain ppc64le 2.1.27-6.el8_5 beaker-BaseOS 49 k
      mariadb-connector-c ppc64le 3.1.11-2.el8_3 beaker-AppStream 215 k
      mariadb-connector-c-config noarch 3.1.11-2.el8_3 beaker-AppStream 15 k
      net-snmp ppc64le 1:5.8-25.el8 beaker-AppStream 355 k
      net-snmp-agent-libs ppc64le 1:5.8-25.el8 beaker-AppStream 740 k
      net-snmp-libs ppc64le 1:5.8-25.el8 beaker-BaseOS 847 k
      Transaction Summary
  2. Create a directory to store the default databases and start the MongoDB server. The server comes with some sample databases that are already created. You can create additional databases and objects with MongoDB Compass, the GUI for MongoDB, which is the easiest way to explore and manipulate your data. It can be downloaded free for development environments.

    mkdir -p /data/db
    #Start the MongoDB Server
    nohup /usr/bin/mongod --dbpath /data/db --bind_ip_all &
  3. Install dotnet. See the Red Hat Product Documentation for more details about the installation.

    • Verify that dotnet is installed correctly by running the  dotnet --info command:

      # Install dotnet
      dnf update
      dnf install dotnet-sdk-7.0
      # Verify that dotnet is installed
      dotnet --info
    • You should see the following output:

      # dotnet --info
      .NET SDK:
       Version:   7.0.100
       Commit:    e12b7af219
      Runtime Environment:
       OS Name:     rhel
       OS Version:  8
       OS Platform: Linux
       RID:         rhel.8-ppc64le
       Base Path:   /usr/lib64/dotnet/sdk/7.0.100/
        Version:      7.0.0
        Architecture: ppc64le
        Commit:       d099f075e4
      .NET SDKs installed:
        7.0.100 [/usr/lib64/dotnet/sdk]
      .NET runtimes installed:
        Microsoft.AspNetCore.App 7.0.0 [/usr/lib64/dotnet/shared/Microsoft.AspNetCore.App]
        Microsoft.NETCore.App 7.0.0 [/usr/lib64/dotnet/shared/Microsoft.NETCore.App]
      Other architectures found:
      Environment variables:
        DOTNET_ROOT       [/usr/lib64/dotnet]
      global.json file:
        Not found
      Learn more:
  4. Create a sample console C# program that connects to MongoDB and lists the databases.

    • Create a console application and add the .NET Driver for MongoDB MongoDB.Driver to the C# project file. 

      # Create the console application
      dotnet new console -o csharp-mongo
      # Change dir to application directory
      cd csharp-mongo
      # Add package MongoDB.Driver
      dotnet add package MongoDB.Driver
    • View the csproj file. Note that the TargetFramework is set to net7.0 based on the version of dotnet installed on the machine. Also note that in the csproj file, PackageReference tag includes the MongoDB.Driver.

       cat csharp-mongo.csproj 
       <Project Sdk="Microsoft.NET.Sdk">
          <PackageReference Include="MongoDB.Driver" Version="2.18.0" />
  5. Edit the Program.cs file to connect to the MongoDB server and list the databases.

    cat Program.cs
    using System;
    using MongoDB.Driver;
    namespace test
        class Program
            static void Main(string[] args)
                // Replace with connection stringfor your MongoDB server
                MongoClient dbClient = new MongoClient("mongodb://");
                var dbList = dbClient.ListDatabases().ToList();
                Console.WriteLine("The list of databases on this server is: ");
                foreach (var db in dbList)
  6. Build and run the program.

    dotnet restore
    dotnet build
    dotnet run

    The output of the run should list the databases on the server. Below is an example of what the output may look like.

    # dotnet run
    The list of databases on this server is: 
    { "name" : "admin", "sizeOnDisk" : 32768.0, "empty" : false }
    { "name" : "config", "sizeOnDisk" : 61440.0, "empty" : false }
    { "name" : "local", "sizeOnDisk" : 32768.0, "empty" : false }


This brings us to the end of this exercise where you learned how to write a simple console application that connects from a .NET C# program to a back-end MongoDB server and lists the sample databases. If you want to continue, you can extend the sample program in this blog or write your own .NET application to connect to the Mongo DB server that you have set up. Look for other blogs with sample .NET applications connecting to other databases. Drop comment below if you want an example of connecting to a different database.

Additional resources