Sunday 18 March 2012

Difference between proxy and channel factory


Hi friends, 


In this article I will show you what difference between WCF Channel factory and Proxy.
There are 3 basic ways to create a WCF client:
1.      Proxy
2.      WCF Channel Factory
3.      REST services, using the HttpClient or WebClient
Before the difference I will explain you what is WCF channel factory and what is proxy


1.   What is Proxy:
The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.

The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy. Let Visual Studio generate your proxy. This auto generates code that connects to the service by reading the WSDL. If the service changes for any reason you have to regenerate it. The big advantage of this is that it is easy to set up - VS has a wizard and it's all automatic. The disadvantage is that you're relying on VS to do all the hard work for you, and so you lose cont

Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with
When we generate (WCF) service proxy class for a service by using svcutil.exe, it creates a proxy that derives from System.ServiceModel.ClientBase<TChannel>. The proxy implements IDisposable and the client code can be wraped inside using statement and guaranteed clean-up in the face of exceptions.



2.   What is Channel Factory :

Use ChannelFactory with a known interface. This relies on you having local interfaces that describe the service (the service contract). The big advantage is that can manage change much more easily - you still have to recompile and fix changes, but now you're not regenerating code, you're referencing the new interfaces. Commonly this is used when you control both server and client as both can be much more easily mocked for unit testing
In order to use ChannelFactory<T> you must be willing to share contract assemblies between the service and the client. If this is okay with you then ChannelFactory<T> can save you some time.
When you share a common service contract dll between the client and the server, you'll be using the ChannelFactory class
When we use channel factory following steps to do
  1. Create a binding
  2. Create a ChannelFactory.
  3. Create a Channel
  4. Send/Receive messages over that channel.


In this example this is done by using ChannelFactory. Once the channel is in place we can send messages back and forth to the service.
using System;
using System.ServiceModel;
using EchoClientConsole.EchoServiceReference;

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

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

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

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


3.   Difference between proxy and channel factory


PROXY
Channel Factory
1
Only require URL where the service resides

You must have direct access to the assembly that contains that service contract T for
2
Very Simpler

Not easier
3
Easy to Understand
channels are complex, network-related
4
There is Visual Studio gives you add the reference
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. Contructors 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 will create PRoxy
When you are using DLL that refers Service contract interface then use the channel factory class




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

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal


------------------------------------------------------------------------------------------------
 Blog:  www.sujitbhujbal.blogspot.com
Personal Website :- http://sujitbhujbal.wordpress.com/
Contact me on 9822663535
---------------------------------------------------------------------------------

6 comments: