views:

90

answers:

3

When I try to connect to an unavailable machine, I get an EndpointNotFoundException but it takes too long (about 20 seconds). Setting closeTimeout, openTimeout, receiveTimeout or sendTimeout has no effect. Can I get that exception earlier?

+2  A: 

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.

villintehaspam
The problem is that the exception occurs only when I call any method. That way I would have to make a thread for each method call.
Eduardo
+1  A: 

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.

wallismark
A: 

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.

Eduardo