This article reposted from sharping's nonsense http://www.sharping.net/CommentView,guid,9592435d-5496-45f4-a80e-d0a121d454cf.aspx
Introduction: if you are a technology-loving person, I believe you can't wait to write code after reading the previous articles, you must have discovered some problems in rremoting that I did not mention. For details, see the details. Today we will discuss the implementation of multi-channel registration, one of the details in remoting.
There are often such requirements that we need to complete two different services in two different channels, so let's review the content mentioned above and register the channel:
Tcpchannel channel = new tcpchannels (8080 );
Channelservices. registerchannel (Channel );
What about multiple channels? The general thinking mode should be implemented as follows:
Tcpchannel channel = new tcpchannels (8080 );
Channelservices. registerchannel (Channel );
Tcpchannel channel = new tcpchannels (9999 );
Channelservices. registerchannel (Channel );
The result is unexpected. Such a statement may fail during runtime check and can be compiled through.
After reading the relevant information, we found that remoting is used to identify the channel as the name attribute from the tcpchannel rather than the port number. That is to say, each channel must have different name attributes, in theory, you can assign a value to the name attribute of a tcpchannel instance before registering the instance. However, on msdn, we checked the name attribute of the tcpchannel class and found that this name attribute only has the get accesser, that is, it is read-only. Dizzy ~~~ It seems that the problem still needs to be traced back.
By referring to msdn, we can find that tcpchannel has three heavy-load constructor methods. One of them is our above form, there is no port specified, there is a lot of parameters, let's look at his prototype:
Public tcpchannel (
Idictionary properties,
Iclientchannelsinkprovider clientsinkprovider,
Iserverchannelsinkprovider serversinkprovider
);
Parameter description:
Properties
The idictionary that saves the channel attributes of the current channel configuration information.
Clientsinkprovider
Iclientchannelsinkprovider, which creates a client channel for receiving the basic tcpclientchannel that remotely processes messages.
Serversinkprovider
Iserverchannelsinkprovider, which creates a server channel for receiving the basic tcpserverchannel that remotely processes messages.
If you have good programming habits, you will know that the type of I headers is the interface type. You can view the information on. in net, the hashtable class implements the idictionary interface. The binaryclientformattersinkprovider class implements the iclientchannelsinkprovider interface, and the binaryserverformattersinkprovider class implements the iserverchannelsinkprovider interface. As for the next two classes, you can use the default constructor to create instances and use these two instances as the following two parameters, because they only provide binary formatting receivers. For details, refer to msdn, the channel attributes are differentiated by the first parameter idictionary.
Let's take a look at the hashtable class, which is described in msdn as follows: a set of key/value pairs. These key/value pairs are organized according to the key hash code. Key? Value? Does it sound like a database term? Yes, they are actually the same concept to some extent. The so-called key can be understood as a field, and the value can be understood as the value of this field (I can't think of a word, but I can only use text e to explain Chinese ). In fact, its implementation principle is that the hashtable class provides us with a string-type indexer for both the return type and parameter type, so that we can create some key/value pairs by ourselves. Think about it, whether the name attribute of the tcpchannel class corresponds to a string (string type). Here name is the key, and this string is the value. In addition, the tcpchanne class also has its port attribute. Create an instance of the hashtable class as follows:
Hashtable instance = new hashtable ();
Tcpprop ["name"] = "tcp9090 ";
Tcpprop ["Port"] = 9090;
As mentioned above, the hashtable class implements the idictionary interface, so the program should be like this:
Idictionary ichannel = new hashtable ();
Tcpprop ["name"] = "tcp9090 ";
Tcpprop ["Port"] = 9090;
In this way, the first parameter is constructed, and the multi-channel problem can be solved. Let's take a look at the complete code to register an HTTP channel and a TCP channel.
Register the TCP channel:
Idictionary tcpprop = new hashtable ();
Tcpprop ["name"] = "tcp9090 ";
Tcpprop ["Port"] = 9090;
Ichannel channel = new tcpchannel (tcpprop,
New binaryclientformattersinkprovider (),
New binaryserverformattersinkprovider ());
Channelservices. registerchannel (Channel );
Register an HTTP channel:
Idictionary httpprop = new hashtable ();
Httpprop ["name"] = "http8080 ";
Httpprop ["Port"] = 8080;
Ichannel channel = new httpchannel (httpprop,
New soapclientformattersinkprovider (),
New soapserverformattersinkprovider ());
Channelservices. registerchannel (Channel );
The problem is solved here, but I think it is necessary to study what went wrong with our code at the beginning.
Tcpchannel channel = new tcpchannels (8080 );
Channelservices. registerchannel (Channel );
This registration method only changes port parameters. Why cannot it implement multiple channels? The name attribute of a tcpchannel instance created in the new tcpchannel (8080) mode is "TCP" by default, and the name attribute of an httpchannel instance created in the new httpchannel (8080) mode is "HTTP ", the two channel names are both "TCP", which violates the principle of using the channel name as the channel identification, and of course fails. The article is complete.
Note: "Details determine success or failure! "Details are often the most difficult to grasp in learning. If the difference between a master and a cainiao lies in programming ideas, you can say that, the main difference between a master and a cainiao in programming technology is the mastery of details, so you are also a "little master ".