どんなエキス内の例外からの石鹸の例外をASP.NET?
-
09-06-2019 - |
質問
私は簡単なwebサービス-オペレーションのこのような:
[WebMethod]
public string HelloWorld()
{
throw new Exception("HelloWorldException");
return "Hello World";
}
そしてクライアントアプリケーションを消費する、webサービスの呼び出します。明らかに例外をスローします:-)
try
{
hwservicens.Service1 service1 = new hwservicens.Service1();
service1.HelloWorld();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
私のキャッチブロックでは、どのようなエキスにメッセージの実際の例外のご利用にあたり私のコードです。の例外で獲れは SoapException
, るもので Message
物件はしんでみてはいかがでしょうか。
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: HelloWorldException
at WebService1.Service1.HelloWorld() in C:\svnroot\Vordur\WebService1\Service1.asmx.cs:line 27
--- End of inner exception stack trace ---
---と、 InnerException
は null
.
思い出 Message
物件の InnerException
( HelloWorldException
テキストに私のサンプル),誰でもできるのです。ができるので避けてくださいこの解析に Message
物件の SoapException
.
解決
残念ながらように思うことが可能です。
を除き上げwebサービスコードが符号化へのSoapフォルトしてい渡された文字列として返クライアントのコードです。
んのSoapExceptionメッセージは単純にテキストからのSoapフォルトではなへの変換例外がけとして格納されます。
ばい有用な情報のエラー条件をお勧めしま返すカスタムクラスからwebサービスことができる"エラー"プロパティを含むキーを使用しています。
[WebMethod]
public ResponseClass HelloWorld()
{
ResponseClass c = new ResponseClass();
try
{
throw new Exception("Exception Text");
// The following would be returned on a success
c.WasError = false;
c.ReturnValue = "Hello World";
}
catch(Exception e)
{
c.WasError = true;
c.ErrorMessage = e.Message;
return c;
}
}
他のヒント
で は 可能です!
サービス運用例:
try
{
// do something good for humanity
}
catch (Exception e)
{
throw new SoapException(e.InnerException.Message,
SoapException.ServerFaultCode);
}
お客様の消費のサービス:
try
{
// save humanity
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
だけでも設定する必要がありますcustomErrorsオープン(クラスメソッド)mode='RemoteOnly'または'に'おいます。config (本サービスのプロジェクト)。
単位のcustomErrors発見- http://forums.asp.net/t/236665.aspx/1
また似たような少前 bloggedで.いる場合は正確には適用がなのかもしれません。このコードは、一度実現することを通MessageFaultオブジェクトです。私の場合、というのは判っていましたので詳細が含まれることで使うことがで再クエリのソープのサービスです。次のようなコードになります:
catch (FaultException soapEx)
{
MessageFault mf = soapEx.CreateMessageFault();
if (mf.HasDetail)
{
XmlDictionaryReader reader = mf.GetReaderAtDetailContents();
Guid g = reader.ReadContentAsGuid();
}
}