Igor-Pchelko / Igor-Pchelko.github.io

0 stars 0 forks source link

Read Parameters from AWS SMM in .Net Core 3 Application #7

Open Igor-Pchelko opened 5 years ago

Igor-Pchelko commented 5 years ago

https://pcholko.com/posts/2019-08-26/read-parameters-from-aws-smm-in-net-core-app/

AWS

Since Amazon and Microsoft are competitors in cloud providers market, there are always nuances when you try to integrate pieces from different tech stacks. Let’s dive into how to read AWS SMM parameters in .Net Core 3 application.

rushikeshsane95 commented 4 years ago

Hi! Thanks for the great article. I'm trying to do something similar. I have a json string as parameter value in param store, but when I try to fetch the value, I cannot get it as key-value pair. I am getting it as a string an need to deserialize it later in my code. Is there any configuration way in which I can get value as json? I see you have json value too, not sure how is it getting bound to your DatabaseSettings model.

Thanks again:-)

Igor-Pchelko commented 4 years ago

Hi Rushikesh,

AWS ParameterProcessor returns value as string. string GetValue(Parameter parameter, string path)

Unfortunately it's not possible to deserialise parameters automatically, you have to process them manually later on.

As workaround, you can create an service which receive IOptions<DatabaseSettingsAsJsonString> as argument for constructor. And provides method which returns deserialised settings DatabaseSettings.

public class DatabaseSettingsAsJson
{
    public string Json { get; set; }
}

public class DatabaseSettingsProvider
{
    private readonly DatabaseSettingsAsJson _databaseSettingsAsJson;

    public DatabaseSettingsProvider(IOptions<DatabaseSettingsAsJson> databaseSettingsAsJson)
    {
        _databaseSettingsAsJson = databaseSettingsAsJson.Value;
    }

    public DatabaseSettings GetDatabaseSettings()
    {
        return JsonConvert.DeserializeObject<DatabaseSettings>(_databaseSettingsAsJson.Json);
    }
}
rushikeshsane95 commented 4 years ago

That looks plausible. Just another question, where do you invoke DatabaseSettingsProvider. GetDatabaseSettings() method?

Igor-Pchelko commented 4 years ago

Aforementioned pice of code is not complete solution. To complete it you need to create interface IDatabaseSettingsProvider fromDatabaseSettingsProvider `. Then you need to register service. And inject it into the required pice of code. Checkout more in microsoft documentation: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0