捕获错误或异常

时间:2009-11-07 07:46:56

标签: asp.net google-maps weather mashup

我构建了一个谷歌地图和weather.com的混搭,每次其中一个服务器没有响应我的应用程序也挂起。您认为我可以做什么来防止或最小化挂起我的网络应用程序?挂起来像你无法离开那个页面.... 我在我的应用程序代码上获得了此代码以访问气象服务;


Public Class WeatherIn
    Private _path As String
    Private _cachedFile As String
     Public Sub New(ByVal path As String)
        _path = path
        _cachedFile = String.Format("{0}\WeatherInCache.xml", _path)
    End Sub

Public Function GetWeather(ByVal arg As String) As String

    Return _getWebWeather(arg)

End Function

Private Function _getCachedWeather() As String
    Dim str As String = String.Empty


    Using reader As New StreamReader(_cachedFile)
        str = reader.ReadToEnd()
    End Using


    Return str
End Function

Private Function _getWebWeather(ByVal arg As String) As String

    Dim baseUrl As String = "http://xoap.weather.com/weather/local/{0}?cc=*&dayf=5&link=xoap&prod=xoap&par={1}&key={2}"
    Dim jane As String = arg
    Dim james As String = "api key"
    Dim john As String = "another api key"

    Dim url As String = String.Format(baseUrl, jane, james, john)


    Using client As New WebClient()

        Try

            Dim xml As New XmlTextReader(client.OpenRead(url))


            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Using writer As New StreamWriter(_cachedFile)
                xslt.Transform(xml, Nothing, writer)
            End Using


            Return _getCachedWeather()
        Catch exception As WebException

            Dim xmlStr As String = "<errorDoc>"
            xmlStr += "<alert>An Error Occurred!</alert>"
            xmlStr += [String].Format("<message>{0}</message>", exception.Message)
            xmlStr += "</errorDoc>"


            Dim doc As New XmlDocument()
            doc.LoadXml(xmlStr)


            Dim reader As New XmlNodeReader(doc)

            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Dim resultDocument As New XmlDocument()
            Using writer As XmlWriter = resultDocument.CreateNavigator().AppendChild()
                xslt.Transform(reader, DirectCast(Nothing, XsltArgumentList), writer)
            End Using

            Return resultDocument.OuterXml
        End Try
    End Using
End Function

然后我在我的页面上使用了上面的课程,我在这里显示天气:

Public Function GetWeather(ByVal arg As String) As String

    Return _getWebWeather(arg)

End Function

Private Function _getCachedWeather() As String
    Dim str As String = String.Empty


    Using reader As New StreamReader(_cachedFile)
        str = reader.ReadToEnd()
    End Using


    Return str
End Function

我检索数据并通过javascript写在页面上。大多数时间它的weather.com下降。我没有问题谷歌地图它可靠....任何人都有一个解决方案为什么我的页面挂起如果远程服务器没有响应,也会启动?如果远程服务器正在响应,则mashup会顺利运行...

1 个答案:

答案 0 :(得分:0)

当依赖外部Web服务时,最好以异步方式加载,这样如果其中一个很慢,您可以向页面查看器显示某种加载微调器。如果失败,您的页面可能只是报告从Web服务器读取失败并稍后再试。

在这种情况下,我会使用Google地图加载页面,然后针对天气数据发出AJAX请求。