2014-07-23 36 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函数永远不会被调用,因为我没有完全发送文件。我如何处理服务器端的超时异常? –