locked
Singleton Proxy Classes RRS feed

  • Question

  • Hi,

    I'm using lot of WCF services in my application. Currently the proxy class is created newly every time when i'm invoking a service.
    Instead of that shall I use singleton proxy classes?
    Can anyone suggest a design pattern we can apply for managing proxies.

    Thanks & Regards
    Dnana
    Tuesday, January 12, 2010 12:40 PM

Answers

All replies

  • That's a good question. Wenlong has written the following good articles in his blog that will help you in your design, with best practices for using proxy classes:

    http://blogs.msdn.com/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx

    http://blogs.msdn.com/wenlong/archive/2007/10/26/best-practice-always-open-wcf-client-proxy-explicitly-when-it-is-shared.aspx

    I hope these articles help you.
    Tuesday, January 12, 2010 5:12 PM
    Moderator
  • Hi,

    That was helpful. 

    I have done some testing to check the performance on creating proxies every time.

    Here is the code.    
    namespace ProxyConsole
    {
        class Program
        {
            static DateTime initTime = DateTime.Now;
            static DateTime startTime = DateTime.Now;
            [STAThread]
            static void Main(string[] args)
            {
                for (int i = 0; i < 10; i++)
                {
                    initTime = DateTime.Now;
                    startTime = DateTime.Now;
                    ServiceClient client = new ServiceClient();
                    client.Open();
                    Console.WriteLine(client.DoWork("Washing Clothes"));
                    client.Close();
                    Console.Write("The total time taken: {0}", DateTime.Now - initTime);
                    Console.WriteLine();
                }
            }
        }
    }
    In the above code snippet I'm calculating the time taken for creating a proxy, opening, invoking the service methods and closing it for ten times. DoWork() is the service method just returns a string.

    Here are the results in the console window,

    I have done Washing Clothes
    The total time taken: 00:00:00.4687500
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0312500
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750
    I have done Washing Clothes
    The total time taken: 00:00:00.0468750

    From the above results I came to know that only for the first time more time is taken to create the proxy and open it.
    from the next times it is nearly same. So as mentioned in Wenlong blog the ChannelFactory is cached.
    I checked using the Web client, results are more or less same.

    Since already a caching is happening. I don't think why we need to cache the proxies like Cache["EmailProxy"] = new EmailClient() one more time right?

    More information will be greatly appreciated.

    Thanks & Regards
    Dnana

    Wednesday, January 13, 2010 11:42 AM
  • Yes, I do not think you will need to cache it that way.

    Thanks for your data Dnana.
    Wednesday, January 13, 2010 6:00 PM
    Moderator