C# problem with HttpListener -


i wrote windows service using httplistener process requests points asynchronously.

it works fine, runs problem requires restarting service or server fix. declared listener object with:

public httplistener pointslistener = new httplistener(); 

here code of method, starting listening. i'm calling onstart method of service:

    public string listenerstart()     {         try         {             if (!pointslistener.islistening)             {                 pointslistener.prefixes.add(string.concat("http://*:", points_port, "/"));                 pointslistener.start();                 pointslistener.begingetcontext(pointprocessrequest, pointslistener);                  logwriter("http listener activated on port " + points_port);                 return "listener started";             }             else             {                 return "listener started!";             }          }         catch (exception err)         {             logwriter("error in listenerstart \r\n" + err.tostring());             return ("error: " + err.message);         }     } 

here methods process requests:

    private void pointprocessrequest(iasyncresult result)     {         httplistener listener = (httplistener)result.asyncstate;         httplistenercontext context = listener.endgetcontext(result);         httplistenerrequest request = context.request;         httplistenerresponse response = context.response;         response.keepalive = false;         system.io.stream output = response.outputstream;          try         {             //declaring variable responce             string responsestring = "<html>my response: request not allowed server protocol</html>";               // commands , actions set responcestring              byte[] buffer = encoding.utf8.getbytes(responsestring);             response.contentlength64 = buffer.length;             output.write(buffer, 0, buffer.length);         }         catch (exception err)         {             logwriter("error in pointprocessrequest: \r\n" + err.tostring());         }                 {             try             {                 output.flush();                 output.close();                 response.close();             }             catch (exception err)             {                 logwriter("error in pointprocessrequest closing output stream: \r\n" + err.tostring());             }                         {                 pointslistener.begingetcontext(pointprocessrequest, pointslistener);             }         }     } 

it working of time, following error appears in log:

error in pointprocessrequest:  system.net.httplistenerexception: specified network name no longer available    в system.net.httpresponsestream.write(byte[] buffer, int32 offset, int32 size)    в ark_dealer.ark.pointprocessrequest(iasyncresult result)  [26.01.2011 9:00:54] error in pointprocessrequest closing output stream:  system.invalidoperationexception: cannot close stream until bytes written.    в system.net.httpresponsestream.dispose(boolean disposing)    в system.io.stream.close()    в ark_dealer.ark.pointprocessrequest(iasyncresult result) 

i think problem appears when point sends request server before receiving answer looses connection.

how can prevent exception being thrown? response object disposed of automatically? how can solve problem?

i'm using httplistener in production , i've found nicest way resolve issue, without adding whole bunch of try/catch blocks nothing communicate logic of code @ hand; quite set listener.ignorewriteexceptions = true; and, voilà no more write exceptions!


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -