Forum Discussion
Injecting HttpClient into PowerBIClient
Hi,
I'm using PowerBIClient in my ASP.NET 5 web application. Currently new instance of PowerBIClient (Microsoft.PowerBI.Api 3.22.0) is made for every request. I've noticed that internally PowerBIClient creates HttpClient and from what I know this is not the best practice. I try to find a way to inject my own HttpClient created with IHttpClientFactory. There is a constructor overload in PowerBIClient that takes HttpClientHandler. Unfortunately I'm not able to get it from IHttpClientFactory.
I'm experimenting with inheritance to access HttpClient property and override it with my own one. It works quite well but ServiceClient<> (which is part of PowerBIClient) creates HttpClient in its primary constructor and I need to dispose it first. Is there any better option?
internal class PowerBIClientWrapper : PowerBIClient
{
public PowerBIClientWrapper(
Uri baseUri,
ServiceClientCredentials credentials,
HttpClient httpClient) : base(baseUri, credentials)
{
HttpClient?.Dispose();
FirstMessageHandler = null;
HttpClientHandler = null;
HttpClient = httpClient;
}
}
3 Replies
- V-lianl-msftCommunity Support
Hi Anonymous ,
See if this blog helps:
https://blog.bennymichielsen.be/2017/11/09/powerbi-with-net-core/
Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. - AnonymousNot applicable
Anonymous did you find a solution? I am interested in this as well. Thank you
- AnonymousNot applicable
Anonymous, yes, I found two potential solutions for that. You can either override internal PowerBIClient's HttpClient property or register PowerBIClient as a singleton (with appropirate SocketsHttpHandler configuration). I described both approaches in details on my blog: https://brokul.dev/powerbiclient-and-socket-exhaustion-in-asp-net-core-app/