The message exchange patterns in WCF is divided into request/reply, one-way, duplex, or callback );
This chapter mainly introduces request/reply operations. Request/response operations are the default actions of all service operations. By viewing the WSDL description, we can see <WSDL: description of input> and <WSDL: output> messages. The message can contain request parameters, returned data, <body> elements, or returned soap errors.
In a request/response operation, the client sends a request in the form of a message, which blocks the client until it receives the Response Message. The default response timeout value is 1 minute. If the service still does not respond after this time, the client will get a timeoutexception exception. The request/response is similar to the classic Client/Server programming model. The returned response message includes the returned result, or it converts the returned value to the return value of a common method. In addition, if there is a communication exception or a server exception, the proxy throws an exception on the client. Except for binding netpeertcpbinding and netmsmqbinding, all bindings support request/response operations.
The request/response operation mode is as follows:
Next we will introduce the request/response operations through a demo:
MainCodeAs follows:
Interface code:
[Servicecontract (namespace = " Http://schemas.xinhaijulan.com/demos/RequestReply " )]
Public Interface Ihellowcf
{
[Operationcontract (name = " Getmessagecontract " , Action = " URN: acioninput " , Replyaction = " URN: actionoutput " )]
String Getmessage ( String MSG );
}
Implementation Code:
Public Class Hellowcf: ihellowcf
{
Public String Getmessage ( String MSG)
{
Return " Message from client is: " + MSG;
}
}
Server code:
Static Void Main ( String [] ARGs)
{
Using (Servicehost host = New Servicehost ( Typeof (Servicelibrary. hellowcf )))
{
Host. addserviceendpoint ( Typeof (Servicelibrary. ihellowcf ), New Nettcpbinding (), " Net. TCP: // localhost: 9000/hellowcf " );
Host. open ();
Console. writeline ( " Please input exit to close host. " );
String Key = Console. Readline ();
While (Key. tolower () ! = " Exit " )
{
Console. writeline ( " Please input exit to close host. " );
Key = Console. Readline ();
}
}
}
Client code:
Static Void Main ( String [] ARGs)
{
Channelfactory < Servicelibrary. ihellowcf > Channelfactory = New Channelfactory < Servicelibrary. ihellowcf > ( New Nettcpbinding ());
Servicelibrary. ihellowcf Client = Channelfactory. createchannel ( New Endpointaddress ( " Net. TCP: // localhost: 9000/hellowcf " ));
Using (Client As Idisposable)
{
Console. writeline ( " ------------ Begin ------------- " );
Console. writeline (client. getmessage ( " Hello WCF! " ));
Console. writeline ( " ------------ End --------------- " );
}
Console. Readline ();
}
Run the code and output it as follows:
------------Begin-------------
Message from clientIs: Hello WCF!
------------End---------------
Now, the message exchange mode (MEP)-Request/response operation has been introduced.
Click to download the demo.
Author: Xinhai julan
Source: http://xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.