- Within the Fortune Teller service app, go to the /Services/Startup.cs file and update the following: [Add dependencies]
using Steeltoe.Extensions.Configuration;
using Steeltoe.CloudFoundry.Connector.MySql.EFCore;
[Update the builder configuration to include CloudFoundry service]
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
--> .AddCloudFoundry()
.AddEnvironmentVariables();
[Remove the below code]
//Using MySql Datastore
string connString = "";
try{
dynamic vcap = JObject.Parse(Environment.GetEnvironmentVariable("VCAP_SERVICES"));
connString = String.Format("Server={0};port={1};Database={2};uid={3};pwd={4};",
vcap["p-mysql"][0].credentials.hostname,
vcap["p-mysql"][0].credentials.port,
vcap["p-mysql"][0].credentials.name,
vcap["p-mysql"][0].credentials.username,
vcap["p-mysql"][0].credentials.password);
}catch(Exception ex){
Console.Error.WriteLine("Error retrieving database connection string from environment variables");
Console.Error.WriteLine(ex);
Environment.Exit(1);
}
services.AddDbContext<FortuneTellerContext>(opt => opt.UseMySQL(connString));
[Add the new new MySql connection]
services.AddDbContext<FortuneTellerContext>(opt => opt.UseMySql(Configuration));
- Save your changes
- The Startup.cs file should look like this:
- Open a Terminal (or command prompt) and navigate to the app directory.
> cd ~/DotNet-Core-SteelToe-Workshop/FortuneTeller
- Confirm the API target is set
> cf target
API endpoint: <PROVIDED_BY_INSTRUCTOR>
User: <student-x>
Org: Vantage
Space: <student-x>
- Push the app
> cf push
- The cf cli will provide feedback about each step it takes to create the App Container and deploy
- In AppManager click the '' space in the left box
- The column labeled 'Route' will offer a link to execute the app. Click the route for the 'fortune-teller-www' app.
- A new tab will be created, loading the FortuneTeller app web site
![alt text][fortuneTellerWebSite]
- How easy was that?! The app is using the same MySql database but not more connection string.
| Home |_______________________________________________________________________________| Next Lab |