|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Aynchronous Web Request Handling - Help RequiredI'm trying to modify my synchronous model to asynchronous one and I need help from experts out here.I'm hosting a web service (asmx) using HTTP.SYS and HttpListener. This is most similar to the example code given at http://msdn.microsoft.com/msdnmag/issues/04/12/ServiceStation/ and works well. I need someone to guide me on what changes I need to do to make this listener accept and process multiple requests simultaneously. I have tried to do this but failed. Any help will be highly appreciated. Thanks, Kunal Here are my code snippets - namespace HttpListenerLibrary { . . Extra supporting stuff here.... . public class HttpListenerController { private Thread _pump; private bool _listening = false; private string _virtualDir; private string _physicalDir; private string[] _prefixes; private HttpListenerWrapper _listener; public event LogEventHandler _LogEvent; public LogCallback _logCallback; internal PersistentXmlEventBuffer _pxeb; public HttpListenerController(string[] prefixes, string vdir, string pdir, LogEventHandler leh) { _prefixes = prefixes; _virtualDir = vdir; _physicalDir = pdir; _LogEvent += leh; _logCallback = new LogCallback(LogThis); _pxeb = new PersistentXmlEventBuffer(); } public void Start() { _listening = true; _pump = new Thread(new ThreadStart(Pump)); _pump.Start(); } public void Stop() { // some stuff } private void Pump() { try { _listener = (HttpListenerWrapper)ApplicationHost.CreateApplicationHost( typeof(HttpListenerWrapper), _virtualDir, _physicalDir); _listener.Configure(_prefixes, _virtualDir, _physicalDir, _LogEvent, _pxeb); _listener.Start(); while (_listening) _listener.ProcessRequest(); } catch (Exception ex) { // some stuff } } } public class HttpListenerWrapper : MarshalByRefObject { private HttpListener _listener; private string _virtualDir; private string _physicalDir; public event LogEventHandler _LogEvent; public LogCallback _logCallback; internal PersistentXmlEventBuffer _pxeb; public void Configure(string[] prefixes, string vdir, string pdir, LogEventHandler leh, PersistentXmlEventBuffer pxeb) { _virtualDir = vdir; _physicalDir = pdir; _listener = new HttpListener(); _logCallback = new LogCallback(LogThis); _pxeb = pxeb; foreach (string prefix in prefixes) _listener.Prefixes.Add(prefix); } public void LogThis(string msg, ColorCode color) { //some stuff } public void Start() { _listener.Start(); //some more stuff } public void Stop() { _listener.Stop(); } public void ProcessRequest() { try { HttpListenerContext ctx = _listener.GetContext(); string str = ctx.Request.HttpMethod; if (str.Equals("OPTIONS")) { Console.WriteLine("HTTP Options"); HttpListenerWorkerRequest workerRequest = new HttpListenerWorkerRequest( ctx,_virtualDir, _physicalDir, _logCallback, _pxeb); workerRequest.SendStatus(200, "OK"); workerRequest.EndOfRequest(); } else { HttpListenerWorkerRequest workerRequest = new HttpListenerWorkerRequest( ctx,_virtualDir, _physicalDir, _logCallback, _pxeb); HttpRuntime.ProcessRequest(workerRequest); } } catch (XmlException xE) { LogThis(xE.Message, ColorCode.RcvColor); } } } public class HttpListenerWorkerRequest : HttpWorkerRequest { private HttpListenerContext _context; private string _virtualDir; private string _physicalDir; public LogCallback _logCallback; PersistentXmlEventBuffer _pxeb; public HttpListenerWorkerRequest(HttpListenerContext context, string vdir, string pdir, LogCallback lcb, PersistentXmlEventBuffer pxeb) { _context = context; _virtualDir = vdir; _physicalDir = pdir; _logCallback = lcb; _pxeb = pxeb; } // required overrides (abstract) public override void EndOfRequest() { _context.Response.OutputStream.Close(); _context.Response.Close(); } public override void SendStatus(int statusCode, string statusDescription) { _context.Response.StatusCode = statusCode; _context.Response.StatusDescription = statusDescription; } // other overrided functions follow..... } }
Other interesting topics
problem in data logging from within HttpListener
Handling multiple web requests parallely Visual Studio 2005 weird behavior Visual Studio Sp1 for Professional version. How to detect a handle leak with a GC Reflector not running under correct framework version Rule Engine VS Failed to publish one button (worked well before) Running installed application asks for MSI typecast array of object to bindinglist of object |
|||||||||||||||||||||||