Design and implementation of the WCF Foundation service contract

Source: Internet
Author: User

There was also a chapter "WCF Overview", which is a summary of the text, not "copied", directly from chapter two.

Of course, learning WCF still needs some foundation. https://msdn.microsoft.com/zh-cn/hh148206 This is the "original", like WCF can download it by itself.

WCF Simple terms:

Message: The message is a separate unit of data, mainly composed of the message header and message body, the introduction of the time I understand it simple: to send a message, is to call a method, the specific later.

Service: Service said simple point is the collection of endpoints.

Final node: The construction of the receive and send message that contains the ABC three elements of what we call WCF.

A class or interface can define a service contract, but it is recommended to use an interface, which can implement multiple service contracts because the interface can inherit multiple, and the class can inherit only one.

// The following is a simple service contract // The interface of the ServiceContract property indicates that the interface is a service contract. // The method of the OperationContract property indicates that the method is an operation contract // only these two attributes are marked to be discovered by the client [ServiceContract]  Public Interface ihellowcfservice{    [OperationContract]    string  SayHello ();}

Three modes of operation for service contracts: request/reply, unidirectional and duplex

The first two are through OperationContract's isoneway=true| | False to toggle

True is one-way, false is request/reply. IsOneWay the default value is False.

Give a tip: The return value of the operation is void when there is a return value, so if it is void please set IsOneWay to True

[OperationContract (isoneway=true)] void SayHello ();

Now, let's talk about duplex, this thing .... I'll just talk about my understanding, then get some code. You come down and write two more times, do not understand, you will understand.
Talk about people and washing machines, people are the client, washing machine is the service side. Now we have to wash the clothes, we have to call the washing machine to start washing the way, and put the clothes as parameters in the washing machine, washing clothes must not be a few minutes to fix, at this time we can do something else, watching TV, sweeping the floor, and washing machine after washing how to let us know, The washing machine said or I give you a beep, I said, you make a beep, then everything is OK, of course, you have to with this washing machine in a house alone, if there are many washing machines in the house, or many other people, the washing machine rang, you do not know whether your washing. After washing the washer, will make a beep, we hear this voice, we will automatically call the way to collect clothes.

OK, so many do not know whether it is enough to say, anyway, you write more!

This is the service-side code

[ServiceContract (SessionMode = sessionmode.required, callbackcontract =typeof(Iyifuxihaole))] Public Interfaceixiyijiservice{[OperationContract (IsOneWay=true)]     voidXiyifu ();} [ServiceContract] Public Interfaceiyifuxihaole{[OperationContract (IsOneWay=true)]     voidDiDi ();} Public classxiyiji:ixiyijiservice{ Public voidXiyifu () {//washing clothes takes 5 secondsThread.Sleep ( the); //washed, and began to make a Didi sound to let people hearIyifuxihaole callback = operationcontext.current.getcallbackchannel<iyifuxihaole>(); Callback.    DiDi (); }}

This is the client's

    classProgram {Static voidMain (string[] args) {Tingdaoshengyin Tdsy=NewTingdaoshengyin (); InstanceContext Context=NewInstanceContext (Tdsy); Myservice.xiyijiserviceclient myclient=Newmyservice.xiyijiserviceclient (context);            Myclient.xiyifu ();        Console.readkey (); }    }     Public classTingDaoShengYin:MyService.IXiYiJiServiceCallback { Public voidDiDi () {Console.WriteLine ("It's starting to pick up clothes."); }    }

Sorry, forget to choose a binding that supports duplex.

Design and implementation of the WCF Foundation service contract

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.