A: The service is a collection of endpoints
When you are developing WCF, you may have noticed that a service can advertise multiple endpoint, indeed, there is a classic word in WCF, called " Service is a collection of endpoints ,"
For example, an ordinary service, it announced a service endpoint, a metadata endpoint, right ...
Think carefully, this question is fun, since a service can be published multiple endpoint, and I also know that WCF has a lot of binding, these binding corresponding to a lot of transmission, it is not
Say I a service can be published by a variety of protocol methods, such as at the same time to NETTCP,BASIC,MSMQBINDING,UDP and other ways to publish, right, then this is not super fun, if the other side
Non-. NET program, then you can call my basic, if the other side is a. NET program, that is not possible to call my nettcp, the wrong ... Of course, WCF is omnipotent, it's a mighty bull in history.
Forcing the frame, the dead, has forced the programmer to simply change a few configurations can achieve a completely different effect ... Below I also use nettcp and basic way to announce the service at the same time, OK, now I
Let's witness the miracle ...
Service:
1 usingSystem;2 usingSystem.Runtime.Serialization;3 usingSystem.ServiceModel;4 usingSystem.ServiceModel.Channels;5 usingSystem.Threading;6 7 namespaceMyService8 {9 Public classHomeservice:ihomeserviceTen { One PublicStudent Update (Student message) A { - return NewStudent () {Name ="Line Code Farm" }; - } the } - - [DataContract] - Public classStudent + { - [DataMember] + Public stringName {Get;Set; } A at [DataMember] - Public intAge {Get;Set; } - } -}
Host:
1 classProgram12 {3 Static voidMain (string[] args)4 {5ServiceHost host =NewServiceHost (typeof(Homeservice),NewUri ("http://192.168.1.105:1920"));6 7Host. AddServiceEndpoint (typeof(Ihomeservice),NewBasicHttpBinding (),"Homeservie");8 9Host. AddServiceEndpoint (typeof(Ihomeservice),NewNetTcpBinding (),"net.tcp://192.168.1.105:1921/homeservietcp");Ten OneHost. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true }); A -Host. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex"); - the host. Open (); - - Console.read (); - } +}
Client side:
1 class Program2 {3 Static voidMain (string[] args)4 {5 //Basic Mode6Channelfactory<ihomeservice> factory =NewChannelfactory<ihomeservice> (NewBasicHttpBinding (),7 NewEndpointAddress ("Http://192.168.1.105:1920/HomeServie"));8 9 varClient =Factory. CreateChannel ();Ten One varresult = client. Update (NewStudent () {}); A - - //Nettcp Way theFactory =NewChannelfactory<ihomeservice> (Newnettcpbinding (), - NewEndpointAddress ("net.tcp://192.168.1.105:1921/homeservietcp")); - -Client =Factory. CreateChannel (); + -result = client. Update (NewStudent () {}); + } A}
Through the above code, is not already found, I in the client side, can be called in basic mode, and can be called in nettcp way, this technique is not the sense that WCF is extremely powerful???
Two: Host host multiple service
We know that there are many ways to host WCF, there are IIS, there are windowservice, there is a simple and convenient console way, and by default, our most common method is a service, a homestay,
And in fact??? In fact, a host host can host multiple service, it does not seem very fun, if you have 10 Servcie, now you just need to use a console host to be able to boarding up , waste
Don't say much, I'll show you what I can show you.
Service:
1 namespaceMyService2 {3 [ServiceContract]4 Public InterfaceIhomeservice5 {6 [OperationContract]7 Student Update (Student message);8 }9 Ten [ServiceContract] One Public InterfaceIflyservice A { - [OperationContract] - Student Fly (Student stu); the } -}
Host:
1 classProgram12 {3 Static voidMain (string[] args)4 {5 //The first one: this is home service6ServiceHost host =NewServiceHost (typeof(Homeservice),NewUri ("http://192.168.1.105:1920"));7Host. AddServiceEndpoint (typeof(Ihomeservice),NewBasicHttpBinding (),"Homeservie");8Host. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true });9Host. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex");Ten host. Open (); One AConsole.WriteLine ("Home Service opens .... "); - - //The first one: This is the fly service the varHost2 =NewServiceHost (typeof(Flyservice),NewUri ("http://192.168.1.105:1930")); -Host2. AddServiceEndpoint (typeof(Iflyservice),NewBasicHttpBinding (),"Flyservie"); -Host2. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true }); -Host2. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex"); + Host2. Open (); - +Console.WriteLine ("Fly Service Open .... "); A at Console.read (); - } -}
Have you seen, now two services are open, this way seems not very good, otherwise, you need to open two host, so that my procedures are streamlined ... Right..
Three: Port sharing in TCP
This thing sounds like everyone knows, port sharing, is not two programs to share a port, right, under normal circumstances, we will certainly think this can not be done, in fact? In WCF we can still play
, that is, a portsharingenabled thing!!! If the port can be shared, then our service is not able to open a few ports it? Also it is convenient for us to service the tube
, let me give you a demonstration ... It's a lot of fun, huh, da?
As you can see, my two hosts are using 1920 ports, and now I really open up .... Well, three tricks are said, I think you in the real development of WCF, more or less can be connected
Touch, hope to you useful ~ ~ ~
15 days proficient in the fifth day of wcf--three tips you need to know