Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#
Tip/Trick

Difference between Proxy and Channel Factory in WCF

Rate me:
Please Sign up or sign in to vote.
4.72/5 (29 votes)
7 Mar 2013CPOL3 min read 151.2K   34   19
In this article I will show you the differences between WCF channel factory and proxy and when to use them.

Introduction

In this article I will show you the difference betweens a WCF Channel factory and Proxy and when to use a Proxy and when to use a Channel Factory.

Background

There are three ways to create a WCF client:

  • Proxy
  • WCF Channel Factory
  • REST services, using HttpClient or WebClient

Using the code

Before going into the differences, I will explain what a WCF channel factory is and what a proxy is in WCF and when to use a Proxy and when to use a channel factory.

What is Proxy

Proxy is a CLR class that exposes a single CLR interface representing the service contract.

A proxy is the same as a service contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy gives the following details: its location, its implementation technology, and run time platform and communication transport.

A proxy can be generated in two ways:

  1. By using Visual Studio: by right clicking References and clicking on Add Service Reference. Then open the Add Service Reference dialog box where you have to specify the base address of the service and the namespace that contains the proxy.
  2. This auto generates proxy code that connects to the service by reading the WSDL (Web Service Descriptive language). If the service address changes for any reason you have to regenerate it. The big advantage of this is that it is easy to set up - Visual Studio has a wizard and it's all automatic. The disadvantage is that you're relying on Visual Studio to do all the hard work for you, and so you lose control.

  3. Using the SvcUtil.exe command-line utility. We have to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, using svcutil.exe, it creates a proxy that derives from System.ServiceModel.ClientBase<TChannel>.

2. What is Channel Factory

Use the ChannelFactory class with a known interface. This depends on you having local interfaces that describe the service contract. The big advantage is that you can change more easily. But you still have to recompile and fix changes, but now you're not regenerating code, you're only referencing the new interfaces. Channel factory is commonly used when you have control of both the server and the client.

In ChannelFactory<T> you must share contract assemblies between the service and the client. That's why ChannelFactory<T> can save you time.

When your project shares a common service contract DLL between the client and the server, you have to use the ChannelFactory class.

For using channel factory, the following steps are necessary:

  1. Create a binding in WCF
  2. Create a ChannelFactory class
  3. Create a Channel
  4. Send-Receive messages over that channel

Example:

In the below example, we use the ChannelFactory class and we send messages back and forth to the service.

C#
using System;
using System.ServiceModel;
using ClientConsole.ServiceReference;

namespace ClientConsole
{
    internal class Program
    {
        private static void Main()
        {
            var channelFactory =
                new ChannelFactory<IEchoService>(
                    "WSHttpBinding_IService" // endpoint name
                    ); 

            IService channel = channelFactory.CreateChannel();
            Message result = channel.Echo(
                new Message
                    {
                        Text = "Hey "  
                    });

            Console.WriteLine("Service given respond at {0}: {1}",
                              result.Invoked,
                              result.Text);
            Console.ReadLine();

            ((IClientChannel)channel).Close();
        }
    }
}

Difference between proxy and channel factory

Proxy Channel Factory
1 Only requires a URL where the service resides You must have direct access to the assembly that contains the service contract
2 Very simple Not easier
3 Easy to understand Channels are complex, network-related
4 Visual Studio gives you the Add the Reference option When you share a common service contract DLL between the client and the server, you'll be using the ChannelFactory class
5 Proxies have several restrictions like:
  1. Properties need to have gets and sets
  2. Constructors can't be exposed
  3. Methods other than the service contract cannot be exposed
If you know that your entities will not change much and the client code is less, then a DLL would work better than a proxy
6 By using SVCutil.exe, you can create a proxy When you are using a DLL that refers to the service contract interface then use the channel factory class

Good luck and please let me know your feedback! In this way you can learn the difference between a proxy and a channel factory.

If you have any queries, mail me at Sujeet.bhujbal@gmail.com.

License

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


Written By
Technical Lead
India India
Sujit Bhujbal is Senior Software Engineer having over 12 + years of Experience in .NET core , C#, Angular , SQL Server and has worked on various platforms. He worked during various phases of SDLC such as Requirements and Analysis, Design and Construction, development, maintenance, Testing, UAT.

He is Microsoft Certified Technology Specialist (MCTS) in Asp.net /WCF Applications. He worked at various levels and currently working as a Senior Software Engineer.

His core areas of skill are Web application development using WPF,WCF , C#.Net, ASP.Net 3.5, WCF, SQL Server 2008, CSS, Java script Web design using HTML, AJAX and Crystal Reports

He is proficient in developing applications using SilverLight irrespective of the language, with sound knowledge of Microsoft Expression Blend and exposure to other Microsoft Expression studio tools.


Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

------------------------------------------------------------------------
Blog: Visit Sujit Bhujbal

CodeProject:- Sujit Bhujbal codeproject

DotNetHeaven:- DotNetHeaven

CsharpCorner:-CsharpCorner

Linkedin :-Linkedin

Stack-Exchange: <a href="http://stackexchange.com/users/469811/sujit-bhu

Comments and Discussions

 
Questionis config changes required at client or server end Pin
sachin.vishwa9028-Jul-16 6:13
professionalsachin.vishwa9028-Jul-16 6:13 
GeneralMy vote of 5 Pin
Sanket Bankar28-Oct-15 1:05
Sanket Bankar28-Oct-15 1:05 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal7-Jul-16 1:56
Sujeet Bhujbal7-Jul-16 1:56 
QuestionPerformance Pin
Julia_H20-Feb-15 2:22
Julia_H20-Feb-15 2:22 
GeneralGood Pin
cateyes998-Jul-14 17:04
cateyes998-Jul-14 17:04 
GeneralRe: Good Pin
Sujeet Bhujbal27-Oct-14 20:03
Sujeet Bhujbal27-Oct-14 20:03 
QuestionIs there a way to use a service interface or dll so the Client WCF does not need to recompile when the service interface changes? Pin
rapiddata30-Oct-13 3:39
rapiddata30-Oct-13 3:39 
AnswerRe: Is there a way to use a service interface or dll so the Client WCF does not need to recompile when the service interface changes? Pin
Sujeet Bhujbal30-Oct-13 7:17
Sujeet Bhujbal30-Oct-13 7:17 
AnswerRe: Is there a way to use a service interface or dll so the Client WCF does not need to recompile when the service interface changes? Pin
abhi_here2-Feb-15 5:00
abhi_here2-Feb-15 5:00 
GeneralMy vote of 5 Pin
Pratik Bhuva24-Oct-13 2:24
professionalPratik Bhuva24-Oct-13 2:24 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal24-Oct-13 20:32
Sujeet Bhujbal24-Oct-13 20:32 
GeneralRe: My vote of 5 Pin
Pratik Bhuva24-Oct-13 20:37
professionalPratik Bhuva24-Oct-13 20:37 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal24-Oct-13 20:58
Sujeet Bhujbal24-Oct-13 20:58 
GeneralRe: My vote of 5 Pin
Pratik Bhuva24-Oct-13 22:23
professionalPratik Bhuva24-Oct-13 22:23 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal24-Oct-13 22:24
Sujeet Bhujbal24-Oct-13 22:24 
QuestionCan I use ChannelFactory in case of multiple third party Services Pin
niraj12311-Aug-13 20:03
niraj12311-Aug-13 20:03 
AnswerRe: Can I use ChannelFactory in case of multiple third party Services Pin
Sujeet Bhujbal11-Aug-13 22:25
Sujeet Bhujbal11-Aug-13 22:25 
Hi
You need DLL with interface for Channel Factory
Please do following

pull out your interface and your contract objects, to a 3rd assembly, and reference that from your service code and also your client

Channel Factory As its name suggests, ChannelFactory is a factory for creating service communication channels at runtime.

XML
ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, address);
      service = factory.CreateChannel();




Thanks
Sujeet
Thanks
Sujeet

Blog: www.sujitbhujbal.blogspot.com
Personal Website :- http://sujitbhujbal.wordpress.com/
Facebook :- www.facebook.com/sujit.bhujbal
CodeProject:-http://www.codeproject.com/Members/Sujit-Bhujbal
www.sujitbhujbal.com

GeneralMy vote of 5 Pin
Terespl7-Aug-13 1:55
Terespl7-Aug-13 1:55 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal7-Aug-13 20:36
Sujeet Bhujbal7-Aug-13 20:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.