如果WebClient返回异常,WCF服务会超时

时间:2012-02-24 15:58:25

标签: wcf silverlight exception-handling webclient

我有一个WCF服务作为代理,调用WebClient返回一个字符串。 WebClient正在查询令牌服务,其中用户名和密码通过https发送。如果用户名和密码正确,则该服务正常工作,但是如果用户名和密码无效,则WebClient会抛出异常(403 Forbidden),该异常在下面的代码中是预期和处理的。然而,WCF服务然后继续挂起,直到它超时,我无法弄清楚原因。

Public Function GetToken(un As String, pw As String, ref As String) As TokenResult Implements IAGSAuthentication.GetToken

        Dim Token As String
        Dim TokenURL As String = String.Format("https://server/arcgisserver/tokens?request=getToken&username={0}&password={1}&timeout={2}", un, pw, Timeout)
        Dim tokenResult As TokenResult = New TokenResult
        If TokenService.IsBusy = False Then

            Try
                Token = TokenService.DownloadString(New Uri(TokenURL))
                tokenResult.Token = Token
                Return tokenResult
                Exit Function
            Catch ANEx As ArgumentNullException
                TokenService.Dispose()
                tokenResult.TokenException = ANEx
                Return tokenResult
                Exit Function
            Catch WEx As WebException
                TokenService.Dispose()
                tokenResult.TokenException = WEx
                Return tokenResult
                Exit Function
            Catch CEx As CommunicationException
                TokenService.Dispose()
                tokenResult.TokenException = CEx
                Return tokenResult
                Exit Function
            Catch Ex As Exception
                TokenService.Dispose()
                tokenResult.TokenException = Ex
                Return tokenResult
                Exit Function
            End Try

        End If
        Return tokenResult
    End Function

我还应该补充说,当我调试WCF参考文件时,在自动填充的客户端方法中显示异常。

Public Function EndGetToken(ByVal result As System.IAsyncResult) As AuthServiceRef.TokenResult Implements AuthServiceRef.AuthService.EndGetToken
                Dim _args((0) - 1) As Object
                Dim _result As AuthServiceRef.TokenResult = CType(MyBase.EndInvoke("GetToken", _args, result),AuthServiceRef.TokenResult)
                Return _result
            End Function

我的想法是,我认为通过处理WCF服务中的异常并在自定义类中返回异常,我可以将异常推送给用户而没有运行时问题。以下是客户端捕获的异常:

System.ServiceModel.CommunicationException was unhandled by user code

Message =远程服务器返回错误:NotFound。   堆栈跟踪:        在System.ServiceModel.AsyncResult.End [TAsyncResult](IAsyncResult结果)        在System.ServiceModel.Channels.ServiceChannel.EndCall(String action,Object [] outs,IAsyncResult result)        在System.ServiceModel.ClientBase 1.ChannelBase 1.EndInvoke(String methodName,Object [] args,IAsyncResult result)        在MatrixWebMap.AuthServiceRef.AuthServiceClient.AuthServiceClientChannel.EndGetToken(IAsyncResult结果)        在MatrixWebMap.AuthServiceRef.AuthServiceClient.AuthServiceRef_AuthService_EndGetToken(IAsyncResult结果)        在MatrixWebMap.AuthServiceRef.AuthServiceClient.OnEndGetToken(IAsyncResult结果)        在System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult结果)   InnerException:System.Net.WebException        Message =远程服务器返回错误:NotFound。        堆栈跟踪:             在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)             在System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)             at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)        InnerException:System.Net.WebException             Message =远程服务器返回错误:NotFound。             堆栈跟踪:                  在System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)                  在System.Net.Browser.BrowserHttpWebRequest。<> c_ DisplayClassa.b _9(Object sendState)                  在System.Net.Browser.AsyncHelper。<> c_ DisplayClass4.b _0(Object sendState)             InnerException:

1 个答案:

答案 0 :(得分:0)

因此,从我收集的事件中,当我将异常添加到TokenResult类并返回它时,WCF响应出现了exception属性的问题。解决方法是在响应之前处理异常,并使TokenException成为在返回结果之前创建的字符串。

因此,对于'403 Forbidden'网络异常,我只是构造了一个字符串并返回“”您输入的信息与我们的记录不符。请再试一次。“有点肮脏的工作,但它非常适合我在这里尝试做的事情。

            Try
                Token = TokenService.DownloadString(New Uri(TokenURL))
                tokenResult.Token = Token
                Return tokenResult
                Exit Function

            Catch ANEx As ArgumentNullException                
                tokenResult.TokenException = ANEx.Message
                Return tokenResult
                Exit Function

            Catch WEx As WebException                   
                If WEx.Status = WebExceptionStatus.ProtocolError Then
                    tokenResult.TokenException = "The information you have entered does not match our records. Please try again."
                Else
                    tokenResult.TokenException = WEx.Message
                End If
                Return tokenResult
                Exit Function

            Catch CEx As CommunicationException      
                tokenResult.TokenException = CEx.Message
                Return tokenResult
                Exit Function

            Catch Ex As Exception                    
                tokenResult.TokenException = Ex.Message
                Return tokenResult
                Exit Function
            End Try
相关问题