Chapter 3)

Source: Internet
Author: User

Duplex MEP Duplex refers to sending and receiving messages simultaneously. The Duplex message exchange mode defines an operation set that allows the receiver and the sender to send messages simultaneously, and vice versa. The WSDL associated with the WSDL and the duplex message exchange mode includes two operations: One operation (SomeOperation) indicates that the message is sent to the receiver, and the other operation (SomeCallbackOperation) indicates that the message is sent back to the sender from the receiver. Two contracts are required for duplex message exchange mode and WCF contract duplex communication. Traditionally, the contract describing the messages (and response messages, if any) sent to the receiving application is called a service contract, and the contract describing the messages sent by the receiver to the sender is called a callback contract. The two contracts are associated through the ServiceContractAttribute. CallbackContract attribute of the service contract. [Csharp] // Service contract using System. ServiceModel; using CallbackContract; namespace WcfService4 {[ServiceContract (Name = "IServiceContract", CallbackContract = typeof (ICallbackService), Namespace =" http://www.andersoft.com/1ServiceContract ")] Public interface IServiceContract {[OperationContract (Name =" Sum ", IsOneWay = true)] void Sum (int value1, int value2);} // Service contract using System. serviceModel; using CallbackContract; namespace WcfService4 {[ServiceContract (Name = "IServiceContract", CallbackContract = typeof (ICallbackService), Namespace =" http://www.andersoft.com/1ServiceContract ")] Public interface IServiceContract {[OperationContract (Name =" Sum ", IsOneWay = true)] void Sum (int value1, int value2 );}} [csharp] // Service implements using System. serviceModel; using CallbackContract; namespace WcfService4 {public class ServiceAPIs: IServiceContract {public void Sum (int value1, int value2) {int res = value1 + value2; ICallbackService callback = OperationContext. current. getCallbac KChannel <ICallbackService> (); callback. sumCallback (value1, value2, res) ;}}// Service implements using System. serviceModel; using CallbackContract; namespace WcfService4 {public class ServiceAPIs: IServiceContract {public void Sum (int value1, int value2) {int res = value1 + value2; ICallbackService callback = OperationContext. current. getCallbackChannel <ICallbackService> (); callback. sumCallback (value1, Value2, res) ;}} [csharp] // Callback interface using System. serviceModel; namespace CallbackContract {public interface ICallbackService {[OperationContract (Name = "SumCallback", IsOneWay = true)] void SumCallback (int value1, int value2, int res );}} // Callback interface using System. serviceModel; namespace CallbackContract {public interface ICallbackService {[OperationContract (Name = "SumCallback", IsOneWay = True)] void SumCallback (int value1, int value2, int res);} [csharp] // Callback API using System; namespace CallbackContract {public class CallbackAPIs: ICallbackService {public void SumCallback (int value1, int value2, int res) {Console. writeLine ("{0} + {1} = {2}", value1, value2, res) ;}}// Callback APIusing System; namespace CallbackContract {public class CallbackAPIs: ICallbackService {Public void SumCallback (int value1, int value2, int res) {Console. writeLine ("{0} + {1} = {2}", value1, value2, res) ;}} [csharp] // Host using System; using System. serviceModel; using WcfService4; namespace SVC {class Program {static void Main (string [] args) {NetTcpBinding binding = new NetTcpBinding (); serviceHost svc = new ServiceHost (typeof (ServiceAPIs); svc. addServiceEndpoint (typeof (ISe RviceContract), binding, "net. tcp: // localhost: 5678/IServiceContract "); Console. writeLine ("Begin start host now... "); svc. open (); Console. writeLine ("Host was started now... "); Console. readKey (); svc. close () ;}}// Hostusing System; using System. serviceModel; using WcfService4; namespace SVC {class Program {static void Main (string [] args) {NetTcpBinding binding = new NetTcpBinding (); ServiceHost Svc = new ServiceHost (typeof (ServiceAPIs); svc. addServiceEndpoint (typeof (IServiceContract), binding, "net. tcp: // localhost: 5678/IServiceContract "); Console. writeLine ("Begin start host now... "); svc. open (); Console. writeLine ("Host was started now... "); Console. readKey (); svc. close () ;}} [csharp] // Client using System; using System. serviceModel; using CallbackContract; using WcfService4; namespace SendClient {class Program {static void Main (string [] args) {NetTcpBinding binding = new NetTcpBinding (); EndpointAddress address = new EndpointAddress (new Uri ("net. tcp: // localhost: 5678/IServiceContract "); // Set callback instance class for InstanceContext context = new InstanceContext (new CallbackAPIs ()); // Set a InstanceContext IServiceContract services = DuplexChannelFactory <IServ IceContract>. createChannel (context, binding, address); services. sum (1, 100,200); Console. readKey () ;}}// Clientusing System; using System. serviceModel; using CallbackContract; using WcfService4; namespace SendClient {class Program {static void Main (string [] args) {NetTcpBinding binding = new NetTcpBinding (); endpointAddress address = new EndpointAddress (new Uri ("net. tcp: // localhost: 5678/IServiceCon Tract "); // Set callback instance class for InstanceContext context = new InstanceContext (new CallbackAPIs (); // Set a InstanceContext IServiceContract services = DuplexChannelFactory <timeout>. createChannel (context, binding, address); services. sum (1, 100,200); Console. readKey () ;}} when creating a duplex contract, remember the operation as one-way operation. Setting the IsOneWay attribute to True can reduce the activation of each message interaction.

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.