none
Can I singleton the client? RRS feed

  • Question

  • Hello,

    I need some advice on how to increase client creation speed.
    I create a client on CF1.1 and then call several methods of the service. This doesn't happen one after another.
    Can I perform the singleton pattern in some way on this?
    That I don't always need to recreate the client, but use an already created instance?

    Can anyone point me to the right direction?

    I have also noticed that the first time that I connect to a service it takes more time.
    Is this normal? Can I decrease this delay?

    Thanks
    Tuesday, June 9, 2009 2:03 PM

Answers

  • If you want to cache the proxy just create a container class something like this

    static class Container
        {
            static PingClient proxy;
            static object guard = new object();
    
            public static PingClient Proxy
            {
                get
                {
                    if (proxy == null)
                    {
                        lock (guard)
                        {
                            if (proxy == null)
                            {
                                proxy = new PingClient();
                            }
                        }
                    }
    
                    return proxy;
                }
            }
    
            public static void ResetProxy()
            {
                proxy = null;
            }
    
        }
    This uses double check locking to allow thread safe allocation of the proxy and will always return the singleton instance on Container.Proxy. The ResetProxy method is there in case the channel faults and you need to recreate (due to a timeout for example)

    You are right about InstancecontextMode - that only affectes teh service
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    • Marked as answer by Marco Zhou Tuesday, June 16, 2009 9:03 AM
    Tuesday, June 9, 2009 3:05 PM
    Moderator
  • -> This uses double check locking to allow thread safe allocation of the proxy and will always return the singleton instance on Container.Proxy.

    This is not thread safe, please try reading my blog article on how to implement thread safe singleton pattern:

    http://shevaspace.blogspot.com/search?q=singleton+

    Thanks
    Marco
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    • Marked as answer by Marco Zhou Tuesday, June 16, 2009 9:03 AM
    Thursday, June 11, 2009 9:27 AM

All replies