2009-02-26 15 views
4

ASP.Net asmx Webサービスを開発中です。クライアント側では、サーバーへのリクエストがHTTP 500のようなHTTPエラーコードを返した場合、どのようにWebサービスクライアント側から知ることができますか(Web参照の追加を使用して自動的に生成されたクライアントプロキシを使用します)。事前に非同期Webサービス呼び出しのエラーを確認する方法

おかげで、 ジョージ

+0

は、非同期呼び出しに関して具体的に質問しているタイトル/説明に記載する価値があります。 –

+0

同じ問題がある場合、HTTP 500応答で例外はスローされません。 – Lenny

答えて

0

あなたはMSDNから、以下の方法で、あなたのWebサービスのトレースを設定することができます。

http://msdn.microsoft.com/en-us/library/bb885203.aspx

あなたはまた、サーバーへのアクセス権を持っている場合は、 HealthMonitoringを設定することができます。たとえば、内部サーバーエラー500を送信したように、サーバー側で発生したエラーを記録します。

ヘルスモニタリング - リモートまたはサーバーにログインできるかどうhttp://msdn.microsoft.com/en-us/library/ms998306.aspx

はまた、これまでに便利なイベントビューアを持っています。このことができます

希望:

アンドリュー

+0

こんにちはAndrew、あなたのソリューションはサーバー側ですが、私はクライアント側で追跡したいと思います。 – George2

0

あなたはe.Response.GetResponseStreamから情報を取得することができます()。読んだところで、より完全な情報を得るためには、サーバー側を見る必要があります。

+0

良いアイデア!しかし、完全なイベントハンドラで非同期Webサービスコールを使用しているので、どのようにしてResponseオブジェクトを取得できますか? – George2

0

Webサービスでは、処理されない例外が発生したときにHTTP 500(内部サーバーエラー)メッセージを送信する傾向があるため、これはよく発生する問題です。私は長いことを発見したトリックを使用します。基本的には、StreamReaderを使用してWebExceptionをドリルダウンして、例外の根本原因を特定する必要があります。

サンプルコード:(申し訳ありませんが、便利な任意のC#のコードを持っていなかったコンバータを使用してください。)

Try 
    'Hit the webservice. 
Catch ex As WebException 
    Dim r As HttpWebResponse = CType(ex.Response(), HttpWebResponse) 
    Using sr As StreamReader = New StreamReader(r.GetResponseStream()) 
    Dim err As String = sr.ReadToEnd() 
    'Log the error contained in the "err" variable. 
    End Using 
    Return Nothing 
Finally 
    'Clean up 
End Try 

は、私は非常にお勧めDeveloperFusion converterを、使用して変換することができます。

+0

あなたのコードはとても涼しいです!しかし、完全なイベントハンドラで非同期Webサービスコールを使用しているので、どのようにしてResponseオブジェクトを取得できますか? – George2

+0

Responseオブジェクトは、トラップされたExceptionから抽出されます。あなたは例外を受け取りますか? P.S:答えがクールだと投票してください! ;-) – Cerebrus

+0

私が得ることができるのは、クライアントサイドからのArgumentNullExceptionです。 [サーバー名:ストリーム "} – George2

0

前の質問、easy-way-to-catch-all-unhandled-exceptions-in-c-netを参照してください。

IISで実行されているWebサービスでは、UnhandledExceptionModuleを実装することによって、すべてのスレッドで例外をキャッチする必要があるようです。

+0

解決策は、サーバー側のエラー処理のためのものですが、私の質問はクライアント側、より具体的にはWebサービスプロキシベースのクライアントアプリケーションからエラーを取得する方法です。 :-) – George2

1

Georgeは、非同期WSコールを使用しているため、コールバックメソッドで例外処理を実装する必要があります。 例: 以下は非同期のデリゲートをデモするために開発したサンプルコードです。

public class TransformDelegateWithCallBack 
{ 
    /// <summary> 
    /// Delegate which points to AdapterTransform.ApplyFullMemoryTransformations() 
    /// </summary> 
    /// <param name="filename">Transformation file name</param> 
    /// <param name="rawXml">Raw Xml data to be processed</param> 
    /// <param name="count">Variable used to keep a track of no of async delegates</param> 
    /// <returns>Transformed XML string</returns> 
    public delegate string DelegateApplyTransformations(string filename, string rawXml, int count); 

    public ArrayList resultArray; 


    //// Declare async delegate and result 
    DelegateApplyTransformations delegateApplyTransformation; 
    IAsyncResult result; 

    /// <summary> 
    /// Constructor to initialize the async delegates, results and handles to the no of tabs in excel 
    /// </summary> 
    public TransformDelegateWithCallBack() 
    { 
     resultArray = ArrayList.Synchronized(new ArrayList()); 
    } 


    /// <summary> 
    /// Invoke the async delegates with callback model 
    /// </summary> 
    /// <param name="filename">Transformation file name</param> 
    /// <param name="rawXml">Raw Xml data to be processed</param> 
    /// <param name="count">Variable used to keep a track of no of async delegates</param> 
    public void CallDelegates(string fileName, string rawXml, int count) 
    { 
     try 
     { 
      AdapterTransform adapterTrans = new AdapterTransform(); 
      // In the below stmt, adapterTrans.ApplyFullMemoryTransformations is the web method being called 
      delegateApplyTransformation = new DelegateApplyTransformations(adapterTrans.ApplyFullMemoryTransformations); 
      // The below stmt places an async call to the web method 
      // Since it is an async operation control flows immediately to the next line eventually coming out of the current method. Hence exceptions in the web service if any will NOT be caught here. 
      // CallBackMethod() is the method that will be called automatically after the async operation is done 
      // result is an IAsyncResult which will be used in the CallBackMethod to refer to this delegate 
      // result gets passed to the CallBackMethod 
      result = delegateApplyTransformation.BeginInvoke(fileName, rawXml, count, new AsyncCallback(CallBackMethod), null); 
     } 
     catch (CustomException ce) 
     { 
      throw ce; 
     } 
    } 

    /// <summary> 
    /// Callback method for async delegate 
    /// </summary> 
    /// <param name="o">By default o will always have the corresponding AsyncResult</param> 
    public void CallBackMethod(object o) 
    { 

     try 
     { 
      AsyncResult asyncResult = (AsyncResult)o; 
      // Now when you do an EndInvoke, if the web service has thrown any exceptions, they will be caught 
      // resultString is the value the web method has returned. (Return parameter of the web method) 
      string resultString = ((DelegateApplyTransformations)asyncResult.AsyncDelegate).EndInvoke((IAsyncResult)asyncResult); 

      lock (this.resultArray.SyncRoot) 
      { 
       this.resultArray.Add(resultString); 
      } 
     } 
     catch (Exception ex) 
     { 
      // Handle ex 
     } 
    } 

} 

あなたのWS呼び出しが例外を投げている場合は、AsynResultにEndInvokeをを行うときに、それが唯一の引っ掛かり。火災&を使用している場合、非同期WSコールのメカニズムを忘れると、EndInvokeを呼び出さないので、例外が失われます。だから、例外をキャッチする必要があるときに常にコールバックのメカニズムを使用してください これが役に立ちますようお願いいたします:)

これ以上疑問があれば教えてください。

+0

Rashmiありがとう、申し訳ありません私は非常にばかげた質問をしています。あなたのコードのどのメソッドが、私たちが呼び出したWebメソッドとして使用されていますか? – George2

+0

"WS呼び出しが例外をスローしている場合、AsynResultでEndInvokeを実行したときにのみ捕捉されます。" - 混乱します。私は、非同期Webサービスの呼び出しを使用すると、基本的なWebサービススタックが自動的にBegin/EndInvokeを呼び出すのに役立つと思いますので、再度呼び出す必要はありませんか? – George2

+0

delegateApplyTransformation =新しいDelegateApplyTransformations(adapterTrans.ApplyFullMemoryTransformations); adapterTrans.ApplyFullMemoryTransformationsは呼び出されているWebメソッドです。 –

0

Visual StudioでWebサービスをインポートし、Microsoft Web Services Enhancement 3を使用しているとします。すべてのエラーは、「MyWebService.DoSomethingCompletedEventArgs」オブジェクトの内部で返されます

​​

:0ライブラリ、それはおそらく、このようになります。

関連する問題