DotNet and Mysql on Cloud Foundry Platform

Ravi Jagannathan
5 min readFeb 26, 2017

--

In this article we will push a basic DotNet app onto Cloud Foundry (CF) and bind it to a Mysql service. We will take a look at how the platform makes it easy to create and deploy apps faster with backing services {mysql here}.

So this is a DotNet classic codebase.. different than DotNet core.

For how they are different.. read this and look at the section Comparison with .NET Framework.

First, you will need a Cloud Foundry developer account which will allow’s you to provision a Mysql service.

Your instance of CF should have a Windows Diego cell where this app will get deployed.

Next, clone this repo https://github.com/reagul/DotNetMysqlPCFStarter

You will also need cloud foundry cli on you machine. Get it here.

Let’s login to our account with CF cli like this. Replace [ ] with your URL.

$ cf login -a api.[my-cloudfoundry].com
API endpoint: https://api.[my-cloudfoundry].com

Email> [my-email]

Password> [my-password]
Authenticating...
OK

Once logged into your CF account via CF cli, cd into the cloned repo

cd DotNetMysqlPCFStarter

The app has everything you need to use Mysql db service in the cloud, and all we are doing now is to inspect and understand how it uses it once it gets deployed to CF.

Let us look at the manifest file for this app. Manifest is a way to organize parameters needed by CF cli when pushing your code to CF.

---
applications:
- name: env
host: env-${random-word}
memory: 512m
stack: windows2012R2
buildpack: binary_buildpack

The name of the app is env and the host is env-{some random word}. The memory is set to 512 m and the stack is windows2012. We are using binary buildpack. The host param will define the url where it can be accessed.

Cd into ViewEnvironment and type

cf push

CF cli takes your app bits and pushes it to CF. Once it is completely pushed, PCF attempts to start the app. Along the way it creates a URL for the app which you can access on your browser.

Once the app starts in CF, you can access it’s URL and will see something similar to screen grab below. This sample app is reading the environment information accessible to it, and displaying it on home page. The important pieces are current .Net version, bound services { blank for now }, Instance Id { GUID }and instance index { 0 }. If we had scaled it to 2 instance, it would be display index { 0, 1}. So instances index start at 0. Once we bind Mysql service to it, it will start reading it and display more info in the bound services line.

Wait, what does kill btn do… if you press the red btn here, it will kill this instance and PCF will bring it up again automatically. This is resiliency the PCF platform provides out of the box to your app.

So far so good.. lets bind mysql service to it.

From the marketplace, select Mysql and select the 100mb plan and enter some name and select the app we pushed to PCF named “env” in Bind to app drop down.

Bind Mysql to app

Once we bind mysql, lets re-stage our app from cli. This will make the app recognize new environment vars coming from the mysql service its bound to.

cf restage env

Lets refresh the browser and you will see new details. I went ahead and pressed the Add Attendee and it inserted a record into our new Mysql db that we bound to this app. It displays the dummy record here for us to look at with the Environment vars detail. So at this point it is connected to our mysql backend service and able to insert into and read it back.

So what does the C# code that detects our bound mysql Db look like.. look at CurrentEnvironment.cs . It detects if it finds “p-mysql” in its env vars and then sets the connection strings accordingly { server, port , uid }.

Our app depends on driver dll’s which are defined in the packages.config with this line .

id=”MySql.Data” version=”6.9.8" targetFramework=”net45"

So if you clicked “Add Attendees” button again, it will add the same record into DB and fetch it back. Not really functional but gives you enough idea on how records are inserted into our backing service and read back.

As a developer, you are used to waiting for provisioning web server, databases and other backing services, almost like watching paint dry, while your code is ready but not tested yet. While you are waiting for deployment, you cannot test your app and know it work’s as intended especially with a large codebase which means lots of risk to take on by the Dev teams.

Cloud foundry platform frees developer’s to focus on writing code and allows them to iterate on it, while providing necessary services within the platform. So a developer can push code in small increments and test it almost immediately. This also has the intended effect of pushing iterations faster into test phase, so the QA and Dev teams are almost working in tandem.

For the developer, Cloud Foundry is a Door to productivity. The platform abstracts a lot of details, like logging, security, metrics, resiliency, elastic capacity, and all developer’s have to do is to write and push code, bind into backing services and keep refining their code base.

The same way we used mysql, we could have used Redis, Gemfire and many other services all available through the marketplace.

As a developer you are not waiting for resources anymore and are fee to iterate on code faster. The platform opens up developer agility.

You can also integrate into custom services that you have created and offer it inside the marketplace for others to consume. Thus you can build on reusable services and modular code almost from get-go.

--

--