Open gsino opened 3 years ago
I found a few problems with this issue:
@gsino can you add some example use cases or code samples to this issue to elaborate how this can be used with Firebase? Wouldn't you be able to achieve the same result with something like https://github.com/firebase/firebase-admin-dotnet/blob/master/FirebaseAdmin/FirebaseAdmin.Tests/MockHttpClientFactory.cs?
Wouldn't you be able to achieve the same result with something like https://github.com/firebase/firebase-admin-dotnet/blob/master/FirebaseAdmin/FirebaseAdmin.Tests/MockHttpClientFactory.cs?
Half Yes.
In fact, I can get any HttpMessageHandler
by injecting System.Net.Http.IHttpMessageHandlerFactory
.
public class MyHttpClientFactory : Google.Apis.Http.HttpClientFactory
{
private readonly System.Net.Http.IHttpMessageHandlerFactory _httpMessageHandlerFactory;
public MyHttpClientFactory (System.Net.Http.IHttpMessageHandlerFactory httpMessageHandlerFactory)
{
_httpMessageHandlerFactory = httpMessageHandlerFactory;
}
protected override HttpMessageHandler CreateHandler(Google.Apis.Http.CreateHttpClientArgs args)
{
return _httpMessageHandlerFactory.CreateHandler("MyHttpClientFactory");
}
}
However, compared to Google.Apis.Http.HttpClientFactory
, it lacks the following two features:
For Google.Apis.Http.HttpClientFromMessageHandlerFactory
, both features are supported.
If the type of the AppOptions.HttpClientFactory
property is Google.Apis.Http.IHttpClientFactory
, you can set Google.Apis.HttpHttpClientFromMessageHandlerFactory
in addition to Google.Apis.Http.HttpClientFactory
.
public sealed class AppOptions
{
public IHttpClientFactory HttpClientFactory { get; set; }
}
var defaultHttpOption = new AppOptions
{
HttpClientFactory = new HttpClientFactory(),
}
var customHttpOption1 = new AppOptions
{
HttpClientFactory = new HttpHttpClientFromMessageHandlerFactory(...),
}
//or use DI Container
var customHttpOption2 = new AppOptions
{
HttpClientFactory = serviceProvider.GetService<IHttpClientFactory>(),
}
Fair enough. We can look into making this change in a future release (shouldn't be a breaking change as far as I can tell).
Thank you! :)
@hiranya911 Any updates on this? We'd really like to use .NET's IHttpClientFactory
to manage the underlying http connections for efficiency. Since this is already supported in other Google APIs, and the pattern is now the universal best-practice for all modern .NET applications, it would be great for the Firebase SDK to support it as well. Thanks!
Are there any updated on this. or any other methods of optimizing the HTTP client usage on FireBase Messaging?
Hello.
In the GoogleApi repository, an implementation of
Google.Apis.Http.IHttpClientFactory
usingMicrosoft.Extensions.Http.IHttpMessageHandlerFactory
is presented.https://github.com/googleapis/google-api-dotnet-client/issues/1756
https://github.com/googleapis/google-api-dotnet-client/blob/bfc9090dc2fae5b06a654328c4da7bc6b05b66f1/Src/Support/IntegrationTests/HttpClientFromMessageHandlerFactoryTests.cs#L38
To apply this method to Firebase as well, change the type of the
AppOptions.HttpClientFactory
property fromGoogle.Apis.Http.HttpClientFactory
toGoogle.Apis.Http.IHttpClientFactory
?