Difference between Proxy and Channel Factory in WCF

Introduction:
This article explains the difference between a WCF Channel factory and Proxy and when to use a Proxy and when to use a Channel Factory.

There are three ways to create a WCF client:
    Proxy
    WCF Channel Factory
    REST services, using HttpClient or WebClient 


1- 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.
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.
    

2- 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- 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:

  •     Create a binding in WCF
  •     Create a ChannelFactory class
  •     Create a Channel
  •     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.

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
Difference between Proxy and Channel Factory in WCF
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment