Is there a good way to check if a web service is available that does not involve simply calling a served method?
That is, I was looking for something like a .TryConnect() or .IsAvailable() method I could call before calling my .TransferData(data) method.
I've tried reading the State variable of the service, but it even while the service is throwing EndpointNotFoundExceptions, it's reporting itself in the "Opened" state, and not "Faulted" like I had hoped.
I'm using synchronous data calls, and the web service implements IDataTransferService, if that helps. Also--.NET 3.5.
The only reliable way is to actually invoke the method and retry if it fails. No matter what preliminary checks you do the service could report that it is running and go offline at the moment you call it.
No, there is no better way. Just call the simplest WebMethod. Some services provide a dummy method or GetVersion() more or less for this purpose.
And then catch the TimeOut exception.
You can attempt to make a WebRequest that checks the wsdl of the web service. It's not a guarantee, but in most cases, if the service will serve the wsdl it'll serve the function as well. Here's one I wrote to test basic availability of a service.
Dim _streamReader As StreamReader
Dim responseString As New StringBuilder()
_streamReader = New StreamReader(Me.Response.GetResponseStream())
responseString.Append(_streamReader.ReadToEnd())
_streamReader.Close()
_streamReader = Nothing
If responseString.ToString().Contains("<wsdl:definitions") AndAlso _
responseString.ToString().Contains("</wsdl:definitions>") Then
wsdlVerified = True
Else
Throw New Exception("The response did not generate valid wsdl.")
End If
Where the properties for this class serve the code above:
Public Property Url() As String
Get
Return _url.Trim()
End Get
Set(ByVal value As String)
_url = value.Trim()
End Set
End Property
Public ReadOnly Property Request() As System.Net.HttpWebRequest
Get
If _request Is Nothing AndAlso Me.Url.Trim.Length > 0 Then _
_request = CType(System.Net.HttpWebRequest.Create(Me.Url & "?wsdl"), _
System.Net.HttpWebRequest)
Return _request
End Get
End Property
Public ReadOnly Property Response() As System.Net.HttpWebResponse
Get
If _response Is Nothing AndAlso Me.Url.Trim().Length > 0 Then _
_response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
Return _response
End Get
End Property
EDIT: I'd refactor this into C# but it is fairly basic and should translate easily.
We are using a cached proxy client in one of our projects. In order to guarantee that the connection stays alive, we've created a simple Ping Operation in the Service. We call this method every x seconds, and report in the log if the Ping operation failed. You can expand this to also find the reason for the fail - timeouts or communication errors would indicate service unavailable. Of course this method is only possible when you have both service and client under your control.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.