Develop a Virtual Assistant with IBM Watson and .NET Core on IBM Cloud Code Engine-Part 1
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:
- Develop and deploy the virtual assistant web site with .NET Core and IBM Code Engine (Part 1)
- Develop the Virtual Assistant with IBM Watson Assistant and integrate it with the web site (Part 2)
- Add search capabilities to the Virtual Assistant using IBM Watson Discovery (Part 3)
- Develop a serverless function with .NET Core and IBM Functions to query the account balance from the virtual assistant (Part 4)
Prerequisites
In order to complete the examples provided in this series you will need the following:
- An IBM Cloud account
- A GitHub account
- A Dockerhub account
- Docker Desktop (I use Docker Desktop for Windows)
- 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:
- Create the Web Application with Visual Studio 2019 and .NET Core
- Integrate the source code with GitHub
- Generate the docker image and publish it to Dockerhub
- 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
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.
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.
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
In the next screen enter your github.com account details and project information.
Verify the creation of the 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.
You should have your application running on localhost port:5001
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.
Click on Commit and Push to publish the changes to Github.
We are ready to create the docker image. Execute the following commands in a Command Prompt or PowerShell window.
- docker login
- docker build -t username/newbankvirtualassistant:latest .
- docker push username/newbankvirtualassistant:latest
At this point verify on dockerhub that your image has been built and published.
A quick recap so far.
- We have created the Web Application in Visual Studio with .NET Core
- We have created our git repository with the code on github
- 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
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.
Click in Application URL. You should see your .NET Core Web Application running on IBM Code Engine….and pay as you go!
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)”