Strumenta / SmartReader

SmartReader is a library to extract the main content of a web page, based on a port of the Readability library by Mozilla
https://smartreader.inre.me
Apache License 2.0
160 stars 36 forks source link

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'SocketsHttpHandler'. #22

Closed MaratPavlov closed 4 years ago

MaratPavlov commented 4 years ago

.NET Core 3.1 Smart Reader 0.7.3

foreach(var url in urls){ var tmpArticle = Reader.ParseArticle(url); .... }

The first iteration is successful, and the second has an error "System.ObjectDisposedException: Cannot access a disposed object. Object name: 'SocketsHttpHandler'. at System.Net.Http.SocketsHttpHandler.CheckDisposed() at System.Net.Http.SocketsHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at SmartReader.Reader.GetStreamAsync(Uri resource) at SmartReader.Reader.ParseArticle(String uri, String userAgent)

gabriele-tomassetti commented 4 years ago

Thanks for the bug report. I will look into it.

MaratPavlov commented 4 years ago

There is in asp.net core, not in console app. You can try it in the Controller: Reader.ParseArticle("https://blabla.com"); Reader.ParseArticle("https://blabla.com"); // throws an error

gabriele-tomassetti commented 4 years ago

I found the reason for the bug. You are not supposed to dispose HttpClient unless you use a setting to not dispose the inner handler. However, given what is said here I am starting think that the underlying issue is that probably we should not allow to change user agent if the static methods are used.

For now, I will release a new version to fix this bug. Let me know if the new version 0.7.4 will fix this for you. It will take a while to appear on nuget.

MaratPavlov commented 4 years ago

Yes, your fix helped, thank you!