Git Product home page Git Product logo

Comments (2)

derole1 avatar derole1 commented on August 18, 2024

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.

from embedio.

rdeago avatar rdeago commented on August 18, 2024

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:

  • WebApiModule parses the request, extracts parameter values and calls your controller method;
  • your controller method calls SendDataAsync(), thereby sending response headers;
  • your controller method returns;
  • WebApiModule calls the JSON response serializer to serialize your return value and send it as a response (that's the reason why WebApiModule even exists: to spare you from having to serialize data and call SendDataAsync);
  • the serializer tries to set the Content-Type response header, but headers have already been sent, so HttpListenerResponse throws an InvalidOperationException;
  • the exception is caught here by WebServerBase<>, that calls this method to generate an HTTP 500 response;
  • in the meantime, the client has received your response (the data you sent before returning from the controller method) and has reused the TCP connection to send another request;
  • after sending the 500 response, WebServerBase<> closes the HttpContext, which closes the HttpListenerResponse, which in turn closes the underlying connection;
  • whichever method handles the second request is bound to get an ObjectDisposedException as soon as it tries to send a response.

(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.

from embedio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.