When the remoting client uses tcpchannel to connect to the server, if the address does not exist, it will try to connect to the timeout for about 21 seconds, for example, connecting to "TCP ://192.192.192.192: 8080/serverobject "This does not exist remoting service address. How can we shorten the remoting timeout time? According to the msdn channel properties Description: The default timeout setting is infinite (unlimited), and the default number of failed attempts/retrycount is one. So naturally, set the remoting client to use the tcpchannel to link the server's connection timeout in the timeout parameter,CodeYes:
The following code calls the instance method (defined in imyservicecontract ).
However, the test found that the set timeout parameter did not work, and the timeout was still 21 seconds.
After I searched the internet, it was indeed a common problem. I checked this and this post, but unfortunately it did not solve my problem. There is no good way on the Internet, but I have to do it myself. Use task. Wait (timespan) and cancel in. Net 4.0 TPL.
Use task. Wait and cancel to solve the remoting timeout Problem
Let's take a look at the following code:
I think you must understand it. It means that the thread pool starts a thread and does something (this is assumed to take 4 seconds), but it must be within the timeout time (3 seconds) when the timeout time is reached, the thread task is canceled and false is returned. If the operation is completed within 3 seconds, true is returned for a long time. It's easy.
(Note: Cancelationtokensource. Cancel () is used to cancel a thread task, and the task. Wait (timespan) in TPL is used in combination. In this way, when the timeout time is reached, the thread task is canceled. See msdn: Task cancelation and how to: cancel a task and its children. details: If you only call bool wait (timespan timeout), the task will not stop when the time is reached. Instead, the system will return false if the time used exceeds timespan, otherwise, true is returned .)
This method is used to test whether a remote address is available, for example, connecting to "TCP ://192.192.192.192: 8080/serverobject "the nonexistent remoting service address. If it can be returned within the timeout time (for example, within 3 seconds), the test is successful. The server address that fails to be exceeded. You can place time-consuming operations in the do some task area above, such as activator. GetObject or instance. dosomething ()-be sure to use try-catch to enclose them.
Pay attention to channelservices. unregisterchannel
Pay attention to the channelservices. unregisterchannel. If it is placed in the block of try-catch, it may not work and it will not work. For more information, see the following code:
The reason is that if you connect to "TCP ://192.192.192.192: 8080/serverobject "the nonexistent remoting service address may be in activator. getObject or instance. dosomething () encountered a connection failed exception, prompting that the remote remoting server could not be connected, and then went to catch to call channelservices. unregisterchannel? No, there is retrycount, retry upon failure, so the channel is not logged out.
The correct solution is elsewhere, or call channelservices. unregisterchannel later. Or, after my function tests whether a remote address can be called by testremotingserveraddress, if it is false, call channelservices. unregisterchannel. In addition, if the endpoint address is changed, call channelservices. unregisterchannel. Otherwise, an exception occurs.