Question

I am confused about the Proxy and Channels. According to my reading, WCF client is using a proxy which pass the message through a chain of channels. Every Channel is responsible of certain task, for example one channel is encoding the message and another Channel is encrypting it.

My confusion begins when I saw the following code

  • When proxy.MyMethod() is called, it actually called the whole chain of channels?

  • The author used method called CreateChannel and named the identifier proxy. So in WCF architecture Proxy is just a spacial high level channel, it is not a stand alone architecture element?

    Binding binding = new NetTcpBinding();  
    EndpointAddress address = new EndpointAddress("net.tcp://localhost:8000");  
    IMyContract proxy = ChannelFactory<IMyContract>.CreateChannel(binding,address);  
    using(proxy as IDisposable)  
    {  
        proxy.MyMethod();  
    }  
    
Was it helpful?

Solution 2

Yes, I think you described this accurately. WCF has this concept of "channels", which developers tend to configure in the web.config and not write C# code around.

These are described in the Channel Model Overview.

When you call ChannelFactory.CreateChannel(binding,address); the framework looks at your configuration and creates all these channels for you as one object. So yes, the proxy is like a stack of channels.

From your end you interact with it as one object. The framework deals with the implementation of separate channels. It is still nice to understand that you are going through these layers so that you can configure them properly.

OTHER TIPS

In WCF you have 3 main component - Contract, Adress and Binding. The channel is a pipe, which is building according to these three parts.

enter image description here

The aim of the channel is to modify the message into format, that is understood for both - client and server, and to organize it's proper transport. The transport and protocol channels are used for this. To make this process easier, we use bindings. Each binding consists of elements, which are representing some channel in channel stack.

So, each time when you call you method, it forms the message according to your DataContract, and passes it throught the whole chain of channels. Each channel modifies your message. The prosses looks like this

enter image description here

A WCF proxy is really just a level of abstraction. It is an in-process representative of an out-of-process service. You can imagine it as an object, generated and properly configured according to your binding elements and your dataContract, which allows your client-side and server-side to understand each other.

In this case "proxy" is a reference to the software design pattern. From Wikipedia:

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

In the case of WCF the ChannelFactory<>.CreateChannel is creating a channel stack based on the configuration. Each channel provides an abstraction to the channel below it. For example a simplified channel stack might be:

  • channel 1 serialize the .NET object into a SOAP message
  • channel 2 Adds security information to the message
  • channel 3 encode the message to be sent over TCP.

I think you understand all that.

Back to the use of “proxy”: Any channel in the stack is a proxy for the channel below it. That is to say no channel is the final destination. All channels are “an interface to something else”. However as you move down the channel stack the different channels are providing a different interface/abstraction (.NET object, SOAP Message, etc.) to the caller.

Again this discussion is meant to answer the question and intentionally simplified.

When you are writing client code, the interface that is most useful is the one that understands .NET objects. Knowing this the ChannelFactory returns the channel that’s at the top of the stack that has a .NET interface that matches your service contract. It would be valid to create a SOAP message as a string, walk down the channel stack (or create a custom stack), find the channel that accepts a raw SOAP message, label that a “proxy”, and call it directly.

The author used method called CreateChannel and named the identifier proxy. So in WCF architecture Proxy is just a special high level channel, it is not a stand alone architecture element?

Channel is an architectural element which implements the proxy software design pattern. All channels are proxies. When you are writing client code, the top level channel happens to be the most useful because it accepts .NET operation contracts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top