Click here to Skip to main content
13,052,669 members (60,184 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 18 Dec 2009

Best Practices for creation of WCF proxy in .NET

Rate this:
Please Sign up or sign in to vote.


From past few days I was doing some findings about the best ways of creating the service proxy's in WCF.

Here goes the collection of results of that exercise..


In this first case, We are creating the service proxy with the help of the ChannelFactory and we are closing that factory once we are done with the exercise,but it might so happen that during close call it might throw an exception,Afterwards that proxy state becomes faulted and it may be of no use(you cant make any call on that), hence to be on the safer side, by using a bool we are cleaning the instance if at all any exception happens. How cool right?
        private void ProxyCreation()
            ChannelFactory<OrderServiceReference.OrderService> factory = default(ChannelFactory<OrderServiceReference.OrderService>);
            bool blnClosed = false;
                factory = new ChannelFactory<OrderServiceReference.OrderService>("WSHttpBinding_OrderService");
                OrderServiceReference.OrderService proxy = factory.CreateChannel();
                //Make a call by proxy
                int i = proxy.GetScore();
                blnClosed = true;
                if (!blnClosed)


In this second case, we are using the proxy generated by the svcutil through VS IDE.
using block will ensure that the proxy is Disposed,and hence the resource is getting cleanedup (You can see why is it like that in the Equivalent code section)

    using (OrderServiceReference.OrderServiceClient proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient())
       int i = proxy.GetScore();
Equivalent code after compilation - 
     OrderServiceReference.OrderServiceClient proxy = default(OrderServiceReference.OrderServiceClient);
                proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient();
                int i = proxy.GetScore();

I hope this helps!!



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


About the Author

Vinayaka Krishna Shenoy
Architect MindTree Ltd
India India
Motivated achiever who guides organizations in applying technology to business settings, provides added value, and creates project deliverables in a timely manner. An experienced Technical Consultant, have successfully led large project teams of more than 20 people from requirements gathering to implementation and support using C#, .NET ,ADO.NET, ADO.NET Entity Framework,ASP.NET,ASP.NET MVC, WCF and SQL Server.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 23 Dec 2009
Article Copyright 2009 by Vinayaka Krishna Shenoy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid