Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should HttpTileSource implement IDisposable? #125

Open
donid opened this issue Jun 16, 2020 · 3 comments
Open

Should HttpTileSource implement IDisposable? #125

donid opened this issue Jun 16, 2020 · 3 comments

Comments

@donid
Copy link
Contributor

donid commented Jun 16, 2020

I was searching if I could set the 'Referrer' Property of the HttpClient used by HttpTileSource, when I noticed that HttpClient is IDisposable (through HttpMessageInvoker) and HttpTileSource does not implement IDisposable. Doesn't this violate this rule?

@pauldendulk
Copy link
Contributor

Yes, that is correct. One way to resolve this is to add the IDisposable, but this will make everything else that is using it IDisposable. Perhaps there is another way where we keep the HttpClient more on the outside. This is also needed for setting properties like the referrer. Also it is advised these days to use a factory for HttpClient to allow reuse. So perhaps there should be some central factory that could be configured... not sure how to resolve this. Perhaps leave it like this for now.

It is not possible to set the referrer on the HttpClient within the the HttpTileSource but it is possible to pass in your own tile fetch method in which you can use your own HttpClient with the settings your choose. Here is an example for google maps setting the referrer:
https://github.com/BruTile/BruTile/blob/master/Samples/BruTile.Samples.Common/Samples/GoogleMapsSample.cs

@donid
Copy link
Contributor Author

donid commented Jun 17, 2020

I agree that having IDisposable on HttpTileSource is annoying, especially when providing a tileFetcher where HttpClient is not even used.

@pauldendulk
Copy link
Contributor

I was thinking about using a HttpClientFactory to avoid having to make HttpTileSource disposable. This page mentions this about the HttpClient:

Though this class implements IDisposable, declaring and instantiating it within a using statement is not preferred because when the HttpClient object gets disposed of, the underlying socket is not immediately released, which can lead to a socket exhaustion problem.

So, indeed it should not be disposed but we should use a factory instead.

Microsoft has:

  • HttpClientFactory in the Microsoft.AspNet.WebApi.Client nuget
  • IHttpClientFactory in the Microsoft.Extensions.Http nuget

I would not like to add this as a dependency. If it is not in the standard dotnet we could add something like this ourselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants