Channel (2) of WCF)

Source: Internet
Author: User

Next we will talk about channellistener and channelfactory.

Channellistener is a service-side technology used to listen for messages, create channel stacks, and provide applications with references pointing to the top of stacks.

We do not directly use channellistener, but we often use servicehost to listen for messages.

static void Main(string[] args){    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);    Uri address = new Uri("http://localhost/request");    Console.WriteLine("Starting Service...");    IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>(address, new BindingParameterCollection());    listener.Open();    IReplyChannel channel = listener.AcceptChannel();    channel.Open();    Console.WriteLine("Service Started~");    Console.WriteLine("Waiting for request...");    RequestContext request = channel.ReceiveRequest();    Message message = request.RequestMessage;    string data = message.GetBody<string>();    Message replyMessage = Message.CreateMessage(message.Version, "http://localhost/reply", data);    request.Reply(replyMessage);    Console.WriteLine("Service stopped!");    message.Close();    request.Close();    channel.Close();    listener.Close();    Console.ReadLine();}

 

Channelfactory is a client-side technology used to send messages and define the owner of the channel it creates.

We do not directly use channelfactory, but we often see clientbase <t>, which is the code automatically generated after the add service reference.

static void Main(string[] args){    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);    IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());    factory.Open();    IRequestChannel channel = factory.CreateChannel(new EndpointAddress("http://localhost/request"));    channel.Open();    Message requestMessage = Message.CreateMessage(MessageVersion.Soap11, "http://localhost/reply", "This is the body data");    Console.WriteLine("Sending message...");    Message replyMessage = channel.Request(requestMessage);    string data = replyMessage.GetBody<string>();    Console.WriteLine("Reply received~");    requestMessage.Close();    replyMessage.Close();    channel.Close();    factory.Close();    Console.ReadLine();}

 

The above two sections of code form a demo of request-reply. Download the code: requestreply.zip

 

The difference between channellistener and channelfactory is that the former closes all related channels, while the latter does not.

 

Channelfactory <t> is different from channelfactory. The former is used to create multiple clients.

 

Finally, I mentioned icommunicationobject. All channels, channellistener, and channelfactory have implemented this interface, for example:

IOutputChannel : IChannel, ICommunicationObject

This interface is used to implement the state machine.

Note the state attribute of this interface, which is an Enum and corresponds to all States of the state machine:

public enum CommunicationState{    Created = 0,    Opening = 1,    Opened = 2,    Closing = 3,    Closed = 4,    Faulted = 5,}

 

Clientbase <t> implements the icommunicationobject interface.

 

In addition, WCF provides an abstract class communicationobject, implements the icommunicationobject interface, and implements the state machine. Therefore, we can use the events exposed by the icommunicationobject interface. As long as we derive from this abstract class, we can customize our own methods and events.

In this example on the p109 page of WCF, a client is converted to an icommunicationobject interface, and a custom method is attached to the event exposed by the interface:

 

At this point, all the channels have been completed, but unfortunately, these contents can only be used to deepen our understanding of WCF, but are rarely used in practice. The WCF development team needs to think about why.

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.