1

私は単純な WCF HTTP/SOAP Web サービスを持っています。サービスの実装は次のようになります。

public CustomResponse DoSomething(CustomRequest request)
{
    try
    {
        return InternalGubbins.WithErrorHandling.ProcessRequest(request);
    }
    catch
    {
        // Some sort of error occurred that is not gracefully
        // handled elsewhere in the framework
        throw new SoapException("Hmmm, it would seem that the cogs are meshed!", SoapException.ServerFaultCode);
    }
}

ここで、その SoapException がスローされた場合Hmmm, it would seem that the cogs are meshed!、追加の例外の詳細 (つまりスタック トレース) なしで、呼び出し元のクライアントに例外メッセージ (つまり ) が返されるようにします。

true (サーバー上の web.config)に設定includeExceptionDetailInFaultsすると、スタック トレースなどを含む完全な例外がクライアントに返されます。ただし、false に設定すると、一般的なメッセージが表示されます。

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

問題は、SoapException メッセージを呼び出し元のクライアントに戻すにはどうすればよいかということです。すなわち:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
        <a:RelatesTo>urn:uuid:185719f4-6113-4126-b956-7290be375342</a:RelatesTo>
    </s:Header>
    <s:Body>
        <s:Fault>
            <s:Code>
                <s:Value>s:Receiver</s:Value>
                <s:Subcode>
                    <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
                </s:Subcode>
            </s:Code>
            <s:Reason>
                <s:Text xml:lang="en-GB">Hmmm, it would seem that the cogs are meshed!</s:Text>
            </s:Reason>
        </s:Fault>
    </s:Body>
</s:Envelope>
4

2 に答える 2

2

操作で FaultContract を宣言し、FaultException を使用する必要があると思います (SoapException は WCF 以前です)。エラーがサービス コントラクトの一部でない場合、WCF はエラーをクライアントに送り返さないと思います。私は SoapException を試したことはありませんが、確かに FaultException をスローすることは常にうまくいきました。

[ServiceContract()]    
public interface ISomeService
{
     [OperationContract]
     [FaultContract(typeof(MyFault))]
     CustomResponse DoSomething(CustomRequest request)
}

public class SomeService
{
    public CustomResponse DoSomething(CustomRequest request)
    {
        ...
        throw new FaultException<MyFault>(new MyFault());
    }
}
于 2011-05-16T13:34:31.753 に答える
1

カスタム例外タイプを定義したくない場合は、これを試してください

try    
{        
    return InternalGubbins.WithErrorHandling.ProcessRequest(request);    
}    
catch    
{
    throw new FaultException("Hmmm, it would seem that the cogs are meshed.");    
}

そうすると、次の応答がクライアントに送信されます

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>s:Client</faultcode>
      <faultstring xml:lang="en-US">Hmmm, it would seem that the cogs are meshed.</faultstring>
    </s:Fault>
  </s:Body>
</s:Envelope>
于 2011-05-16T13:46:19.837 に答える