[Honestly learn WCF] Nineth message communication mode (top) request answer and one-way

Source: Internet
Author: User

Original: [Honestly learn WCF] Nineth message communication mode (top) request answer and one-way

Learn WCF Honestly

Nineth message communication mode (top) request answer and one-way

Through the first two studies, we learned about some of the features of the service model, such as session and instantiation, and today we'll learn another important feature of the service model: message communication patterns.

WCF has three modes of communication between the server and the client: one-way mode, request/answer mode, and duplex mode.

If one-way mode is selected, the caller does not expect any response after the call has been made to the callee, and the callee does not give any feedback to the caller after the call has been executed. If a client invokes a service-side operation through a one-way mode, it does something else, does not wait for any response from the server, and he does not know whether the call was successful or not, even if there was an error. The characteristic of this pattern is that the client returns immediately after invoking the operation, from the client's point of view, the response of the user operation is very fast, but the client cannot know the result of the call.

If the request/Response mode is selected, the client will wait for the server's reply after the call is made to the server, and the server will return the result to the client after executing the operation, even if the service operation signature return value is void, the server will return an empty message telling the client that the call is complete. The client does not return from the calling method until it returns to continue with the following work. This mode is characterized by the client can always know the situation of service execution, if there is a mistake, the error will be returned, the client to monitor the execution of the service is very good, but because the client will wait until the service is returned, so if the service side of the service execution time is longer, the client side of the user response will be very slow, If the client's call to the service is on the same thread as the user interface, the application dies there, as the user sees it.

If the use of duplex mode, the client and the server can be separate to each other to send message calls, in fact, this mode is on the basis of one-way mode, both sides of the call is a one-way call, but on both sides can be independent, who do not have to wait for who, this model is more complex, we will further detailed research.

1. How to set the message communication mode.

The duplex mode has other settings, the single-line mode and the request-answering mode are set in the same location, which is set by modifying the IsOneWay property of the OperationContract property of the operation contract. The following code sets the HELLOWCF operation contract to one-way mode:

    [ServiceContract]    Public interface IHELLOWCF    {        [OperationContract (isoneway=true)]        void hellowcf ();      

If you do not configure the IsOneWay property, then he defaults to false, which means that the default message communication pattern is request/reply mode unless we explicitly specify it as one-way mode.

The following code sets the HELLOWCF action contract to request/reply mode:

    [ServiceContract]    Public interface IHELLOWCF    {        [OperationContract (isoneway=false)]        void hellowcf ();      

Because it is the default value, the IsOneWay property is not configured.

Note that in one-way mode, the return value must be void, and the parameter value cannot be returned using any out or ref means, that is, no value can be returned in any way, which is not allowed by the infrastructure, which causes the service side to throw an exception. In Request/Response mode, these are all possible, even if there is no return value (the return value is void), the return message is sent as well, but it is an empty message.

2. Examples of two modes

First we look at an example of a request/response pattern, and I use the example of the IIS hosting service used in the previous few, if you forget, turn back and get familiar.

We let the HELLOWCF on the server return "Hello wcf!" String before, let him sleep for a while on the thread.

The source code for HelloWCFService.CS is as follows:

Using system;using system.servicemodel;namespace learnwcf{    [ServiceContract] public    interface IHELLOWCF    {        [OperationContract (Isoneway=false)]        string hellowcf ();    }      public class HELLOWCFSERVICE:IHELLOWCF    {        private int _counter;        public string HELLOWCF ()        {            System.Threading.Thread.Sleep ();            Return "Hello wcf!";}}    }

No change, just let him sleep 3 seconds on the thread.

Here is the Web. config file, and nothing changed:

<configuration>  <system.serviceModel>    <services>      <service name= " Learnwcf.hellowcfservice "behaviorconfiguration=" MetadataExchange ">        <endpoint address=" "binding=" Wshttpbinding "contract=" LEARNWCF.IHELLOWCF "/>        <endpoint address=" Mex "binding=" mexHttpBinding "contract = "IMetadataExchange"/>      </service>    </services>    <behaviors>      < servicebehaviors>        <behavior name= "MetadataExchange" >          <servicemetadata httpgetenabled= "true" />        </behavior>      </serviceBehaviors>    </behaviors>  </ System.servicemodel></configuration>

Here is the Svc file, a line of code that indicates that this is a WCF service and specifies the background type:

<% @ServiceHost language=c# debug= "true" service= "Learnwcf.hellowcfservice"%>

Put the Svc file and the Web. config file under the root folder of the Web site, CS file under the App_Code folder, start IIS, the service will be homestay, if you forget how to host in IIS, immediately turn back to the third article familiar.

Generate the client with SVCUTIL.EXE or add service reference, in order to see the time of the call, we output the time separately before and after the call. The Program.cs code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.ServiceModel; Namespace consoleclient{    class program    {        static void Main (string[] args)        {            Services.hellowcfclient client = new Services.hellowcfclient ();            Console.WriteLine (DateTime.Now.ToLongTimeString ());            Console.WriteLine (client. HELLOWCF ());            Console.WriteLine (DateTime.Now.ToLongTimeString ());            Console.ReadLine ();}}}    

F5 run, the results are as follows:

Can see that the entire call took 4 seconds, in addition to the service method sleep for 3 seconds, the establishment of a session communication is also used for 1 seconds, in the server method sleep, the client has been waiting.

Next, we look at the situation of one-way mode, we modify the service contract code, let it adopt one-way mode, but note that at this time can not have a return value, must be set to void, the service method is to sleep 3 seconds, the other do nothing.

Using system;using system.servicemodel;namespace learnwcf{    [ServiceContract] public    interface IHELLOWCF    {        [OperationContract (isoneway=true)]        void hellowcf ();    }      public class HELLOWCFSERVICE:IHELLOWCF    {        private int _counter;        public void hellowcf ()        {            System.Threading.Thread.Sleep (+);}}    }


The client needs to re-download the metadata or update the service reference because the content of the service contract has changed, and the client Program.CS code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.ServiceModel; Namespace consoleclient{    class program    {        static void Main (string[] args)        {            Services.hellowcfclient client = new Services.hellowcfclient ();            Console.WriteLine (DateTime.Now.ToLongTimeString ());            Client. HELLOWCF ();            Console.WriteLine (DateTime.Now.ToLongTimeString ());            Console.ReadLine ();}}}    


F5 look at the results:

Can see only 1 seconds, the client and the server set up a session after the call sent back immediately, did not wait for the service to sleep that three seconds, of course, the client at this time do not know what the server is doing.

Note that the request answering mode is required for session support, must use a support session binding, and the service contract must be at least sessionmode-allowed. The ServiceBehavior InstanceContextMode of the service class must be persession, and we are not configured here because they are default, but we must know that they need such a configuration to support the request/reply mode.

If you encounter a puzzling problem in your experiment, try to remove all client service references and re-add the service reference, because sometimes updating a service reference is not always useful.

3. Summary

Through this study, we understand the two basic modes of message communication, and on this basis there are more complex duplex communication mode, we study in detail in the next article.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Honestly learn WCF] Nineth message communication mode (top) request answer and one-way

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.