4

Qクライアント側で元の例外(サーバーで発生)を取得するにはどうすればよいですか?

セルフホストのWCFサービスとC#4を使用して、適切な例外処理を設定しようとしています。

私はこのようなクライアントを持っています

private ServiceResponse PerformRemoteAction(ServiceRequest request, ProcessOperations operation)
{
    ...    
    try
    {
        //remote call
        switch (operation)
        {
            ...
            case ProcessOperations.VerifyAction: { response = client.VerifyAction(request); break; } 

            default: throw new NotImplementedException();
        }
    }
    catch (Exception ex)
    {
        AppManager.LogException(ex);
    }

    return response;
}

そしてサービスの実装

public override ServiceResponse VerifyAction(ServiceRequest request)
{
    RegisterRequest(request);

    try
    {
        var chain = new VerificationChain();
        Responce = chain.Run(request);
    }
    catch (Exception ex)
    {
        throw new FaultException<WcfException>(ExceptionFactory.Create(ex),
            new FaultReason(ex.Message));
    }

    SetSuccessfulResponce();

    return Responce;
}

サービスweb.configには

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>

これは私の最初の例外です ここに画像の説明を入力してください

そして、これは私がクライアントに得るものですここに画像の説明を入力してください

必要に応じて、クライアント側で完全な詳細を投稿できますが、クライアントの例外には元の例外への参照がありません。

更新
これは、FaultContractAttributeが定義された私のインターフェースです

[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/ITestRunnerService/VerifyAction", ReplyAction = "http://tempuri.org/ITestRunnerService/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/ITestRunnerService/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}

そしてこれは私のWcfExceptionクラスです

[DataContract]
public class WcfException : Exception
{
    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public new string Message { get; set; }

    [DataMember]
    public new string InnerException { get; set; }

    [DataMember]
    public new string StackTrace { get; set; }
}
4

4 に答える 4

4

代わりにFaultExceptionをキャッチしようとしましたか?

catch (FaultException<WcfException> ex)
{
   AppManager.LogException(ex);
}
catch (FaultException unknownFault)
{
   AppManager.LogException(unknownFault);
}

これを機能させるには、コントラクトにFaultContractAttributeを含める必要があります。

于 2011-06-06T10:07:29.887 に答える
3

WcfExceptionシリアル化可能とは何ですか?

いずれの場合も、このMSDNの記事で説明されているように、サーバーでトレースを有効にすることをお勧めします。これは、問題の診断に役立つはずです。

于 2011-06-06T10:22:26.453 に答える
2

元の例外に関する情報が必要な場合は、次を使用するだけで十分です。

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>
于 2011-06-06T11:05:39.337 に答える
1

すべての回答をありがとう。私の問題はサービスインターフェースにありました。つまり、FaultContractAttributeのActionプロパティで、間違ったURLを指していました。

[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/IServiceOperation/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}

他のすべてのプロパティ(自動的に決定される)を削除して解決し、WcfFaultのタイプのみを残しました

[ServiceContract]
    public interface IServiceOperation : IOperation
    {
        ...
        [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
        [FaultContractAttribute(typeof(WcfException))]
        ServiceResponse VerifyAction(ServiceRequest request);
    }
于 2011-06-06T12:45:19.670 に答える