Archive for the ‘.Net’ category

How to find the caller from a callee method in .NET

November 10th, 2009

In .Net, how do you find from a method, who the caller of that method is?

MethodA <————-MethodB, MethodC, MethodD (All these methods call MethodA)


string callerName = new System.Diagnostics.StackTrace(false).GetFrame(1).GetMethod().Name;

Pass in ‘true’ to the StackTrace constructor if you need to know the Filename, line number, and column number. Isn’t that cool?

.NET Threading – Http Connection Limit and how to change it

November 10th, 2009

Accidentally sent the last email w/o finishing…
Here is the KB article from MSFT http://support.microsoft.com/kb/183110

So, basically all my threads were using the 2 available connections and hence the difference in response time.
Greg, thanks for pointing this out.

Yes with 2 processes I get 4 connections… You can set the DefaultConnectionLimit by setting

System.Net.ServicePointManager.DefaultConnectionLimit = 1000;

or through the registry. The KB article tell you that.

I read somewhere that production ASP.NET servers accessing a webservice must set this to 12 * #CPUs….

Subject: RE: Threading Question

So just to clarify, when you run it in two processes you get a total of 4 connections?

Are you able to change the DefaultConnectionLimit?

How would this affect an ASP.NET app?

OK. So, this has to do with System.Net.ServicePointManager.DefaultConnectionLimit. This value by default is 2. This means each process is limited to have 2 connections. All requests wll be queued on these 2 connections. This is done to comply with RFC2616 (http 1.1) which mandates a 2 connection limit.

Greg, this is also the case for WinInet.

The .NET thread pool allows only 25 threads. I need more than that. I even tried with a custom thread pool class but no luck.

The thing is it doesn’t matter how you launch the threads ( I tested this… ). If you have threads in the same executable vs in 2 executables, it takes longer to recieve the response. Something at the transport layer???

Any reason why you are not using the ThreadPool?

I have an executable (.NET), which can launch a user specified number of threads to send requests to a webservice. My goal is to measure the response time for each request. Now, when I launch 256 threads from this executable, I get response times of about 40 secs for each request. But, when I launch the 2 executables with 128 threads each I get response times around 25 secs. The threads are pretty independent of each other (they just run a function and return the response time) and don’t share any file handles.

CPU usage is never beyond 20%. Looks like the threads are contending for something else that is not so obvious.

Any clue about whats going on here?