antonyvorontsov / RabbitMQ.Client.Core.DependencyInjection

.Net Core library-wrapper of RabbitMQ.Client for Dependency Injection.
MIT License
111 stars 36 forks source link

RabbitMQ User Password as Environment variable #2

Closed skashif closed 5 years ago

skashif commented 5 years ago

Hi,

Thank you for the library.

I was hoping to get some help in understanding, how to use this library to pass the RabbitMQ user password as an environment variable rather than from appsettings, is it possible? Let me know.

Thanks.

antonyvorontsov commented 5 years ago

Hello!

Yup, you could not configure RabbitMQ client connection in other way than passing IConfiguration from appsettings to the extension method in the latest version. But I have just added another way in new version of the library. So, lets begin with an explanation.

Here is a new method with the signature:

public static IServiceCollection AddRabbitMqClient(this IServiceCollection services, RabbitMqClientOptions configuration);

So you can manually pass configuration as an instance of the options class.

static void ConfigureServices(IServiceCollection services)
{
    var options = new RabbitMqClientOptions
    {
        UserName = "user",
        Password = "password"
    };
    services.AddRabbitMqClient(options);
}

And nothing stopping us to use environment variables like this:

static void ConfigureServices(IServiceCollection services)
{
    var options = new RabbitMqClientOptions
    {
        UserName = Environment.GetEnvironmentVariable("RabbitMQ_User_Environment_Variable"),
        Password = Environment.GetEnvironmentVariable("RabbitMQ_Password_Environment_Variable")
    };
    services.AddRabbitMqClient(options);
}

I have also updated Examples.Producer project and changed the way of configuring RabbitMQ. Now all the parts configured manually. You can dive into that project on your own and use it as a sample.

Changes will appear in version 1.3.2, just download latest package and try it out.

I hope it helps! My best regards.

skashif commented 5 years ago

Hi Antony,

Thank you for a prompt reply!

I have updated the library and configured the client setup in the following manner, just to keep some settings in the appsettings file -

image

Works great! Thanks again.

Regards, Kashif