如何处理Restful Web Services上的超时异常

时间:2014-07-23 07:35:00

标签: vb.net web-services rest

我有一个vb.net应用程序,我应该在Restful Web服务上处理超时。 在Web.config配置文件中,我设置了属性“receiveTimeout”和“sendTimeout”,但我无法理解的是如果发生超时,如何在Web服务端执行某些操作。 基本上,如果我有超时,我应该在数据库上执行控制操作,总是在Web服务端。 我该怎么办? 这是将数据发送到Web服务的代码:

Private Function SendActivityToWEBSERVICE_POST(ByVal xmlFile As String) As Boolean
Try
    sUri = "http://localhost:35299/WS/SincronizzaAttivita"

    Dim encoding As ASCIIEncoding = New ASCIIEncoding()
    Dim data() As Byte = encoding.GetBytes(xmlFile)

    Dim webrequest As HttpWebRequest = Net.WebRequest.Create(sUri)
    webrequest.Method = "POST"
    webrequest.ContentType = "text/xml"
    webrequest.ContentLength = data.Length
    Dim newStream As Stream = webrequest.GetRequestStream()
    newStream.Write(data, 0, data.Length)
    newStream.Close()

    Dim webresponse As HttpWebResponse = webrequest.GetResponse()
    Dim stIn As IO.StreamReader = New IO.StreamReader(webresponse.GetResponseStream())
    Dim strResponse As String = stIn.ReadToEnd

    Dim xmlDoc As XDocument = New XDocument()
    xmlDoc = XDocument.Parse(strResponse)
    Return xmlDoc.Root.Value

Catch ex As Exception
    Console.WriteLine("Eccezione " + ex.Message)
    WriteToErrorLog(ex.Message, Environment.StackTrace, "Error")
    Return False
End Try
End Function

使用此功能,我向Web服务发送大文件。我希望在发送数据失败的情况下处理Web服务端的超时,例如在网络连接出现问题的情况下。

这是界面:

<OperationContract>
<WebInvoke(Method:="POST",
           RequestFormat:=WebMessageFormat.Xml,
           ResponseFormat:=WebMessageFormat.Xml,
           BodyStyle:=WebMessageBodyStyle.Bare,
           UriTemplate:="SincronizzaAttivita")>
Function SaveDataPost(sXMLFile As Stream) As Boolean

1 个答案:

答案 0 :(得分:0)

您可以在webrequest上添加超时属性,如下所示:

webrequest.Timeout = 200000

它会抛出超时错误,你可以抓住它。

或者您可以自己构建计时器,如果它已经超时,您可以做一些事情:

Dim boolTimeOut as boolean = false

Private Function SendActivityToWEBSERVICE_POST(ByVal xmlFile As String) As Boolean
Try

    Dim MyTimer As New Timer

    MyTimer.Interval = 600000 'set your time-out...
    AddHandler MyTimer.Elapsed, AddressOf DisplayTimeEvent ' create handler to manage the timer...
    MyTimer.Start() 'start the timer...
    MyTimer.AutoReset = False

    sUri = "http://localhost:35299/WS/SincronizzaAttivita"

    Dim encoding As ASCIIEncoding = New ASCIIEncoding()
    Dim data() As Byte = encoding.GetBytes(xmlFile)

    If boolTimeOut = False Then ' if it's already time out, and do something....
        Dim webrequest As HttpWebRequest = Net.WebRequest.Create(sUri)
        webrequest.Method = "POST"
        webrequest.ContentType = "text/xml"
        webrequest.ContentLength = data.Length
        Dim newStream As Stream = webrequest.GetRequestStream()
        newStream.Write(data, 0, data.Length)
        newStream.Close()

        Dim webresponse As HttpWebResponse = webrequest.GetResponse()
        Dim stIn As IO.StreamReader = New IO.StreamReader(webresponse.GetResponseStream())
        Dim strResponse As String = stIn.ReadToEnd

        Dim xmlDoc As XDocument = New XDocument()
        xmlDoc = XDocument.Parse(strResponse)
        Return xmlDoc.Root.Value
    Else
        ' Do your thing when it's timeout in here...
    End If

Catch ex As Exception
    Console.WriteLine("Eccezione " + ex.Message)
    WriteToErrorLog(ex.Message, Environment.StackTrace, "Error")
    Return False
End Try
End Function


Public Sub DisplayTimeEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    boolTimeOut = True
End Sub
是的,我没有测试代码。