WCF programming series (7) channel and channel Factory

Source: Internet
Author: User

Channel and channel Stack

As mentioned above, in WCF, client-server interaction is performed through messages. Messages are sent from the client to the server through multiple processing actions. In the WCF programming model, these actions are performed in layers: after the previous layer completes message processing, send messages to the next level, and so on. Each layer corresponds to a channel (we can understand it as a message channel ). The channel layers of these columns constitute a channel stack. Obviously, the last channel in the channel stack should be the transmission channel, which sends the processed messages to the server or receives the response from the server, there are also a series of protocol channels on top of the transmission channel, which process messages.

We can regard the channel as the implementation of binding. From the previous example, we can see that binding is actually an end point configuration, this configuration affects the transmission and processing of messages when the client interacts with the server (which protocol is used for transmission, message encoding, and message Security Protocol waiting ). The binding itself is composed of a series of binding elements. Each binding element must have a channel layer corresponding to it. The final binding element affects messages through the corresponding channel layer.

Channel Factory

The channel stack is usually created in factory mode. In this mode, the channel stack is created by binding. On the client, use the bind to generate the channel factory channelfactory, then the channelfactory generates the channel stack and returns a reference to the top channel in the stack. Then, the application can use this channel to send messages.

On the server side, use the bind to generate the channel listener ichannellistener to listen for incoming messages.IchannellistenerBy creating a channel stack and passing application references to the top channel, messages are provided to the listener application. Then, the application uses this channel to receive incoming messages. (Note: The Listener used by the server will be described later. This article only describes the channel factory used by the client)

OK, to sum up:

1. The binding includes binding elements of some columns. Each binding element corresponds to a channel.

2. The channel is responsible for processing messages.

3. The channel stack is formed by the stacked order of the binding elements in the binding.

4. The channel is generated by the channel factory, and the channel factory is created by binding (the channel factory that calls each binding element creates a channel and returns the first channel in the channel stack)

 

 

 

Channelfactory <t> generic class

A channel factory class is a generic class used to create a channel. The parameter type T is usually our service contract interface (ifirstservice), which can associate the channel with the service contract.

The channel factory has seven common constructor functions. The constructor parameters are used to build a service endpoint. If you use a non-parameter constructor, after instantiation, you can set the endpoint attribute through the endpoint attribute. For specific function declaration, see msdn.

Another important method for Channel factory is to create a channel: createchannel. This method returns a T-type instance, that is, the instance of the service contract interface. Therefore, through this instance, we can directly call the service method on the client just like operating a local object.

 

In example 1, we use the client code generated by svcutil. In the generated code file, we define a service contract interface ifirstservice. Of course, this interface is required. Another class is firstserviceclient, which inherits from clientbase <ifirstservice> and ifirstservice. In this class, operations on the channel factory are encapsulated. We usually call this class a client proxy class, note the implementation code of the ifirstservice interface method in the class:

Hide row number copy code? Getdata
  1. public string GetData(string name)
  2. {
  3.     return base.Channel.GetData(name);
  4. }

The getdata method of the channel in the clientbase <ifirstservice> is called directly. The channel attribute is actually an ifirstservice instance returned by the createchannel method of the channel factory in channelfactory <ifirstservice>. All, the firstserviceclient class is only used to encapsulate the basic operations of the channel factory. svcutil generates this class so that we directly call the service without directly operating the channel factory in the client application, recall our client program. call code in Cs:

Hide row number copy code? Program. CS
  1. FirstServiceClient client = new FirstServiceClient("BasicHttpBinding_IFirstService");
  2. Console. writeline ("using basic HTTP binding:" + client. getdata (key ));

The clientbase <ifirstservice> class is a base class provided by WCF for the client, which implements operations on the channel factory. However, this class can only be used as a base class and cannot be instantiated. Therefore, the client proxy class inherits the clientbase <ifirstservice> method to implement basic channel operations. After understanding the relationship between the client proxy class, service contract interface, and clientbase class, we can implement the client proxy class by ourselves: Derived from clientbase, the service contract method corresponding to the channel in clientbase is used to implement the service contract interface of the client proxy class (the implementation of the getdata method ).

Of course, the client proxy method is a standard client implementation method, but we can also directly operate the channel factory to implement the client call code, which is actually very simple:

Hide row number copy code? Program. CS
  1. ChannelFactory<IFirstService> cf = new ChannelFactory<IFirstService>(new BasicHttpBinding(), "http://localhost:8000/");
  2. IFirstService s = cf.CreateChannel();
  3. Console.WriteLine(s.GetData(key));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.