我有一個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
向我們顯示您用於向您的網絡服務發送請求的代碼 – reptildarat
我修改了消息... –
您是什麼意思,「在Web服務端」? –