WCF Duplex mode

Source: Internet
Author: User

Duplex mode

Description: duplex mode is built on the basis of the reply mode and one-way mode, which realizes the client-server mutual invocation.

Call each other: In the past we just call the server on the client side, and then the service side has a return value to return the client, and the mutual call is not only the client calls the server, but also the server can invoke the client's method.

1. Add the WCF service Service2.svc, and define the interface for the callback, server-side interface IService2.cs:

Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.text;namespace wcfservice1{    //callbackcontract = typeof (IService2CallBack) The interface type that defines the callback    [ServiceContract (callbackcontract = typeof (Iservice2callback))] public    interface IService2    {        [OperationContract]        string showname (string name);    Callback interface in which the method on the client implements public    interface Iservice2callback    {        //isoneway = True to start one-way mode, the mode method cannot have a return value        [OperationContract (IsOneWay = true)]        void printsomething (String str);}    }

  

2. Service-Side Implementation SERVICE2.SVC

Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using system.servicemodel;using system.text;namespace wcfservice1{Public    class Service2:iservice2    {        Iservice2callback callback = null;//Callback interface type public        Service2 ()        {            //Gets the client instance that invokes the current operation            callback = Operationcontext.current.getcallbackchannel<iservice2callback> ();        }        <summary>//        services called by the client///</summary>//        <param name= "name" ></param>        //<returns></returns> public string ShowName (string name)        {            callback. Printsomething (name);            Return "Server call client, WCF service, display name: Xsj ...";}}    

  

3. Server-side configuration, Web. config adds configuration in System.ServiceModel,

There are 4 types of bindings that support callbacks: Wsdualhttpbinding, NetTcpBinding, NetNamedPipeBinding, netpeertcpbinding.

Here we use wsdualhttpbinding as an example:

<endpoint address= ""  binding= "wsdualhttpbinding" contract= "Wcfservice1.iservice2" ></endpoint>
<system.serviceModel>    <services>      <service name= "Wcfservice1.service2" >        < Endpoint address= ""  binding= "wsdualhttpbinding" contract= "Wcfservice1.iservice2" ></endpoint>      </service>    </services></system.serviceModel>

  

WCF Duplex mode

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.