我有一个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服务端”? –