Search code examples
c#.netvisual-studio-2008iis-7httpwebrequest

sending Http request error (Http 503) in C#


I am using VSTS 2008 + C# + .Net 3.5 to develop a console application and I send request to another server (IIS 7.0 on Windows Server 2008). I find when the # of request threads are big, the IIS 7.0 will response with http 503 error. Any ideas what is wrong and how to tune to make IIS 7.0 serve more requests?

Here is my code,

class Program
{
    private static int ClientCount = 100;
    private static string TargetURL = "http://labtest/abc.wmv";
    private static int Timeout = 200;

    static void PerformanceWorker()
    {
        Stream dataStream = null;
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        StreamReader reader = null;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(TargetURL);
            request.Timeout = Timeout * 1000;
            request.Proxy = null;
            response = (HttpWebResponse)request.GetResponse();
            dataStream = response.GetResponseStream();
            reader = new StreamReader(dataStream);

            // 1 M at one time
            char[] c = new char[1000 * 10];

            while (reader.Read(c, 0, c.Length) > 0)
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
        }
        finally
        {
            if (null != reader)
            {
                reader.Close();
            }
            if (null != dataStream)
            {
                dataStream.Close();
            }
            if (null != response)
            {
                response.Close();
            }
        }
    }

    static void Main(string[] args)
    {
        Thread[] workers = new Thread[ClientCount];
        for (int i = 0; i < ClientCount; i++)
        {
            workers[i] = new Thread((new ThreadStart(PerformanceWorker)));
        }

        for (int i = 0; i < ClientCount; i++)
        {
            workers[i].Start();
        }

        for (int i = 0; i < ClientCount; i++)
        {
            workers[i].Join();
        }           

        return;
    }
}

Solution

  • The 503 Status Code stands for Service Unavailable and can be issued by servers when they are temporarily overloaded or too busy.

    Like Bhushan says many factors can affect the ability of a server to service requests.

    Look up IIS configuration on MSDN or maybe try asking your question on Server Fault (the server administrators equivalent of StackOverflow). You'll probably need to increase the amount of simultaneous requests permitted in a config file somewhere but even if you do you'll still be limited by the capabilities of your server.

    Also take a look at this MSDN forum post about increasing the maximum simultaneous requests to one host that HttpWebRequest can make: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007/
    By default this is limited to 2

    Edit

    Try looking at http://www.server7.iis.net/ConfigReference/system.applicationHost/sites/site/siteDefaults/limits for configuring server connection and bandwith limits