开发者

Fastest way to check if WCF endpoint is listening

开发者 https://www.devze.com 2023-03-18 12:01 出处:网络
Often, if a WCF endpoint is unavailable (in my case, usually because the service host is not running), I\'ll get an EndpointNotFoundException after a timeout.I\'d like to have a fast way to query the

Often, if a WCF endpoint is unavailable (in my case, usually because the service host is not running), I'll get an EndpointNotFoundException after a timeout. I'd like to have a fast way to query the service to see if it's available without having to rely on the normal timeout. In other words, I want to keep a normal timeout for normal circumstances, b开发者_JS百科ut for a quick "ping" of the endpoint, I want it to fail fast if it's not available right away.

How could this be accomplished?


You will have to wait for a TimeOut exception. You can set (override) the TimeOut when creating the Proxy object. They're cheap so make a temp proxy for the Ping.

On the server side, you could make sure there is a lightweight function to call (like GetVersion).


To check availability, you can try connecting to host thru Socket Connection like this (its vb.net 2.0 code should work in WCF too)

Dim sckTemp As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    sckTemp.ReceiveTimeout = 500 : sckTemp.SendTimeout = 500

    Try
        '' Connect using a timeout (1/2 second)
        Dim result As IAsyncResult = sckTemp.BeginConnect("Host_ADDRESS", YOUR_SERVER_PORT_HERE, Nothing, Nothing)
        Dim success As Boolean = result.AsyncWaitHandle.WaitOne(500, True)
        If (Not success) Then
            sckTemp.Close() : Return False
        Else
            Return True
        End If
    Catch ex As Exception
        Return False
    End Try

It will give you Server status in 1/2 second


It's the SendTimeout you want to change. In my particular case it's a FedEx package rating service which incidentally always seems to be down Friday night. You will probably have to carefully consider the timeout value depending upon how important false negatives (so the service being down) are.

rateService.Endpoint.Binding.SendTimeout = TimeSpan.FromSeconds(.5);

This value will just affect the WCF client and won't permanently change the timeout for that service.

0

精彩评论

暂无评论...
验证码 换一张
取 消