1

WCF サービスでスローされている例外を確認しようとしていますが、応答から得られるのは次のとおりです。

「サーバーは内部エラーのため、要求を処理できませんでした。エラーの詳細については、サーバーで (ServiceBehaviorAttribute または構成動作のいずれかから) IncludeExceptionDetailInFaults をオンにして、例外情報をクライアントに送り返します。または、Microsoft .NET Framework 3.0 SDK のドキュメントに従ってトレースをオンにして、サーバーのトレース ログを調べてください。」

「REST」の方法で行っているため、web.config にこれらのオプションがありません。では、WCF 4 REST テンプレートを使用するときに「IncludeExceptionDetailInFaults」を有効にするにはどうすればよいでしょうか?

4

1 に答える 1

0

これが私が思いついたものです:

web.config の standardEndpoint の下で、faultExceptionEnabled をオンにします。

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true">

次に、カスタム メッセージを表示するには、スローされた例外が FaultException である必要があります。私が使用する例を次に示します。

if (!Enum.GetNames(typeof(Models.Games.GsfCurrencyPrice.Sections)).Contains(section)) throw new FaultException<ArgumentException>(new ArgumentException("Value must be one of the following: " + string.Join(", ", Enum.GetNames(typeof(Models.Games.GsfCurrencyPrice.Sections))), "section"));

スローされると、次の応答が生成されます。

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code><Value>Sender</Value></Code><Reason><Text xml:lang="en-US">The creator of this fault did not specify a Reason.</Text></Reason><Detail><ArgumentException xmlns="http://schemas.datacontract.org/2004/07/System" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema"><ClassName i:type="x:string" xmlns="">System.ArgumentException</ClassName><Message i:type="x:string" xmlns="">Value must be one of the following: Buy, Sell</Message><Data i:nil="true" xmlns=""/><InnerException i:nil="true" xmlns=""/><HelpURL i:nil="true" xmlns=""/><StackTraceString i:nil="true" xmlns=""/><RemoteStackTraceString i:nil="true" xmlns=""/><RemoteStackIndex i:type="x:int" xmlns="">0</RemoteStackIndex><ExceptionMethod i:nil="true" xmlns=""/><HResult i:type="x:int" xmlns="">-2147024809</HResult><Source i:nil="true" xmlns=""/><WatsonBuckets i:nil="true" xmlns=""/><ParamName i:type="x:string" xmlns="">section</ParamName></ArgumentException></Detail></Fault>

お役に立てれば。

于 2011-02-04T01:31:57.327 に答える