2012-02-24 52 views
0

我有一个WCF服务作为调用WebClient返回字符串的代理。 WebClient正在查询通过https发送用户名和密码的令牌服务。如果用户名和密码正确,该服务就可以正常工作,但如果用户名和密码无效,则WebClient会抛出一个预期并在下面的代码中处理的异常(403 Forbidden)。然而,WCF服务然后继续挂起,直到它超时,我不知道为什么。如果WebClient返回异常,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 

消息=远程服务器返回一个错误:NotFound。 堆栈跟踪:在System.ServiceModel.Channels.ServiceChannel.EndCall 在System.ServiceModel.AsyncResult.End [TAsyncResult](IAsyncResult的结果) 在System.ServiceModel.ClientBase 1.ChannelBase(字符串动作,对象[]奏,IAsyncResult的结果) 1.EndInvoke(字符串方法名,对象[]指定参数时,IAsyncResult的结果) 在MatrixWebMap.AuthServiceRef.AuthServiceClient.AuthServiceClientChannel.EndGetToken(IAsyncResult的结果) 在MatrixWebMap.AuthServiceRef.AuthServiceClient.AuthServiceRef_AuthService_EndGetToken(IAsyncResult的结果) 在MatrixWebMap.AuthServiceRef.AuthServiceClient .OnEndGetToken(IAsyncResult result) at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result) Inne rException:System.Net.WebException Message =远程服务器返回错误:NotFound。 堆栈跟踪: 在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态) 在System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult) 在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest。 CompleteGetResponse(IAsyncResult结果) InnerException:System.Net.WebException Message =远程服务器返回一个错误:NotFound。 Stack.Trace: at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest。 <> c_ DisplayClassa.b _9(Object sendState) at System.Net.Browser.AsyncHelper。 <> C_ DisplayClass4.b _0(对象sendState) 的InnerException:

+0

也许是因为你处置TokenService? – 2012-02-24 16:22:09

+0

注释掉处理方法并没有改变任何东西。 – BrokenRobot 2012-02-24 17:12:16

+0

什么是例外? – 2012-02-24 17:14:18

回答

0

所以,从我收集了什么时,我异常添加到TokenResult类并返回它的WCF响应曾与异常性的问题。解决方法是在响应之前处理异常,并使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 
+0

我认为它是因为WCF响应以xml格式返回给客户端,并且看到异常是作为一个非常规异常进来的,因为它作为异常类返回。所以处理它的服务器端和返回一个字符串可能是一个很好的方法来解决这个问题。 – BrokenRobot 2012-02-27 17:07:51