Groups | Blog | Home
all groups > dotnet framework > december 2006 >

dotnet framework : HttpListener BeginGetContext does not seem to handle more than 2 request simultaneously



Kunal
12/21/2006 2:41:15 PM
Hi Friends,

I'm trying to host a webservice that will receive/process multiple
client requests simultaneously. For this purpose, I wrote the following
code, but it does not seem to be handling more than two at a time. I
put in a few console prints and have also attached the output. Here it
goes -

public void serverStart() {

HttpListener _listener = new HttpListener();

_listener.Prefixes.Add(.......);

_listener.Start();

while (true)

ProcessRequest();

}

public void ProcessRequest()

{

Console.WriteLine("PR " + ++count); //count initialized to 0 in
beginning

IAsyncResult result = _listener.BeginGetContext(new AsyncCallback
ListenerCallback),this._listener);

result.AsyncWaitHandle.WaitOne();

}

protected void ListenerCallback(IAsyncResult result)

{

if (this._listener == null) return;

HttpListenerContext context = this._listener.EndGetContext(result);

Console.WriteLine("LC " + count);

this.ProcessRequest2(context);

}

public void ProcessRequest2(HttpListenerContext ctx)

{

Console.WriteLine("PR2 " + count);

string str = ctx.Request.HttpMethod;

HttpListenerWorkerRequest workerRequest =

new HttpListenerWorkerRequest(ctx,_virtualDir, _physicalDir,
_logCallback, _pxeb);

HttpRuntime.ProcessRequest(workerRequest);

}

The output this gives me looks like this -

PR 1
PR 2
LC 2
PR2 2
LC 2
PR2 3
PR 3
LC 3
PR2 4
PR 4
LC 4
PR2 5
PR 5
PR 6
LC 6
PR2 6
LC 6
PR2 7
PR 7
PR 8
LC 8
PR2 8
PR 9
LC 9
PR2 9
LC 9

Can someone point out what's happening ? The Microsoft documentation
does not say anything about the no. of connections this model can
handle or has a limit to. Any developers who have used this may please
enlighten me !

Thanks and Regards,

Kunal
Vadym Stetsyak
12/22/2006 12:00:00 AM
Hello, Kunal!


line "result.AsyncWaitHandle.WaitOne();"
blocks in ProcessRequest method thus breaking the async execution.

Try to comment that line an repeat your tests.


You wrote on 21 Dec 2006 14:41:15 -0800:

K> Hi Friends,

K> I'm trying to host a webservice that will receive/process multiple
K> client requests simultaneously. For this purpose, I wrote the
K> following
K> code, but it does not seem to be handling more than two at a time. I
K> put in a few console prints and have also attached the output. Here
K> it
K> goes -

K> public void serverStart() {

K> HttpListener _listener = new HttpListener();

K> _listener.Prefixes.Add(.......);

K> _listener.Start();

K> while (true)

K> ProcessRequest();

K> }

K> public void ProcessRequest()

K> {

K> Console.WriteLine("PR " + ++count); //count initialized to 0 in
K> beginning

K> IAsyncResult result = _listener.BeginGetContext(new AsyncCallback
K> ListenerCallback),this._listener);

K> result.AsyncWaitHandle.WaitOne();

K> }

K> protected void ListenerCallback(IAsyncResult result)

K> {

K> if (this._listener == null) return;

K> HttpListenerContext context =
K> this._listener.EndGetContext(result);

K> Console.WriteLine("LC " + count);

K> this.ProcessRequest2(context);

K> }

K> public void ProcessRequest2(HttpListenerContext ctx)

K> {

K> Console.WriteLine("PR2 " + count);

K> string str = ctx.Request.HttpMethod;

K> HttpListenerWorkerRequest workerRequest =

K> new HttpListenerWorkerRequest(ctx,_virtualDir, _physicalDir,
K> _logCallback, _pxeb);

K> HttpRuntime.ProcessRequest(workerRequest);

K> }

K> The output this gives me looks like this -

K> PR 1
K> PR 2
K> LC 2
K> PR2 2
K> LC 2
K> PR2 3
K> PR 3
K> LC 3
K> PR2 4
K> PR 4
K> LC 4
K> PR2 5
K> PR 5
K> PR 6
K> LC 6
K> PR2 6
K> LC 6
K> PR2 7
K> PR 7
K> PR 8
K> LC 8
K> PR2 8
K> PR 9
K> LC 9
K> PR2 9
K> LC 9

K> Can someone point out what's happening ? The Microsoft documentation
K> does not say anything about the no. of connections this model can
K> handle or has a limit to. Any developers who have used this may
K> please
K> enlighten me !

K> Thanks and Regards,

K> Kunal


With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com

AddThis Social Bookmark Button