The listener address of the "WCF" endpoint

Source: Internet
Author: User

The main purpose of the endpoint is to expose some information portals to the client, through which you can find the service operation to invoke. Usually, the endpoint uses three elements to express, I remember Lao Jiang (Artech, who can find him in the garden) in his book on WCF, the three elements are called "ABC".

A is address, that is, the endpoint, B is binding, binding, describing the transport protocol, whether security mode is enabled, and C is contract, the service contract.

A service contract can be exposed by multiple endpoints , such as an endpoint that might use the HTTP protocol, another using TCP, and so on.

Is WCF really as complicated as some people say it's hard to learn? According to the old week's low see, is a little more difficult than other aspects of things, mainly the content involved in a wide range, but, it is not as scary as some rumors say. Judging from the inferior level of the old week, it must be more studious than C + + (of course, that's not the same thing, C + + is practicing). The process of programming learning will inevitably encounter difficulties, which is inevitable phenomenon, you can not escape, if you want to escape, I suggest you still learn how to be covered by the wealthy.

If you want to learn something in your life, and you can do something, you can brag about it in front of your offspring, no matter what you learn or what you do, it won't be easy. You want to really want to get something for nothing, want to have a relaxed, that line, find a big bag to raise you, the world to do anything is bitter, only to be the most happy to be a cheap person.

Referring to C + +, Nature also includes C, I think of a thing: often people ask, 0 basic programming what, I can definitely answer you, starting from C. Whether you use C in the future, you should go to school, the foundation does not play well, you will pay the price later.

OK, p is a lot of words. If you're brave enough to learn about WCF, come and listen to the old week's learning methods. In fact, when the old weeks encounter difficult to learn things, the first thing is to throw all kinds of theories and concepts aside, and ignore their existence, and then focus on the code, anyway, forget what it is, learn how to write code, and then go back to see those concepts Ah, theory ah what. Anyway, the old weeks do not like the concept of things, said iffy and Xuan, see do not understand, also deliberately installed force, make even the semantics are not smooth, read up also special awkward.

Those who make a theory seem to like showing off. Old weeks always like simple and easy to understand things, it is better to say even a small doll can understand, it is called a good, is not it? People like Albert Einstein were all learning to try to tell an old lady about his research.

Alas, another section F, now into the theme, today we talk about the endpoint's listening address.

Typically, when we define an endpoint, we specify an address through the Address property, where the client can find the corresponding service operation in order to communicate with the server. This is like you let your friends come to your house to play, you have to tell him where you live, as long as there is an address, even if he took a mobile phone with GPS can also find you, otherwise, you let him send a missing notice to find you?

To set the Address property of an endpoint directly, you must specify a valid real address, and if a base is specified in the service, the endpoint can use a relative address, provided that the endpoint's address is the same as the protocol for the base site, for example, HTTP.

Protocol, host (port) must be real, as for the following path, can be casually, such as:

http://192.168.1.7:8888/worker/sendout/

http://192.168.1.7:8888/feedback/

HTTP is the protocol, to be clear; 192.xxx.xxx.xxx:8888 is the host and port, to be true, otherwise the client cannot find it. And behind the/.../. Can be arbitrarily specified, these paths are used only to identify endpoints.

However, you will notice that the endpoint has another property called ListenUri, what is this? This is the address that the server uses to listen for client requests and must be the actual address. So, what is the relationship between the listening address and addressing?

If only address is specified and no ListenUri is specified, the address must be the real one, and the server will receive the client connection with address as the listening address. That is, address and ListenUri are the same.

If ListenUri is explicitly specified, the service listens for client connections at this address, which can either use a real address or a non-real address. In this case, the address is a logical location and is used only to identify the endpoint.

For example, I set the ListenUri as: http://DogPC:80/in/,

The address can then be http://DogPC:1122/wang_wang/, or it can be: http://fuck.org. At this time, address can not exist, not believe you online search, which site will be called fuck.org.

However, you have to be aware that even if the address is a logical location, its protocol must be the same as the ListenUri protocol. If the listener address is HTTP, then the logical address is HTTP, if the listener address is net.tcp://www. Com/getcue, you set the logical address to http:// Mouse.net, this is not possible, because the protocol to listen to the address is net.tcp, so the logical address must also be the NET.TCP protocol.

As a result, the listening address becomes a "transit point" where the service listens for connections, receives it once a client requests it, and then does not process the received message, which dispatches the message to a dedicated channel. Plainly speaking is: The listening address is only responsible for receiving letters, not responsible for eagerness reading letters and replies .

This let old week reminds of college, our school has a special point (ListenUri), courier to the unified to this point, there are teachers and students on duty, unified collection, and then will call a classmate, or to a dormitory room to inform you, there is express, you go to get it.

If, the old week was ambitious to go to graduate school, so with several classmates, in the online book a few review material. After the book arrived, The courier will get that point, where there are teachers dedicated to the collection, but the teacher is only responsible for the collection, as for the inside of what is not the case. Then the teacher will inform the old week, please go to the lobby to pick up and receive the office. So the old week rode nine years ago to buy bicycles, flew to the hall, retrieve the objects, back to the dorm and then unpacking.

Now, let's look at an example, the definition of service contracts and service classes is not going to be made, and these are all understood, let's look at the server's endpoint configuration.

      <Servicename= "SV">        <EndpointAddress= "http://supperman.org"Contract= "Add"ListenUri= "Http://localhost:6000/input"Listenurimode= "Explicit"binding= "netHttpBinding"/>        <EndpointAddress= "http://supperegg.org"Contract= "Sub"binding= "netHttpBinding"ListenUri= "Http://localhost:6000/input" />      </Service>

Supperman.org and supperegg.org are fake address, no this domain name, the reason that address can be configured so, is because I set the two endpoint ListenUri, the listening address must be true. By the way, Listenurimode best to use the default value, that is, explicit, which is convenient for the client configuration, if the value is unique, will be added after the listening address a GUID as a suffix, the key is that the GUID of each run service will be regenerated, each time is different, This is not convenient for client configuration. Of course, it is also useful, if you want to distinguish between two addresses, you can make it unique, or you simply change a port.

As you can see, two endpoints use the same listening address. ListenUri can be shared with multiple endpoints because it is only responsible for receiving messages and not processing messages, it only acts as a forwarding function. However, address is not the same, because it is used as the identity of the endpoint, not to mention the previous ABC, if the two endpoints of the ABC are the same, it doesn't make sense.

On the client side, the address, binding, and contract of the endpoint are also matched to the server.

    <Client>      <Endpointname= "Epadd"Address= "http://supperman.org"Contract= "Add"binding= "netHttpBinding"behaviorconfiguration= "BV"/>      <EndpointAddress= "http://supperegg.org"Contract= "Sub"binding= "netHttpBinding"name= "Epsub"behaviorconfiguration= "BV"/>    </Client>

Since the address has been a logical address instead of a real location on the server just now, it must be configured with behavior, adding Clientvia behavior to specify the actual address.

      < endpointbehaviors >        <  name= "BV">          <viauri= "http ://localhost:6000/input "/>        </behavior>      </ endpointbehaviors >

Viauri must fill in the service's listening address.

You can then try to invoke it.

            using(Channelfactory<commoncontracts.iaddcompute> Fac1 =NewChannelfactory<commoncontracts.iaddcompute> ("Epadd"))            using(Channelfactory<commoncontracts.isubcompute> FAC2 =NewChannelfactory<commoncontracts.isubcompute> ("epsub") {Commoncontracts.iaddcompute Channel1=Fac1.                CreateChannel (); Commoncontracts.isubcompute Channel2=Fac2.                CreateChannel (); intR1 = Channel1. ADD (1,5); intr2 = Channel2. Sub (9,6); Console.WriteLine ($"results 1:{r1}\n results 2:{R2}");            Console.read (); }

Now, let's compare.

Look at the request message sent by the client first.

Then, look at the request message received by the server.

Two message content, but you look carefully, you will find that the server received the message after the message changes, add a to message header, the content of the message header and the endpoint's logical address is the same, this is equivalent to the listening address received a request message, It is then forwarded to the channel corresponding to the endpoint for processing.

Well, today's rotten text finished, how, not difficult.

Sample source Code

The listener address of the "WCF" endpoint

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.