Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

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 3
Anonymous
Not 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/

Anonymous
Not applicable

@Anonymous did you find a solution? I am interested in this as well. Thank you

V-lianl-msft
Community Support
Community 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.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.