Develop a Virtual Assistant with IBM Watson and .NET Core on IBM Cloud Code Engine-Part 1

Jean Paul Tabja
7 min readJan 8, 2021

This is the first of a series of articles that I will publish to understand IBM Cloud and different services which I consider hidden gems including Watson services but also new services like IBM Cloud Code Engine to deploy containerized cloud native workloads.

The objective of this series of articles is to develop a Bank Virtual Assistant using .NET Core which is a supported platform on IBM Cloud including Cloud Foundry runtimes, IBM Kubernetes Services, IBM Cloud Code Engine, Red Hat OpenShift, IBM Functions and other services. NET Core is a development framework that is being adopted by developers around the globe, however there are not many articles on how to work with .NET Core on IBM Cloud. I hope that this and the following articles will help .NET Core developers to experiment and adopt IBM Cloud.

The articles of this series are:

  1. Develop and deploy the virtual assistant web site with .NET Core and IBM Code Engine (Part 1)
  2. Develop the Virtual Assistant with IBM Watson Assistant and integrate it with the web site (Part 2)
  3. Add search capabilities to the Virtual Assistant using IBM Watson Discovery (Part 3)
  4. Develop a serverless function with .NET Core and IBM Functions to query the account balance from the virtual assistant (Part 4)
IBM Cloud Code Engine

Prerequisites

In order to complete the examples provided in this series you will need the following:

  1. An IBM Cloud account
  2. A GitHub account
  3. A Dockerhub account
  4. Docker Desktop (I use Docker Desktop for Windows)
  5. Visual Studio 2019 Community Edition

Develop and deploy the virtual assistant web site with .NET Core and IBM Code Engine

In this article (first article of the series) I will explain how to develop and deploy the virtual assistant hosting web site using Visual Studio with .NET Core. I will explain how to generate a valid docker image that can be deployed on different Kubernetes Services including IBM Kubernetes Service, IBM Code Engine and Red Hat OpenShift. This will be achieved through the following steps:

  1. Create the Web Application with Visual Studio 2019 and .NET Core
  2. Integrate the source code with GitHub
  3. Generate the docker image and publish it to Dockerhub
  4. Deploy the source code or the docker image to IBM Code Engine

Create the Web Application with Visual Studio and .NET Core

We start creating the Web Site using the ASP .NET Core Web Application template

ASP .NET Core Web Application Template

Click Next and enter the name of your project. In my case the name is new newbankvirtualassistant and the folder location is C:\ibmcloud\labs.

Configure Project name and Location

Click Create. In the next screen select NET Core 3.0 Framework and Web Application (Model View Controller) as project template.

Click Create. Visual Studio will show the created project in the Solution Explorer.

.NET Core Web Application Project in Visual Studio Solution Explorer

Integrate the source code with GitHub

In the final step we will deploy the web application in IBM Code Engine. IBM Code Engine supports deployment of a containerized application from a docker image or from source code directly. That is the reason why we are pushing our code to github.com.

Right click on the Solution and then click on Create Git Repository

Add solution to Git Repository

In the next screen enter your github.com account details and project information.

GitHub account and project information

Verify the creation of the repository on GitHub.

Git repository on GitHub

Generate the Docker image for the web site application

One of the challenges when creating docker images is security configuration. There are other challenges in order to make your .NET Core image to run in the different public Cloud services out there. The following dockerfile has worked for me in IBM Kubernetes Services, Red Hat OpenShift 4.x, IBM Code Engine, Azure Kubernetes Services.

In Visual Studio update your Dockerfile with the following code.

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base

RUN sed -i ‘s/MinProtocol = TLSv1.2/MinProtocol = TLSv1/g’ /etc/ssl/openssl.cnf

RUN adduser \
— disabled-password \
— home /app \
— gecos ‘’ app \
&& chown -R app /app
USER app

WORKDIR /app

COPY ./publish .

ENV DOTNET_RUNNING_IN_CONTAINER=true \
ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT [“dotnet”, “newbankvirtualassistant.dll”]

At this point we can build the solution and test with F5. Select newbankvirtualassistant for execution runtime.

Test application with F5

You should have your application running on localhost port:5001

Web Application running on local environment

The next step is to package the required artifacts for the docker image in the publish folder as specified in the dockerfile from above. To do this open a command line window or powershell and go to the root folder of the project. In my case is C:\ibmcloud\labs\newbankvirtualassistant. Execute the following command:

dotnet publish -o ./publish

At this point we have a new folder added to the project and we need to synch it to github as well.

In Solution Explorer right click on the new folder “Publish”, select Git and Add ignored File to Source Control

Using the new integrated Git features in Visual Studio synch and push the publish folder to your github repository.

Integrated Git functions in Visual Studio

Click on Commit and Push to publish the changes to Github.

Publish folder in github repository

We are ready to create the docker image. Execute the following commands in a Command Prompt or PowerShell window.

  1. docker login
  2. docker build -t username/newbankvirtualassistant:latest .
  3. docker push username/newbankvirtualassistant:latest

At this point verify on dockerhub that your image has been built and published.

Docker image published to dockerhub

A quick recap so far.

  1. We have created the Web Application in Visual Studio with .NET Core
  2. We have created our git repository with the code on github
  3. We have created our docker image and published on dockerhub

We are ready for the next step: Deployment the app in IBM Code Engine….and any other Kubernetes service indeed!

Deploy the application on IBM Code Engine service

IBM Code Engine is a public IBM Cloud Service currently in beta which allows you to run containerized applications, batch jobs, serverless functions on a pay as you go billing mode. One great advantage of IBM Code Engine is that you are abstracted from any deployment file and kubernetes details. And as mentioned before you can deploy an application from the source code in your git repository as I will show you in the next lines.

We need to provision an instance of IBM Code Engine. Navigate to the following link on IBM Cloud to create your instance of Code Engine (“https://cloud.ibm.com/codeengine/overview”).

Click on Start creating

IBM Code Engine welcome screen

In this case we will create an Application project. Select application and enter the application name. In my case is newbankvirtualassistant. Click on Create Project.

Enter the project name and select the resource group.

Click on Create.

As mentioned before Code Engine in the same a Red Hat OpenShift allows to deploy an application from a container image or from the source code. Let’s try with the container image option. Enter the name of your image on dockerhub (docker.io/username/imagename:tag). Then click on Deploy.

You could also specify the computing resources to your application instance, timeouts, maximum requests per second and scaling.

Click on Deploy

After a few seconds your application should be ready for testing. IBM Code Engine provide the application URL on the top right of the screen.

IBM Code Engine deployed application and URL

Click in Application URL. You should see your .NET Core Web Application running on IBM Code Engine….and pay as you go!

.NET Core Web Application running on IBM Code Engine

Wrap up and next steps

We finished the first part of our Virtual Assistant project. We created the web site that will host the assistant using .NET Core and IBM Code Engine. In the next article we will create the basic Virtual Assistant and integrate it with the web site.

See you in the next article “Develop the Virtual Assistant with IBM Watson Assistant and integrate it with the web site (Part 2)”

Jean Paul Tabja

--

--

Jean Paul Tabja

Software Solutions Architect with focus on Cloud and AI technologies