3

I am dealing with quite annoying issue, and I could not find any solution for this.

I am calling WebRequest Connection, working under Unity C#;

IAsyncResult startingTheCall =  webRequest.BeginGetRequestStream(new      AsyncCallback(GetRequestStreamCallback), parameters);

It sends call to server running on Windows; All works fine. But, if server is not turned off, connection waits for the response for eternity. So solve this, I add timeout:

ThreadPool.RegisterWaitForSingleObject(startingTheCall.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallbackOfFirstStream), parameters, 10000, true);

Timeout works fine too; The problem is, if I ever trigger the time out (by shutting the server down), then enabled server again, BeginGetRequestStream will never reach server no matter what, until I restart the application.

I thought, that maybe on failure, I am not cleaning connections properly. So I did set up this cleaning routine inside timeout:

        ArrayList unbox = (ArrayList)state;
        HttpWebRequest request = (HttpWebRequest)unbox[1];
        IAsyncResult asynchronousResult = (IAsyncResult)unbox[6];
        Stream postStream = request.EndGetRequestStream(asynchronousResult);
        postStream.Close();
        request.Abort();

I abort the request, close the stream. Still, after 1st failure, the server will never sent responses or get the stream messages, until I restart the application. Like it is completely blocked.

Anyone ever had this behaviour?

pascx64
  • 904
  • 16
  • 31
user2426290
  • 382
  • 2
  • 14
  • What .NET version is this? These are super old obsolete APIs. – usr Aug 18 '15 at 15:02
  • Unity uses NET 2.0, has some functions from 3.5 partially; Is there some fresh solution I could look at? – user2426290 Aug 18 '15 at 19:35
  • Uh, .NET 2.0... How many of these HTTP calls will be pending at each given time at a maximum? A few? Why can't you use super simple synchronous IO? – usr Aug 18 '15 at 19:43
  • Yes, several of them might be pending at once, but this issues reproduced even for single connection query. Synchronous IO caused visible lags in application during problematic connection times, so I was asked by the client to switch to asynchronous. I am keeping track of all IAsyncResult in List, and in case of failure, I clean up every connection properly. Still, after the 1st failure, this stucking happens. If only I new, what is wrong. – user2426290 Aug 19 '15 at 23:24
  • How many of these HTTP calls will be pending at each given time at a maximum? You could switch to synchronous IO on threads which is much simpler. – usr Aug 20 '15 at 09:17

0 Answers0