WCF message exchange mode: datagram Mode

Source: Internet
Author: User

The datagram mode refers to the method in which the sender sends the message to the other party and completes interaction after receiving the confirmation message. The sender only determines that the message is successfully sent, however, I do not know whether the message has reached the final node, whether it has been processed, and how the returned results are completely unknown;

The client implements the ioutputchannel, and the server implements the iinputchannel

SenderCode:

Using system. servicemodel. channels;
Using system. servicemodel;

namespace client output
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> bindingelement [] bindingelements = new bindingelement [3];
bindingelements [0] = new textmessageencodingbindingelement ();
bindingelements [1] = new onewaybindingelement ();
bindingelements [2] = new httptransportbindingelement ();

// HTTP Transmission
Custombinding binding = new custombinding (bindingelements );
Message message;
Ichannelfactory <ioutputchannel> factor = NULL;
Ioutputchannel outputchannel = NULL;

String msgbody = console. Readline ();
Message = message. createmessage (binding. messageversion, "sendmessage", msgbody );

// Create a message
Factor = binding. buildchannelfactory <ioutputchannel> (New bindingparametercollection ());
Factor. open ();
Outputchannel = factor. createchannel (New endpointaddress ("http: // localhost: 9090/inputservice "));
Outputchannel. open ();
Outputchannel. Send (Message );
Console. writeline ("message sent successfully ");

Outputchannel. Close ();
Factor. Close ();
}
}
}

 

Acceptor:

 

Using system. servicemodel. channels;

namespace Server Input
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> bindingelement [] bindingelements = new bindingelement [3];
bindingelements [0] = new textmessageencodingbindingelement ();
bindingelements [1] = new onewaybindingelement ();
bindingelements [2] = new httptransportbindingelement ();

Custombinding binding = new custombinding (bindingelements );
Ichannellistener <iinputchannel> listener = binding. buildchannellistener <iinputchannel> (New uri ("http: // localhost: 9090/inputservice"), new bindingparametercollection ());

Listener. open ();
Iinputchannel inputchangel = listener. acceptchannel ();
Inputchangel. open ();
Console. writeline ("start receiving messages ");
Message message = inputchangel. Receive ();
Console. writeline ("received a new message, Action: {0}, body: {1}", message. headers. action, message. getbody <string> ());
Message. Close ();
Inputchangel. Close ();
Listener. Close ();
Console. Read ();

}
}
}

From:. net3.5 Advanced Programming

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.