Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

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