Click here to Skip to main content
15,888,313 members
Articles / Programming Languages / XML

WCF Throttling

Rate me:
Please Sign up or sign in to vote.
4.91/5 (40 votes)
13 Feb 2009CPOL9 min read 160.5K   6.1K   71  
WCF thottling is about more than just the throttling options.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WCFClient
{
    class Program
    {
        public static int Threads;
        public static int DoneThreads;
        public static int ErrorThreads;
        
        static void Main(string[] args)
        {
            Threads = 20;
            DoneThreads = 0;
            SpawnThreads(Threads);
            Console.ReadLine();
        }

        public delegate bool ThreadDelegate();
        public static void SpawnThreads(int threads)
        {
            for (int i = 0; i < threads; i++)
            {
                ThreadDelegate del = new ThreadDelegate(CallProxy);
                IAsyncResult result = del.BeginInvoke(delegate(IAsyncResult r) { EndCall(del.EndInvoke(r)); }, null);
            }
        }

        public static bool CallProxy()
        {
            try
            {
                Console.WriteLine( new Proxy("ThrottleTCP").Ping());
                return true;
            }
            catch (Exception exc)
            {
                Console.WriteLine(-1);
                //Console.WriteLine(exc.Message);//If you want some debugging
                return false;
            }
        }

        public static void EndCall(bool result)
        {
            if (!result)
            {
                ErrorThreads++;
            }

            DoneThreads++;
            if (DoneThreads == Threads)
            {
                Console.WriteLine(string.Format("The {0} threads completed of which {1} failed.", DoneThreads, ErrorThreads));
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Chris is a full time software developer in London, UK.
He has a BSc in computer science and is busy taking courses in the MCTS stream.

Comments and Discussions