Run applications on a Mac using ASP.NET Core and with MongoDB on Docker

It is easy to develop ASP.NET Core applications on a Mac with VSCode, and run them while they rely Docker based data services. The first part is easy because of ASP.NET core's cross platform compatibility. The is very simple even though it sounds impressive.  Services like MongoDB expose the same ports whether they are Docker deployed or directly installed to the operating system.

Our application is an ASP.NET core micro-service example put out by Microsoft. The bookstore REST service API support implements basic CRUD operations. The net result is an ASP.NET web service that uses MongoDB as its' persistent store.

I build this on a Mac because both .NetCore is cross platform and MongoDB is running in a Linux Docker container isolated from the underlying system. You should be able to run this without changes on a PC without modification.

We are using Visual Studio code because it is cross platform and runs on Mac, PC and Linux.  I have run it in the Linux Subsystem on Chromebooks. For this situation, I installed  a few Visual Studio Code extensions
  • ASP.NET and .NET core development and debugging support
  • C# language support
  • Docker integration - optional
  • MongoDB integration - optional

Tutorials

Repositories

Run MongoDB in docker

We're running MongoDB in docker because it makes it super easy to change versions by just running a different container. There are a bunch of tutorials floating around.

Run MongoDB with Docker using your favorite method. You can pull down docker-compose.yml scripts for various services from my GitHub .
  • Look in the mongodb directory.  
  • Run docker-compose while in the mongodb directory. It deploys mongodb and mongodb-express. 
  • mongodb is available on the standard port.
  • The mongodb-express GUI is available at http://localhost:8081/

Configuring and running the MS bookstore example

This assumes you have Visual Studio Code installed along with plugins and the .net core 3.x SDK / runtime.

Create and populate DynamoDB tables

  • Connect to MongoDB in the running container.  The following command assumes the container name is "mongo01" as built from my GitHub repository
    docker exec -it mongo01 mongo
  • Run Commands - you can probably just paste the text in the shell window.
    use BookstoreDb
    show dbs
    db.createCollection('Books')
    db.Books.insertMany([{'Name':'Design Patterns','Price':54.93,'Category':'Computers','Author':'Ralph Johnson'}, {'Name':'Clean Code','Price':43.15,'Category':'Computers','Author':'Robert C. Martin'}])
    db.Books.find({}).pretty()

Configuring Visual Studio Code

Install the necessary plugins and .net core SDK.

Open code with Visual Studio Code

  • Clone the sample github repository
    git clone https://github.com/aspnet/AspNetCore.Docs.git
  • Open Visual Studio Code to
    /GitHub/AspNetCore.Docs/aspnetcore/tutorials/first-mongo-app/samples/3.x/SampleApp
  • You should see output something like
    Installing C# dependencies...
    Platform: darwin, x86_64
    Downloading package 'OmniSharp for OSX' (44567 KB).................... Done!
    Validating download...
    Integrity Check succeeded.
    Installing package 'OmniSharp for OSX'
    Downloading package '.NET Core Debugger (macOS / x64)' (51272 KB).................... Done!
    Validating download...
    Integrity Check succeeded.
    Installing package '.NET Core Debugger (macOS / x64)'
    Downloading package 'Razor Language Server (macOS / x64)' (50386 KB).................... Done!
    Installing package 'Razor Language Server (macOS / x64)'
    Finished
  • VS Code notifications will pop up with
    Required assets to build and debug are missing from 'SampleApp'. Add them?
  • Take the action requested.  Click the button
    There are unresolved dependencies. Please execute the restore command to continue.
  • Take the action requested. Click the Restore button.
  • You should see something like the following in the .Net output log
    Restore completed in 3.74 sec for /Users/xx/Documents/GitHub/AspNetCore.Docs/aspnetcore/tutorials/first-mongo-app/samples/3.x/SampleApp/BooksApi.csproj.
    Done: 0.

Run the program with VS Code

Run Via GUI

  • Run the application with or without debugging using the GUI
    • With Debug
      • Debug -> Start Debugging F5
    • Without Debug
      • Debug -> Start Without Debugging ^F5

Run Via Command Line

You can build and run dotnet core applications from a command prompt.  You can either instal the dot net run time explicitly or use the one available in Visual Studio Code.  The next steps assume you will use VS Code.

  • Open a terminal inside Visual Studio
    Terminal -> new Terminal
  • Build the application - this will be unnecessary because VSCode will have already built it.
    dotnet build
  • Run the application
    dotnet run
  • Terminate the program via the terminal
    control-c

Verification

Connect to this URL with your browser.  You should see an array with two books

History

Created 2019/12/08
Last Edited 2019/12/08

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs