How to find bad endpoints earlier?
When I try to connect to an unavailable machine, I get an EndpointNotFoundExc开发者_如何学运维eption but it takes too long (about 20 seconds). Setting closeTimeout, openTimeout, receiveTimeout or sendTimeout has no effect. Can I get that exception earlier?
We're using the EnterpriseLibrary ConnectionMonitor block which works reasonably well, with it you can define your own strategy to determine connection availability but we're just using the one that comes in the box.
That default code (which does run on a background thread...handled by the block) uses this internally:
public bool IsAlive(string hostnameOrAddress)
{
bool alive = false;
try
{
Uri address = new Uri(hostnameOrAddress);
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(address);
request.Timeout = 5000;
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
alive = DoesResponseStatusCodeIndicateOnlineStatus(response);
}
}
catch (WebException wex)
{
alive = DoesWebExceptionStatusIndicateOnlineStatus(wex);
}
return alive;
}
Have a look at the block itself, see if you can use it in your project or get some ideas on how to solve your specific problem. (from memory the block is part of the SCSF Smart Client Software Factory)
My understanding though is that you will need to do some kind of background threading OR wait for the timeout yourself. WCF doesn't know the endpoint doesn't exist until the call times out.
You can always try to connect in a separate thread, and have a timer with your desired timeout on the main thread. If your worker thread does not complete within your timeout, then you can assume that it won't respond.
The solution found is a mix of villintehaspam and wallismark's answers. I've created a local method that calls the remote method void IsOk() asynchronously and waits for some seconds. If it takes too long I abort the thread.
精彩评论