I'm trying to learn API's as we have a business case for it. and for the most part I can read and Serialize/Deserialize the data from our Oracle database - HOWEVER, in order to connect to the database im hardcoding the connectionString variable at the top of the page e.g
con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=my.oracle.host)(PORT=9999)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=myOracleService)));User Id=XXXXXX;Password=XXXXXX; ");
Ive created the OracleSettingsDB.cs file as below
public class OracleDbSettings{
public string Host{get;set;}
public string Port{get;set;}
public string User{get;set;}
public string Password{get;set;}
public string Service{get;set;}
public string ConnectionString{
get
{
return $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={Host})(PORT={Port})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={Service})));User Id={User};Password={Password};";
}
}
}
}
and following on from that added the values to the appsettings.json
"AllowedHosts": "*",
"OracleDbSettings": {
"Host": "my.oracle.host",
"Port":"9999",
"User": "XXXXX",
"Service":"myOracleService",
"Password": "XXXXXX" //Yes I know I shouldnt store this here
}```
I added the connectionString into the Startup.cs
``` var connectionString = Configuration.GetSection(nameof(OracleDbSettings)).Get<OracleDbSettings>();
But what I dont now get is how i reference the connectionString within the Repository file so that I can call
using (OracleConnection con = new OracleConnection(ConnectionString)
Hopefully, someone will be able to point me in the right direction as all the videos Ive watched on Youtube or pages ive found just reference the hardcoded data source - which we dont want to do.
Thanks and Hopefully theres enough information there =]
CodePudding user response:
I'm afraid you need to bind the values in appsettings.json
file to your OracleDbSettings
model, and I find