Skip to main content

2 posts tagged with "config"

View All Tags

· 2 min read
Ragavendra Nagraj

Have you ever been in a situation where you have spent hours together trying to debug as to why that database query is not awaited even after using the await for an async method.

Lately, I ran into an issue where the database call was passed and the method run finished with the undefined in the database response even after using await. After spending hours trying to fix it, one article took me back top using a Promise in javascript. Unfortunately this did not end the story as little was known to me that the two methods resolve and reject passed as params to the lambda call can be utilized to fetch the return response from that call like below.

export function getDataFromDb() {
var response = new Promise((resolve, reject) => {
....
// lines to fetch data from db
....

if(error) {
reject(error);
}

resolve(dbData);
})

return response
}

....
....
// make async call
var result = await getDataFromDb();
....

The resolve() call actually returns the response from the database call or similar.

Promise actually helps in awaiting the response from this or similar async calls and it also gives the benefit of reject the response, if an error was found.

· 2 min read
Ragavendra Nagraj

In this article I will give some tips on how one can work with configuration with ASP .Net apps. Almost all or any ASP .Net app anyway have this Microsoft.Extensions.Configuration.IConfiguration or simply put it as type IConfiguration.

There are usually one or two json files appsettings.json or or and the appsettings.Development.json. These contain settings for the app to use depending on the type of environment the instance is being run on. The first is for production and the later for development environment and more can de added as well.

Now let's say or appsettings.json is like below. Most of these config have provision for logging settings. say in production, the default might be Information meaning anything below that like Debug or Trace might not be logged. For development, the default might be Debug meaning almost all except might be Trace may not be logged which deserves another article for sure.

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"SomeApp": {
"Host": "192.168.a.l",
"Email": "abc@comp.co",
"Location":
{
"First": "Gatsby"
}
}

}

In case we need to retrieve the Host address of our SomeApp, then in the class which is loaded by the ASP .Net server let's say FirstService class, the IConfiguration can be dependency injected and the app's settings retrieved like below.

public class FirstService
{
public readonly IConfiguration _configuration;

public FirstService(IConfiguration configuration)
{
_configuration = configuration
}

public void RunApp()
{
Console.WriteLine("Host Config - " + _configuration["SomeApp:Host"]);
}
}

The _configuration["SomeApp:Host"] should give the address value which is "192.168.a.l", in this case. The _configuration["SomeApp:Host:Location:First"] should give the location value and so on.

Similarly additional configurations as json files can be added to be loaded in the Startup or the Program class as well. The fetching mechanism need not be a string key, it can be _configuration.Get\<P>() to bind the configuration to type P and be retrieved in a more strict type way as well.