How appsettings.json object is referenced into dotnet core application, with an example.
Example: let say I have the functionality to download the files from AWS s3 bucket and have the appsettings.json file with an object name S3DownloadSettings,
File Name: appsettings.json and S3DownloadSettingsis the object in it.
"S3DownloadSettings": {
"AccessKeyId":"my key",
"SecretKeyValue": "My Secret to download file",
"S3BucketName":"my s3 bocket container name"
}
Secondly, we create a POCO class with the same fields similar JSON object to a map for dependency injection in the Startup.cs class.
//Below is the class:
public class DownloadSettings
{
public string AccessKeyId {get;set;}
public string SecretKeyValue {get;set;}
public string S3BucketName {get;set;}
}
Now we register the JSON file in startup and build it,
FileName: Startup.cs
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true, reloadOnChange: true); //
Configuration = builder.Build();
}
Add appsettng object in Configuration.GetSection method-->this method returns subsection with the specified key and it never returns null even if no matching found.
If the specified key is not found in JSON, then an empty Microsoft.Extension.Configuration.IconfigurationSection will be return. this line of code makes available these settings to the application at run time.
public IServiceProvider ConfigureServices(IServiceCollection services){
services.Configure< DownloadSettings>(Configuration.GetSection("S3DownloadSettings"));
}
Finally, in the application code level, we have to use IOptions extensions to retrieve the configured value.
Let's say I have a class to download a document from AWS S3 called S3DocumentFactory.
public class S3DocumentFactory
{
private readonly IOptions<DownloadSettings> _downloadSettings;
public S3DocumentFactory(IOptions <DownloadSettings> downloadSettings)
{
_downloadSettings = downloadSettings;
}
public void DownloadDocument(int documentId, string documentType)
{
//Get settings from configuration, the whole objective is to retrieve the json object in application.
string s3bucketName = _downloadSettings.Value.S3BucketName ;
string accessKeyId = _downloadSettings.Value.AccessKeyId;
string secretKeyValue = _downloadSettings.Value.SecretKeyValue;
string documentLocation = "dd/33/";
// download location
string destination = Path.GetTemPath() + documentLocation ;
//create s3 client and check for the file existence
using(IAmazonS3 client = new AmazonS3Client(accessKeyId, secretKeyValue, Amazon.RegionEndPoint.UsEast1))
{
using(var transferUtility = new TransferUtility(client))
{
var s3fileInfo = new S3FileInfo(client, s3bucketName , documentLocation);
//If file exists, download it and return to browser
if(s3FileInfo.Exists)
{
var downloadRequest = new TransferUtilityDownlooadRequest{
BucketName = s3BucketName,
Key = documentLocation,
FilePath = destination
};
await transferUtility.DownloadAsync(downloadRequest);
//Download code
byte[] fileBytes = System.IO.File.ReadAllBytes(destimation);
return File(fileBytes, "application/pdf");
}
else{
//Else upload document or log error or show message
}
}
}
}
Notes:
For AWS S3 references we need, Amazon.S3; Amazon.S3.IO; Amazon.S3.Transfer; and other namespaces.