Open derole1 opened 1 year ago
One observation ive made, if I use the JsonData attribute in the route function parameters to get the json data, and set the response object as the return type and return it instead of using HttpContext.SendDataAsync(), this issue doesnt occur and everything works smoothly. However unfortunately there is no such equivalent for binary data which is unfortunate as that also suffers from the same issue.
My theroy is that when theres multiple requests happening asynchronously, One request finishes and closes the HttpContext whilst the rest are still using it, which causes the ObjectDisposedExceptions. Hope this workaround helps anyone with the same issue in the meantime.
Hello @derole1, thanks for using EmbedIO!
I agree that there's probably a bug somewhere in EmbedIO's implementation of HttpListener, because a response clearly gets closed ahead of its due time.
That having been said, the root cause of the exception you observe is your use of HttpContext.SendDataAsync(), which must never be called from a WebApiModule.
Here's what happens:
(I'm not 100% sure about all the fine details, but I think I got it right.)
I don't even know whether we can fix this, let alone how. It would require some serious reworking of the Net.Internal workspace, but we have to keep it compatible with Microsoft's HttpListener, so our options are quite limited.
As you have guessed, the workaround is... to not cause exceptions in the first place, as silly as it sounds.
Unlike the weird behavior of HttpConnection, EmbedIO failing because you called SendDataAsync from a Web API controller method is not a bug: it's you asking for trouble. 😁 Think of WebApiModule as a higher-level API over the whole request / response stuff: instead of receive / deserialize / compute / serialize / send, you get to do the fun part (compute) and it takes care of everything else.
Describe the bug When the server encounters multiple requests at once, or a lot of traffic, WebApiController encounters an ObjectDisposedException.
To Reproduce
Expected behavior HttpContext objects should not get disposed of by garbage collection whilst processing requests.
Exception
Desktop:
Additional context The WebUI that makes the JSON requests uses fetch, but I dont think this has any relevance to the issue as the same problem occurs when sending requests with Telerik Fiddler.