Open Closed

How to pass values from micro service to Module #8070


User avatar
0
castcrewit created
  • ABP Framework version: v8.3
  • UI Type: Angular
  • Database System: EF Core (SQL Serve)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

I am building a module and using that module in my microservices.

This module is calling a third party API which required an URL, username and password.

I am able to access the appseetings.json values in modules configured in microservices by this :

 context.Services.AddHttpClient("myttpClient", client =>
 {
     // Set the base address
     client.BaseAddress = new Uri(configuration.GetSection("ThirdPartyUrls:xxxx:BaseUrl").Value);
     // Set default request headers
     client.DefaultRequestHeaders.Add("User-Agent", "ddddd");
     client.DefaultRequestHeaders.Accept.Add(
         new MediaTypeWithQualityHeaderValue("application/json"));
     // Set timeout for requests
     client.Timeout = TimeSpan.FromSeconds(180);
     client.DefaultRequestHeaders.CacheControl = CacheControlHeaderValue.Parse("no-cache");
 })
 // Add the custom HTTP message handler
 .AddHttpMessageHandler<myttpClientHandler>();    
 

However I don't like this as it's dependent on the Appsettin.json of module to retrieve the value , rather i would like to use options to pass the value to the modules.

I also have a TokenService in the module which is expecting a usernaem and password to go and get the token from the third party.

So the question is where and how should I write in the module so that I can configure the module in the microservice, something like this :

 Configure<AbpRabbitMqOptions>(options =>
 {
     options.Connections.Default.UserName = configuration["RabbitMQ:Connections:Default:UserName"];
     options.Connections.Default.Password = configuration["RabbitMQ:Connections:Default:Password"];
     options.Connections.Default.HostName = configuration["RabbitMQ:Connections:Default:HostName"];
     options.Connections.Default.Port = 5671;
       options.Connections.Default.VirtualHost = configuration["RabbitMQ:Connections:Default:VirtualHost"];

 });

The gist of the question is how do i write the module so that I can configure that in the microservices.


1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    This module is calling a third party API which required an URL, username and password.

    Which layer is this class in the module? application? domain?

    You can add a MyOptions class in the same layer. and inject the IOptions<MyOptions> in your service to get value. Then, configure this option from appsettings.json or somewhere else in the microservice project.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09