Reputation: 49
I am having below situation : I am using web api core 3.1 framework (c#)
I am using typed httpclient registered in the startup . while registering typed client on startup, i am not able to provide the base URL and credentials because I am getting thru a service called configread and it reads the data from the header , which will be only available when one of our middle ware runs and sets it.
in my case base address, user id and passwords are coming from a service call but service calls depends on the request header (httpContext object). in the configureService methods , request context is not available.
Right now i am having trouble to get the httpClient from the startup.
Any guidance would be appreciated.
Update1:
I am adding a typed client as below
service.AddHttpClient<IAgencyServiceAgent,AgencyServiceAgent> (GetAgencyAgentHttpClient()).
ConfigurePrimaryHttpMessageHandler(GetAgencyHttpMessageHandler()) private Action<HttpClient> GetAgencyAgentHttpClient ()
{
var configUrl = Environment.GetEnvironmentVariable(ConfigConstants.CONFIGSERVICE URL)
return httpClient => {
// Here the base address is availble thru another service // which accept the data from the httpContext and based on the values / It pulls the base address and request header etc...
}
}
Update2:
I am having difficulty in setting this httpclient in the startup beacuse baseUrl and other info depends on the request object. For ex: i am reading a request header called DEV1 and passing it to another service , then it will return me the base address and credentials needed then after i can set the http client My questions are how do go about it . When httpClient configurations are depend on the httpContext object .. then how we should register and use it Thanks
Upvotes: 1
Views: 949
Reputation: 27997
According to your description, I suggest you could try to build ServiceProvider inside your GetAgencyAgentHttpClient
method and use GetService method to get which service you want to use.
More details, you could refer to below codes:
services.AddHttpClient("hello", c =>
{
//Build service provider
ServiceProvider serviceProvider = services.BuildServiceProvider();
//Get the ICurrentUserService
var currentUserService = serviceProvider.GetService<ICurrentUserService>();
//Use ICurrentUserService GetIPaddress method
var re= currentUserService.GetIPaddress();
c.BaseAddress = new Uri("http://localhost:5000");
}).AddTypedClient(c => Refit.RestService.For<IHelloClient>(c)); ;
Result:
If you want to check get the http header from current httprequest, you could try to get the httpcontext accessor in the service.
More details, you could refer to below codes:
services.AddHttpClient("hello", c =>
{
ServiceProvider serviceProvider = services.BuildServiceProvider();
//var currentUserService = serviceProvider.GetService<ICurrentUserService>();
//var re= currentUserService.GetIPaddress();
var httpcontext = serviceProvider.GetService<IHttpContextAccessor>();
var re = httpcontext.HttpContext.Request.Headers.ToList();
c.BaseAddress = new Uri("http://localhost:5000");
}).AddTypedClient(c => Refit.RestService.For<IHelloClient>(c));
Result:
Upvotes: 0