Fix System.ObjectDisposedException passed to WebSocket connection

I am trying to establish a connection between a web browser client and an ASP.NET server using WebSockets.

I make a set of queries of different sizes and with a few seconds elapsed time between each of them. The first three are transmitted correctly, but the exact one, nothing in particular from the other, closes the WebSocket connection, throws an exception on the server side.

The error message and stack trace of this exception are as follows:

FATAL ERROR: Unable to access the located object.
Object Name: "System.Web.WebSockets.AspNetWebSocket".
in System.Web.WebSockets.AspNetWebSocket.ThrowIfDisposed ()
in System.Web.WebSockets.AspNetWebSocket.SendAsyncImpl (ArraySegment 1 buffer, WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken cancelationToken, Boolean performValidation)
in System.Web.WebSockets.AspNetWebSocket.SendAsync (ArraySegment 1 buffer, WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken cancelationToken)
to [my code path is here ...]

This could be a threading issue because I use async methods everywhere from the functions that communicate with websites.

I know that an exception is thrown from this code (in socket.SendAsync ):

 public class SocketTranslater { private WebSocket socket; private JavaScriptSerializer serializer; // [...] private ArraySegment<byte> Encode(Object data) { string json = serializer.Serialize(data); return (new ArraySegment<byte>(Encoding.UTF8.GetBytes(json))); } public async Task Send(Object packet) { ArraySegment<byte> encoded = this.Encode(packet); await socket.SendAsync(encoded, WebSocketMessageType.Text, true, CancellationToken.None); } } 

A socket is created from another class:

 public class EventSender : IHttpHandler { private static List<SocketTranslater> socketTranslaters = new List<SocketTranslater>(); public void ProcessRequest(HttpContext context) { this.id = context.Request.UserHostName + ":" + context.Request.UserAgent; if (context.IsWebSocketRequest) { context.AcceptWebSocketRequest(ProcessSocket); } } private async Task ManageSocket(AspNetWebSocketContext context) { WebSocket socket = context.WebSocket; SocketTranslater translater = new SocketTranslater(socket); translaters.add(translater); while (true) { // Manage messages, using the other class to send responses. translater.Send(/* Any struct here */); } } 

Not good, my project is too big to fit all the code here.

Any idea of ​​the source of the error or additional information that you will need?

UPDATE:

After some additional tests, from time to time I don't have this exception. I always do the same, but the server seems random.

This makes my problem even weirder ...

+5
source share
1 answer

Finally, after several tests and interesting questions and answers from here (like this one ), I realized:

My problem was that I kept WebSockets in a dictionary associated with host names. Thus, when I first connected the client on my server, everything worked correctly. But if I refresh the page, the website was closed by the server (because it was not possible to use it again), and another was created.

But since I used the same key for both sockets, outdated and new, I tried to respond to a new client with a previous socket that was closed. (selected = closed for ASP.NET).

So, the only thing I needed to do was remove the socket from the list when the client disconnected (at the end of the ManageSocket ). And prevent the client from connecting to the same host name twice.

I did not mention the part where I connected the sockets with the host names, so I admit that you could not help me ... I'm sorry.

+1
source

All Articles