Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

asp.net core - Http Client registration based on http header received from a middleware

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

question from:https://stackoverflow.com/questions/65946443/http-client-registration-based-on-http-header-received-from-a-middleware

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...