2014-07-23 59 views
0

我有一個vb.net應用程序,我應該處理Restful Web Service上的超時。 在Web.config配置文件中,我設置了「receiveTimeout」和「sendTimeout」屬性,但是我不明白如何在發生超時時執行Web服務端的某些操作。 基本上,如果我有一個超時,我應該對數據庫執行控制操作,總是在Web服務端。 我應該如何繼續? 這是將數據發送到Web服務代碼:如何處理Restful Web Services上的超時異常

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 
+0

向我們顯示您用於向您的網絡服務發送請求的代碼 – reptildarat

+0

我修改了消息... –

+0

您是什麼意思,「在Web服務端」? –

回答

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 

順便說一句,我不是測試代碼。

+0

好的,但如果它永久斷開連接(我斷開網絡電纜)。我使用「catch」來管理客戶端的超時,但在服務器端? SaveDataPost函數永遠不會被調用,因爲我沒有完全發送文件。我如何處理服務器端的超時異常? –